Hi all,
Newbie here. Trying to do something real basic: get 2 Xbee Series 1 to talk to each other. I'm using 2 UNOs (R3), and 2 wireless shields (arduino made).
Goal: When UNO 1 has a button pushed, it lights an LED on its own circuit, and should send a message to UNO 2 to light its own LED on its own respective circuit.
Problem: When I push the button on UNO 1 (button is on breadboard), only UNO 1 will have its LED light. Also, UNO 2 is supposed to show status of any incoming data on the serial monitor, and it does not.
I used the Xbee explorer and Putty to set the configurations for each of the Xbees:
*Xbee 1 (which is on UNO 1):
my ID: ATMY1000
partner ID: ATDL1001
PAN: ATID1111
WR
*Xbee 2(which is on UNO 2):
my ID: ATMY1001
partner ID: ATDL1000
PAN: ATID1111
WR
Putty responded and said "OK" after everything.
So, I have 2 codes: #1 is the code on UNO 1 (sender), and #2 is code on UNO 2 (receiver)
/*
* Light LED only if button is pressed
* Xbee 1 (sender)
*/
int ledPin = 12; // LED is connected to pin 12
int button = 2; // button is connected to pin 2
int val = LOW; // variable for reading the pin status
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // Set the LED pin as output
pinMode(button, INPUT); // Set the button as input
}
void loop(){
val = digitalRead(button); // read input value and store it in val
if (val == HIGH)
{ // check if the button is pressed
digitalWrite(ledPin, HIGH); // turn LED on
}
else // (val == LOW)
{
digitalWrite(ledPin, LOW); // turn LED off
}
Serial.println(val);
delay(50);
}
/*
* Light LED only if button is pressed
* Xbee 1 (receiver)
*/
int ledPin = 12; // LED is connected to pin 12
void setup() {
Serial.begin(9600); // xbees are communicating over sieral data, except it is occuring wirelessly
pinMode(ledPin, OUTPUT); // Set the LED pin as output
}
void loop(){
while(Serial.available() == 0); /* read in serial value. This waits until we actually have a value
*/
int data = Serial.read() - '0'; /*
This converts the byte or char value into an integer.
This is because '0' is the decimal representation of the number 0.
*/
// val = digitalRead(button); // read input value and store it in val
if (data != 0)
{ // check if the button is pressed
digitalWrite(ledPin, HIGH); // turn LED on
}
else // (val == LOW)
{
digitalWrite(ledPin, LOW); // turn LED off
}
//Serial.println(val);
Serial.println(data);
delay(50);
Serial.flush(); /*
Any extra data which comes in after it read Serial.read, you
only want to read the character that we care about, and flush
the buffer out.
*/
}