Perry - yes, simpler to replace the buttons, but I would not have known that Schmitt triggers even exist if I had gone the simple route. I enjoy learning new things. And yes, inverts the logic, which I will flip when I have sorted out the components - needed to place an order.
Larry - code is below, I had posted it on the programming section of the forum to get help on the ISR routine since it does not work well, but I think (am guessing) that my issues come for the button bounce.
I will eventually want to add an if statement so that I only put the arduino & xbee to sleep after 4 seconds of button activity. Something like :
unsigned long currentMillis = millis();
if (currentMillis - previousMillis > AwakeDuration) {
sleepNow();
previousMillis = currentMillis;
}
but I am not sure if this will actually work - not sure of what happens to the "millis" when interrupts happen, will it screw up the 4 second period (my AwakeDuration value) because they keep "ticking" while the interupt is busy doing other things than the main loop.
#include <avr/sleep.h>
// Declare pin numbers for feedback LEDs 1 thru 4
const int LED1_pin = 10;
const int LED2_pin = 16;
const int LED3_pin = 14;
const int LED4_pin = 15;
// Declare pin numbers for bell and light buttons and power
const int BTN_Bell = 7;
const int BTN_Light_ON = 2;
const int BTN_Light_OFF = 3;
const int Xbee_Pwr_Toggle = A1;
int BTN_Light_state = HIGH;
int BTN_Bell_state = HIGH;
int previousBTN_Bell_state = LOW;
void setup()
{
pinMode(LED1_pin, OUTPUT);
pinMode(LED2_pin, OUTPUT);
pinMode(LED3_pin, OUTPUT);
pinMode(LED4_pin, OUTPUT);
pinMode(Xbee_Pwr_Toggle, OUTPUT);
pinMode(BTN_Bell, INPUT_PULLUP);
pinMode(BTN_Light_ON, INPUT_PULLUP);
pinMode(BTN_Light_OFF, INPUT_PULLUP);
// Open Serial com ports to send data via Xbee board,
// rate matches your XBee setting (9600 is default).
Serial1.begin(9600);
}
void loop()
{
// button send portion to remote units
// on button press turn remote light (relay) on or off
sleepNow();
BTN_Light_state = digitalRead(BTN_Light_ON);
if (BTN_Light_state == LOW) {
delay(50); // pause to give it time to wake up
Serial1.write('L'); // light btn is pressed
delay(275); // long delay to wait out the bounce
}
BTN_Light_state = digitalRead(BTN_Light_OFF);
if (BTN_Light_state == LOW) {
delay(50); // pause to give it time to wake up
Serial1.write('Z'); // light btn is pressed
delay(275); // long delay to wait out the bounce
}
BTN_Bell_state = digitalRead(BTN_Bell);
if (BTN_Bell_state != previousBTN_Bell_state) {
delay(10); // pause to give it time to wake up
if (BTN_Bell_state == LOW) {
Serial1.write('B'); // bell btn is pressed
}
else {
Serial1.write('O'); // bell btn is released
}
previousBTN_Bell_state = BTN_Bell_state;
delay(50);
}
// feedback section from remote units - who got the message ?
// turn corresponding LED on or off to know that remote light is on or off
if (Serial1.available() > 0) {
char Msg = Serial1.read();
switch (Msg) {
case '1': // If received '1' station 1 at OFF
digitalWrite(LED1_pin, LOW);
break;
case '2': // If received '2' station 2 at OFF
digitalWrite(LED2_pin, LOW);
break;
case '3': // If received '3' station 3 at OFF
digitalWrite(LED3_pin, LOW);
break;
case '4': // If received '4' station 4 at OFF
digitalWrite(LED4_pin, LOW);
break;
case '5': // If received '5' station 1 at ON
digitalWrite(LED1_pin, HIGH);
break;
case '6': // If received '6' station 2 at ON
digitalWrite(LED2_pin, HIGH);
break;
case '7': // If received '7' station 3 at ON
digitalWrite(LED3_pin, HIGH);
break;
case '8': // If received '8' station 4 at ON
digitalWrite(LED4_pin, HIGH);
break;
}
delay(50);
}
}
void sleepNow()
{
digitalWrite(Xbee_Pwr_Toggle, LOW);
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
attachInterrupt(digitalPinToInterrupt(BTN_Light_ON), wakeUp, LOW);
attachInterrupt(digitalPinToInterrupt(BTN_Light_OFF), wakeUp, LOW);
attachInterrupt(digitalPinToInterrupt(BTN_Bell), wakeUp, LOW);
sleep_mode();
}
void wakeUp()
{
sleep_disable();
detachInterrupt(digitalPinToInterrupt(BTN_Light_ON));
detachInterrupt(digitalPinToInterrupt(BTN_Light_OFF));
detachInterrupt(digitalPinToInterrupt(BTN_Bell));
digitalWrite(Xbee_Pwr_Toggle, HIGH);
}