arduino to xbee - xbee to arduino

Hello Arduino Users

I am look for some help / guidance.
I am work with 2 arduinos and 2 xbee. I am having a problem with communication.

I am using one set a sender and other as a receiver.

The coe is base on the example from ArduinoFun.com
arduinofun.com/blog/2009/10/19/arduino-wireless-xbee/

Here is my code.

// SENDER

// set pin numbers:
const int buttonPin1 = 2; // the number of the pushbutton pin
const int buttonPin2 = 3;
const int buttonPin3 = 4;
const int buttonPin4 = 5;
const int buttonPin5 = 6;

const int ledPin = 13; // the number of the LED pin

void setup()
{
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);

// initialize the pushbutton pin as an input:
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
pinMode(buttonPin5, INPUT);
}

void loop()
{
if (digitalRead(buttonPin1) == HIGH)
{
digitalWrite(ledPin, HIGH); // turn LED on
Serial.print(251, BYTE);
delay(150);
}

else if (digitalRead(buttonPin2) == HIGH)
{
digitalWrite(ledPin, HIGH); // turn LED on
Serial.print(252, BYTE);
delay(150);
}

else if (digitalRead(buttonPin3) == HIGH)
{
digitalWrite(ledPin, HIGH); // turn LED on
Serial.print(253, BYTE);
delay(150);
}

else if ( digitalRead(buttonPin4) == HIGH)
{
digitalWrite(ledPin, HIGH); // turn LED on
Serial.print(254, BYTE);
delay(150);
}

else if (digitalRead(buttonPin5) == HIGH)
{
digitalWrite(ledPin, HIGH); // turn LED on
Serial.print(255, BYTE);
delay(150);
}

else
{
// turn LED off:
digitalWrite(ledPin, LOW);
}
}

// RECEIVER

byte incomingByte;
const int output1 = 9;
const int output2 = 8;
const int output3 = 7;
const int output4 = 6;
const int output5 = 4;

void setup()
{
// start serial port at 19200 bps
Serial.begin(19200);
Serial.println("Ready!");

// Motor/Led pins outs

pinMode (output1, OUTPUT);
pinMode (output2, OUTPUT);
pinMode (output3, OUTPUT);
pinMode (output4, OUTPUT);
pinMode (output5, OUTPUT);

delay(1000);
}

void loop()
{
if (Serial.available())
{ // are there any bytes available on the serial port ???

// assign bytes to the var 'incomingByte'
incomingByte = Serial.read();

Serial.print(int(incomingByte));

// from now on is pretty clear I guess :slight_smile:
if((int(incomingByte) == 251)) //foward
{
digitalWrite(output1, LOW);
digitalWrite(output2, HIGH);
digitalWrite(output3, LOW);
digitalWrite(output4, HIGH);
}
else if((int(incomingByte) == 252)) //reverse
{
digitalWrite(output1, HIGH);
digitalWrite(output2, LOW);
digitalWrite(output3, HIGH);
digitalWrite(output4, LOW);
}

else if((int(incomingByte) == 253)) //left
{
digitalWrite(output1, HIGH);
digitalWrite(output2, HIGH);
digitalWrite(output3, LOW);
digitalWrite(output4, HIGH);
}

else if((int(incomingByte) == 254)) //right
{
digitalWrite(output1, LOW);
digitalWrite(output2, HIGH);
digitalWrite(output3, HIGH);
digitalWrite(output4, HIGH);
}

else if((int(incomingByte) == 255)) //led
{
digitalWrite(output5, HIGH);
}

else
{
digitalWrite(output1, HIGH);
digitalWrite(output2, HIGH);
digitalWrite(output3, HIGH);
digitalWrite(output4, HIGH);
digitalWrite(output5, LOW);
}

}
//end
}

Thanks Yaz

I think that you are missing the serial begin in the sender. If i am mistaken, sorry, but did not see it there.

I am having a problem with communication.

Oh, what a shame. It's too bad that the problem is so secret that you can't share it with us. If you could, there's a good chance that a solution could be arrived at.

thank you goldenclan.

i was able to get it to communicate by adding Serial.begin(19200);
into the sender.

but now it is doing olny one thing. when i press (sender) buttonPin2 - buttonPin5 does the same thing (which is turn on 1 of 2 motors), expect buttonPin1 trun it off.

when u look at the receiver code, each input from the sender should have a different output.

Thanks Yaz

You haven't enabled the pull-up resistors on the sender. Do you have external pull-up resistors for the switches? What you are describing sounds like a floating pin problem, caused by the pins not having a defined state when the button is not pressed. This can be cured by using pull-up or pull-down resistors.

You haven't enabled the pull-up resistors on the sender. Do you have external pull-up resistors for the switches? What you are describing sounds like a floating pin problem, caused by the pins not having a defined state when the button is not pressed. This can be cured by using pull-up or pull-down resistors.

yes i have external pull-up resistors for the switchers on the sender.
here you can have a look of the sender and the receiver.

SENDER

The sender is being powered via USB. I am using 5x 10Kohm resistors for the push buttons and 1x 220ohm for the led.

RECEIVER

