Let's Make Robots!

Serial Comunication with Arduino and XBee

wireless comunication

Recently I got two XBee modules. And after some struggle with serial comunication I finally understand how to share data between two Arduinos.

As a start I used 2 potenciometers to control the intensity of two LEDs and it works pretty well.  :]

I noticed some lack of information about serial and wireless comunication between Arduinos, so, here´s my code, I hope you find it usefull  :)


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

// SENDER

int analogValue2, analogValue5, val2, val5;

void setup()
{
// start serial port at 19200 bps
Serial.begin(19200);
}

void loop()
{
// read analog input
analogValue2 = analogRead(2);
analogValue5 = analogRead(5);

val2 = map(analogValue2, 0, 1023, 253, 0);  // 254 and 255 for SYNC
val5 = map(analogValue5, 0, 1023, 253, 0);

Serial.print(254, BYTE); //SYNC char
Serial.print(val2, BYTE);

Serial.print(255, BYTE); //SYNC char
Serial.print(val5, BYTE);

delay(150);
}

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

// RECIEVER

byte incomingByte, sensor1, sensor2;

void setup() {

// start serial port at 19200 bps
Serial.begin(19200);
Serial.println(”Ready!”);

pinMode (5, OUTPUT);
pinMode (6, OUTPUT);

delay(1000);

}

void loop() {

if (Serial.available()) {
incomingByte = Serial.read();

Serial.print(int(incomingByte));

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

if ((int(incomingByte) == 255)) {
sensor2 = Serial.read();
Serial.print(”  Sensor 2 = “);
Serial.print(int(sensor2));
}
}

analogWrite (5, sensor1);
analogWrite (6, sensor2);
}

 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
i´ve made un update on my blog regarding serial comunication WITH wires
http://lab.guilhermemartins.net/?p=346

The XBee module I am using is a XBee PRO 60mW Wire Antenna (802.15.4) [INM-0035]
and the breadboard adapter is the Ladyada XBee Adapter Kit, very easy to assemble and to use.

 I find the remote control method very usefull to test one robot first movements. And instead of programming a motion sequence, you can test its motion capabilities in real time.

great, they're the same component i ordered.. looking forward to test them! 

The code looks very easy.. Did you used only two pins? (rx and tx)?

yep, I used only rx and tx pins.

great :) it really looks like they're some very easy components to use.. It explains why they're so succesful :) 
they are very easy to use and full of capabilites.. like you can connect basic sensors directly to the XBee and send their values to another specific XBee.. you can have network meshes and even more complex network systems  :)

Good building block, tip for XBee use. I've only had a chance to use them communicating between a PC and a microcontroller, had wanted to try micro to micro sometime.

Which breadboard adapter did you use? LadyAda?  Those XBeePros you have can talk up to a mile, line of sight.

http://www.digi.com/products/wireless/point-multipoint/xbee-series1-module.jsp

 

what type of zigbee is that