I'm trying to figure out to how send data both ways and writing this small test I can see something's wrong.
I'm probably missing some basic info here and would appreciate your help.
The Sending Arduino (nano) attached to S1 Xbee, sending values from 1 - 70 every 1500ms.
The receiving Arduino (Uno) attached to S1 Xbee, receives that data and acts accordingly. i.e changes the LED state every 10 data values, and sends back data (2) to the Nano. Now, I can see that the Nano's Xbee receives (the RSSI LED lights up every 10 data values), but action is not taken for some reason. I'm trying to differentiate between any data received and specific data - therefore only upon the receiving of (2) the Nano needs to act - but fails to.
What am I doing wrong here?
Sender Code:
//Xbee Send and Receive test - Sender.
//Meant to receive back every 10 and change LED state.
long currentMillis=0;
long previousMillis = 0;
int delayMillis = 1500;
int x=0;
int y = 0;
int LED = 13;
boolean ledState = HIGH;
void setup ()
{
Serial.begin (115200);
pinMode (LED, OUTPUT);
digitalWrite(LED, HIGH);
}
void loop ()
{
currentMillis = millis();
if (Serial.available()>0)
{
y = Serial.read();
if (y == 2)
{
ledState = !ledState;
digitalWrite (LED, ledState);
}
}
if (currentMillis - previousMillis>delayMillis)
{
x++;
if (x>70)
x = 0;
Serial.write (x);
//Serial.print (x);
previousMillis = currentMillis;
}
}
Receiver code:
// Xbee Send and Receive test - Receiver.
// Meant to send back every 10.
int x = 0;
int LED = 13;
boolean ledState = HIGH;
void setup ()
{
Serial.begin (115200);
pinMode (LED, OUTPUT);
digitalWrite(LED, HIGH);
}
void loop ()
{
if (Serial.available())
{
x = Serial.read();
if(x == 10||x == 20||x == 30||x == 40 ||x == 50 || x == 60||x == 70)
{
ledState = !ledState;
digitalWrite (LED, ledState);
Serial.write (2);
}
}
}
Another weird thing that may or may not point to a problem is that upon opening the sender's serial monitor, I get this after a few seconds: " !"#$%&'()*+,-./012345678 " and so on...