The receiver is being powered via a 9V battery. I am using the L293D chip (Quadruple Half-H) to driver the two motors. I am using 1x 220ohm for the led.

L293D chip (Quadruple Half-H)

here is my updated code

The changes I made are; I added Serial.println("Ready!"); and change the push buttons to be active low enable in the sender.

// SENDER

// set pin numbers:
const int buttonPin1 = 2; // the number of the pushbutton pin
const int buttonPin2 = 3;
const int buttonPin3 = 4;
const int buttonPin4 = 5;
const int buttonPin5 = 6;

const int ledPin = 13; // the number of the LED pin

void setup()
{
// start serial port at 19200 bps
Serial.begin(19200);
Serial.println("Ready!");

// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);

// initialize the pushbutton pin as an input:
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
pinMode(buttonPin5, INPUT);
}

void loop()
{
if (digitalRead(buttonPin1) == LOW)
{
digitalWrite(ledPin, HIGH); // turn LED on
Serial.print(251, BYTE);
delay(150);
}

else if (digitalRead(buttonPin2) == LOW)
{
digitalWrite(ledPin, HIGH); // turn LED on
Serial.print(252, BYTE);
delay(150);
}

else if (digitalRead(buttonPin3) == LOW)
{
digitalWrite(ledPin, HIGH); // turn LED on
Serial.print(253, BYTE);
delay(150);
}

else if ( digitalRead(buttonPin4) == LOW)
{
digitalWrite(ledPin, HIGH); // turn LED on
Serial.print(254, BYTE);
delay(150);
}

else if (digitalRead(buttonPin5) == LOW)
{
digitalWrite(ledPin, HIGH); // turn LED on
Serial.print(255, BYTE);
delay(150);
}

else
{
// turn LED off:
digitalWrite(ledPin, LOW);
}
}

// RECEIVER

byte incomingByte;
const int output1 = 9;
const int output2 = 8;
const int output3 = 7;
const int output4 = 6;
const int output5 = 4;

void setup()
{
// start serial port at 19200 bps
Serial.begin(19200);
Serial.println("Ready!");

// Motor/Led pins outs

pinMode (output1, OUTPUT);
pinMode (output2, OUTPUT);
pinMode (output3, OUTPUT);
pinMode (output4, OUTPUT);
pinMode (output5, OUTPUT);

delay(1000);
}

void loop()
{
if (Serial.available())
{ // are there any bytes available on the serial port ???

// assign bytes to the var 'incomingByte'
incomingByte = Serial.read();

Serial.print(int(incomingByte));

// from now on is pretty clear I guess
if((int(incomingByte) == 251)) //foward
{
digitalWrite(output1, LOW);
digitalWrite(output2, HIGH);
digitalWrite(output3, LOW);
digitalWrite(output4, HIGH);
}
else if((int(incomingByte) == 252)) //reverse
{
digitalWrite(output1, HIGH);
digitalWrite(output2, LOW);
digitalWrite(output3, HIGH);
digitalWrite(output4, LOW);
}

else if((int(incomingByte) == 253)) //left
{
digitalWrite(output1, HIGH);
digitalWrite(output2, HIGH);
digitalWrite(output3, LOW);
digitalWrite(output4, HIGH);
}

else if((int(incomingByte) == 254)) //right
{
digitalWrite(output1, LOW);
digitalWrite(output2, HIGH);
digitalWrite(output3, HIGH);
digitalWrite(output4, HIGH);
}

else if((int(incomingByte) == 255)) //led
{
digitalWrite(output5, HIGH);
}

else
{
digitalWrite(output1, HIGH);
digitalWrite(output2, HIGH);
digitalWrite(output3, HIGH);
digitalWrite(output4, HIGH);
digitalWrite(output5, LOW);
}

}
//end
}

Thanks Yaz

There are a number of potential areas for error. The hardware on sender side (the switches, resistors, and wires) could have a problem.

The hardware on the receiver side could have a problem.

There might be problems getting data from the sender Arduino to the XBee. There may be problems getting the data from the receiver XBee to the receiver Arduino.

The sender might be sending the wrong data when a button is pressed (a software problem). The receiver may not being interpreting the incoming data correctly.

You really should create a test sketch, for the sender, and remove the XBee and shield. Verify that all button presses produce exactly one output to the serial monitor, and that the output is what you expect. This removes the hardware problem on the sender possibility and validates the code on the sender.

When that works, remove the XBee and shield from the receiver. Write a simpl sketch that cycles through each state (left, right, forward, reverse, stopped) for 10 seconds at a time.

When you know the receiver hardware is working, modify the sketch to read serial data, and send it serial data using the Serial Monitor. Verify that the serial to state conversion is behaving correctly.

Of course, in order to be able to send commands from the serial monitor, you'll need to change the receiver to expect 'L', 'R', 'F', 'B', and 'S', instead of 251, 252, 253, 254, or 255. But, this should be done anyway.

Anyone looking at the code can expect that 'S' should stop the motors, that 'R' should turn right, etc. There is no reasonable expectation for an action associated with 253.

Try making these sample sketches, and let us now how you progress testing each part of the hardware and software.