Problem:
I am able to send a signal and have it received, but when I want to send a signal back, it doesn't work.
Sender (defs & setup):
#include <LCD4Bit.h> //LCD library
#undef int() //For stdio
#include <stdio.h> //For sprintf
#define debugPin 13 //Pin definitions
#define pinSpeaker 37
#define setSwitchPin 22
#define alarmSwitchPin 26
#define alarmEnabledPin 30
#define incPin 32
#define decPin 33
#define setPin 38
LCD4Bit lcd = LCD4Bit(2); //2 line LCD
//Initial time set
int sec=40;
int min=55;
int hour=22;
int amin=56; //Initial alarm set
int ahour=22;
int alarmHit=0;
int aDisable=0;
char time[9]; //Time string for LCD
int check=0; //Button set value
//Set button variables
int lastSetState = LOW;
int lastAlarmState = LOW;
int lastAlarmEnabledState = LOW;
long lastDebounceTime = 0;
long debounceDelay = 50;
boolean alarmSet = false; //Alarm on/off value, default off
void setup() { //Initialization
pinMode(debugPin, OUTPUT); //Arduino debug LED
pinMode(pinSpeaker, OUTPUT); //Piezo buzzer
pinMode(setSwitchPin, INPUT); //Set button
pinMode(alarmSwitchPin, INPUT); //Alarm time button
pinMode(alarmEnabledPin, INPUT); //Alarm enabled switch
pinMode(incPin, INPUT); //Inc mom
pinMode(decPin, INPUT); //Dec mom
pinMode(setPin, INPUT); //Set mom
Serial.begin(9600); //COM Serial
Serial1.begin(9600); //Xbee Serial
lcd.init(); //LCD prep
lcd.clear();
}
Sender (loop):
void loop() {
unsigned long msbegin=millis();
unsigned long msafter;
digitalWrite(13, HIGH); //Light the debug LED
lcd.cursorTo(1, 0); //Place cursor line 1 char 0
lcd.printIn(inctime()); //Print time
digitalWrite(13, LOW); //Unlight the debug LED
if (alarmSet == true) { //Only run if the alarm is on
lcd.cursorTo(2, 0); //Place cursor line 2 char 0
lcd.printIn("Wakeup at:");
if (ahour == hour && amin == min) { //Clock time = alarm time?
alarmHit = 1; //Need to store the state of the alarm for after the 60s time interval
Serial.println("Alarm!!!!!");
} else {
alarmHit = 0;
Serial.println("Alarm set off, but no more...");
}
if (alarmHit == 1 && aDisable == 0) { //Separate statemnent to allow for beyond 60s limit
//Send signal to xBee to start waiting for a button press
Serial.println("Sending Xbee Start");
Serial1.print(1, DEC);
delay (10);
lcd.cursorTo(2, 0); //Place cursor line 2 char 0
lcd.printIn("WAKE UP! ");
int playFreq = 800; //Frequency of buzzer
while (Serial1.read() == -1) {
playTone(1000, playFreq);
delay (60);
playFreq = playFreq - 100;
if (playFreq == 300) {
playFreq = 800;
Serial.println("Still waiting to receive shutoff button");
}
//aDisable = Serial1.read(); //Once a button press is sent back, set aDisable to 1
//Would add-in max count here for setting shower off
}
Serial.println("Shutoff signal received");
aDisable = 1;
}
lcd.cursorTo(2, 0); //Place cursor line 2 char 0
lcd.printIn("Wakeup at:"); //Set back to enable alarm for the next day
lcd.cursorTo(2, 11); //Place cursor line 2 char 11
sprintf(time, "%2d:%2d", ahour, amin); //Print alarm setting time
lcd.printIn(time);
}
//alarmSet = false; //Set alarm to disable
msafter = millis();
if (msafter < msbegin) { //Millis() reset after 9h due to overflow
delay(1000);
}
else {
delay(1000-(msafter-msbegin));
}
checkButtons(); //Did you toggle the alarm set, time set, or alarm enabled?
}
Receiver:
#define overridePin 5 //Pin definitions
#define shutoffPin 6
#define relayPin 7
int overReading; //Button readings
int lastoverReading;
int shutoffReading;
int lastshutoffReading;
int overCount=0; //Override count
int loopMe=0; //Remeber to loop
void setup() { //Initialization
pinMode(overridePin, INPUT);
pinMode(shutoffPin, INPUT);
pinMode(relayPin, OUTPUT);
Serial.begin(9600); //Xbee Serial
}
void loop() { //Main loop
overReading = digitalRead(overridePin);
//Override pressed?
if (overReading == 0) {
if (overCount==0) { //Relay currently off
digitalWrite(relayPin, HIGH); //Set on
overCount=1; //Store HIGH value
} else {
digitalWrite(relayPin, LOW); //Set off
overCount=0; //Store LOW value
}
}
lastoverReading = overReading; //Store previous button read
if (Serial.read() != -1) {
digitalWrite(relayPin, HIGH); //Turn on Shower
loopMe = 1;
if (loopMe == 1) {
//Looking for the disabling of alarm
shutoffReading = digitalRead(shutoffPin);
//Check Press
if (shutoffReading == 0) {
for (int i=0; i<1000; i++) {
Serial.print(1, DEC); //Send Xbee signal that the button was pressed
//Store previous button read
}
delay (10);
loopMe = 0;
}
}
}
}