Xbee 2 Xbee

i have two xbee attached to two diff Arduino boards. I'm trying to communicate(wireless) between both boads such that if pin 6 on Arduino board A (with xbee sender) goes high, Arduino boad B (with xbee receiver) blinks an LED on pin 5 of the receiver until pin 6 on Arduino board A (sender)goes low.

Below is the code i used but im not getting the desired result. please help

//Sender (Board A)

#define Button 6
//int inPin = 6; //
int val = 0;
int old_val = 0;
int state = 0;
int val5 = 86;

void setup() {
pinMode(Button,INPUT); //
pinMode(13, OUTPUT);
Serial.begin(9600);
Serial.println("Chip 1 online");
delay(2000);
// set the data rate for the SoftwareSerial port
Serial.begin(9600);
// mySerial.println(”Whats for dinner?”);
}

void loop() // run over and over again
{
val = digitalRead(Button); // read input value and store it. Yum, fresh
// check if there was a transition
if ((val == HIGH) && (old_val == LOW))
{
state = 1 - state;
Serial.println(2);
Serial.println(5);
Serial.println("Rising Edge");
delay(250);
}
old_val = val; // val is now old, let's store it

if (Serial.available()) {
Serial.print((char)Serial.read());
}
if (Serial.available()) {
Serial.print((char)Serial.read());
}

delay(50);
Serial.println(val); //

}

//Receiver (board B)
int LEDpin = 5;
byte address;
byte data;

void setup() {

Serial.begin(9600);
pinMode(LEDpin,OUTPUT);
pinMode(13, OUTPUT);
Serial.println("Chip 2 online");

}

void loop() // run over and over again
{

if (Serial.available()) {
// Serial.print((char)mySerial.read()); // took this out so I do not loose a byte
address = Serial.read();
Serial.print(address);
if (address == '2')
{
data = Serial.read();
if (data == '5');
{
digitalWrite(LEDpin,HIGH);
delay(1000);
digitalWrite(LEDpin,LOW);
}

Serial.print(data);

}

}
if (Serial.available()) {
Serial.print((char)Serial.read());
}
delay(50);

}

original source

I don't understand your problem : the video on your page shows that it works, and you say it : "It works pretty well. :]"

Thus, what help do you need :slight_smile:

The original source hasn't been implemented exactly the same.

It uses SYNC bytes, eg..

if ((int(incomingByte) == 254)) {
sensor1 = Serial.read();
Serial.print(”Sensor 1 = “);
Serial.print(int(sensor1));

So after 254 is received, the next byte received is the value for sensor1.

You've also got 2 Serial.available() handlers, the last one will keep reading bytes received and do nothing with them.

And if your transmitting and receiving bytes, try this:-

data = Serial.read();
if (data == 5);

Instead of this:-

data = Serial.read();
if (data == '5');