BWSN – Better Doorbells

Here is a circuit demo of the “Chapter 3 – Doorbell Project” with feedback:

As I mentioned in the video, here is my code for sending data at the the button transitions instead of all the time when the switch is pressed.

Here is the doorbell button side:

/*
   State Based XBee doorbell - Button Side

   IF the button is pushed and this is a change of state it will send a "P" (for Pushed)
   IF the button is released and this is a change of state it will send a "R" (for released)

   Buzzer will send an 'A' when it gets the PUSHED status.
   Buzzer will send an 'B' when it gets the NOT_PUSHED status.

  Created by James Stokebrand, Aug 6, 2013.
  Released into the public domain.

  Based on the "Doorbell Feedback BUTTON" sketch from
  "Building Wireless Sensor Networks by Robert Faludi.
   Copyright 2011 Robert Faludi,
   ISBN 978-0-59680773-3"
   http://faludi.com

 */

// LED on pin 10
int LED = 10;

// Button on pin 2
int BUTTON=2;

// Button States
int PUSHED = 1;
int NOT_PUSHED = 2;

// Keep track of the current states.
int currentState = NOT_PUSHED;
int newState = NOT_PUSHED;

void setup() {
  // init the LED as an output.
  pinMode(LED, OUTPUT);
  digitalWrite(LED, LOW); // Init to low (off)

  // Init the button as an input.
  pinMode(BUTTON, INPUT);

  // Initialize serial over XBee
  Serial.begin(9600);
}

void loop() {
  // Read the state of the button
  if (digitalRead(BUTTON) == HIGH)
  {
    // If its pushed set the new state
    newState = PUSHED;
  }
  else
  {
    newState = NOT_PUSHED;
  }

  // Check if there is a state change.
  if (currentState != newState)
  {
    // State change ...
    if (newState == PUSHED)
    {
      // Notify Buzzer that the button's been pushed.
      Serial.print('P');
    }
    else // newState == NOT_PUSHED
    {
      // Notify Buzzer the button's been released.
      Serial.print('R');
    }
    // Save the new state
    currentState=newState;
  }

  // Check for communications.
  if (Serial.available() > 0)
  {
    char feedback = Serial.read();
    if (feedback == 'A')
    {
      // Buzzer confirms the button was pushed ... light up the LED.
      digitalWrite(LED,HIGH);
    }
    else if (feedback == 'B')
    {
      // Buzzer confirms the button was released ... turn off the LED.
      digitalWrite(LED,LOW);
    }
  }
}

And here is the buzzer side:

/*
State Based XBee doorbell - Buzzer Side

When the button is pushed should get a "P" (for Pushed) over serial.
Buzzer will send an "A" to acknowledge the button was pushed.

When the button is released should get a "R" (for Released) over serial.
Buzzer will send a "B" to acknowledge the button was released.

Created by James Stokebrand, Aug 6, 2013.
Released into the public domain.

Based on the "Doorbell Feedback BELL" sketch from
"Building Wireless Sensor Networks by Robert Faludi.
Copyright 2011 Robert Faludi,
ISBN 978-0-59680773-3"
http://faludi.com
*/

// Constants for the pin inputs.
int LED = 10;
int BELL = 5;

// State machine states.
int PUSHED = 1;
int NOT_PUSHED = 2;

// Set the current state
int currentState = NOT_PUSHED;

void setup() {
// Init the LED  as output
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW); // Init to low (off)

// Init the BELL pin as output
pinMode(BELL, OUTPUT);

// Init serial with the XBee
Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {

// State machine is serial driven.  Is there data available?
if (Serial.available() > 0)
{
// Have a char...
char feedback = Serial.read();

if (feedback == 'P')  // If the button was pushed... ack with an 'A' and buzz.
{
// Ack with an "A" char response.
Serial.print('A');
currentState = PUSHED;  // Change states to pushed.
digitalWrite(LED,HIGH);  // Turn the LED on.
}
else if (feedback == 'R') // For released ... ack with a 'B'
{
// Ack with a "B" response.
Serial.print('B');
currentState = NOT_PUSHED;  // Change states to released.
digitalWrite(LED,LOW);  // Turn the LED off.
}
}

// If the button is pushed ... sound the buzzer.
if (currentState == PUSHED)
{
digitalWrite(BELL,HIGH);
delay(10);
digitalWrite(BELL,LOW);
}
}

Instead of spamming the channel with char’s each time the button is detected low, I send a char on button high/low transitions. Lowers the number of chars sent across the XBee communication bridge.

Advertisements
This entry was posted in BWSN. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s