I am trying to simultaneously read temperature remotely from a temperature sensor through the router Xbee to the coordinator Xbee and do the same but remotely control LEDs from digital inputs on the keyboard.
With my code below I can only read one input at a time so I’m not exactly sure what to do as i have tried interrupts and delays.
Any help please.
int myData = 0;
int ledCool = 2;
int ledWarn = 4;
int const ledControl = 7;
float temp;
void setup() {
pinMode(ledCool, OUTPUT);
pinMode(ledWarn, OUTPUT);
Serial.begin(9600);
pinMode(ledControl, OUTPUT);
}
void loop(){
remoteLight();
delay(1);
remoteTemp();
delay(1);
}
void remoteTemp(){
// Remote XBee: AT, Base XBee: API
if (Serial.available() >= 21) { // Make sure the frame is all there
if (Serial.read() == 0x7E) { // 7E is the start byte
for (int i = 1; i<19; i++) { // Skip ahead to the analog data
byte discardByte = Serial.read();
}
//Reading the temperature
int analogMSB = Serial.read(); // Read the first analog byte data
int analogLSB = Serial.read(); // Read the second byte
int analogReading = analogLSB + (analogMSB * 256);
temp = analogReading / 1023.0 * 1.23;
temp = temp - 0.5;
temp = temp / 0.01;
if (temp < 21){
digitalWrite(ledCool,HIGH);
digitalWrite(ledWarn,LOW);
Serial.print(temp);
Serial.println(" degrees C");}
else if (temp >= 21){
digitalWrite(ledWarn,HIGH);
delay(1000);
digitalWrite(ledWarn,LOW);
delay(1000);
{digitalWrite(ledCool,LOW);
Serial.print("WARNING Temperature is ");
Serial.println(temp);
Serial.println(" degrees C");
}
}
}
}
}
void remoteLight(){
//On/Off switch from keypad
if(Serial.available() > 0){
myData = Serial.read();//Read the data from the keypad
if(myData == '1'){
digitalWrite(ledControl, HIGH);
Serial.println(" On");}
if(myData == '2'){
digitalWrite(ledControl, LOW);
Serial.println("OFF");
}
}
}