Xbee knowldege wanted

If you know how to work with xbees and how they communicate via Arduino I need your help. I have been messing around with these things for almost a year on and off and have had success only when using xbees without arduino's and have no idea why. If you could leave your email I would very much appreciate your help. Thank you

I've had success using XBees with Arduino, but I don't think using email is a good idea. Working things out in the forum allows everyone to benefit from the discussion.

XBees communicate via the serial ports on Arduinos. It really is that simple (provided the XBees are configured correctly).

What type of XBees do you use?

I use the tiny little RFM-22B's in all my projects (half the size of a postage stamp, $5.60 on EBay). I've even designed a board which integrates a Nano with an RFM-22B, then brings all remaining pins out to one edge, so I can use the board to build up a radio-based project. Just took about ten resistors and two transistors to accommodate the needed level shifting. I will state only that they are very docile, easy to use, and all they ever do is work right. Almost boring.

Now I know nothing about XBee's. They look expensive. But if they communicate with a serial port, then you would definitely run into trouble with the Arduino siblings that use the hardware serial pins D0, D1, unless you establish a software serial connection, or are using a more sophisticated board (like a Mega) with multiple hardware serials.

Thank you for replying, and I have two series 1 xbees with two sparkfun xbee explorers, two sparkfun xbee shields, and two Arduino unos. My problem seems to be that when I use the arduino's serial to send data to another Arduino, it works, however, the other does not receive the data. To be honest I know very little about the programming language and how the serial and xbees work in general.

Mrbobokins:
Thank you for replying, and I have two series 1 xbees with two sparkfun xbee explorers, two sparkfun xbee shields, and two Arduino unos. My problem seems to be that when I use the arduino's serial to send data to another Arduino, it works, however, the other does not receive the data. To be honest I know very little about the programming language and how the serial and xbees work in general.

Series 1 XBees are configured to work "out the box" and are very easy to work with.

Since you we using an Arduino Uno, you only have one serial port. I'm guessing the XBees are talking via the hardwired serial port? I'm also guessing you are verifying that you received data by printing read data via the same serial port?

This would be your problem. The solution would be to use the softserial library to allow the XBees to talk without hijacking the serial port used to talk to your PC.

Also, make sure all of the jumpers on your shield are present and are making the correct connections. Can you share pictures of your boards, shields, and code?

It really is that simple (provided the XBees are configured correctly).

And the XBee is connected to the Arduino properly (via a decent shield is easiest). So, how ARE the XBees connected to the Arduino(s)? What IS the problem?

Series 1 XBees are configured to work "out the box" and are very easy to work with.

But, they work even better when properly configured.

My project is an attempt to build this

I've hooked all of the parts correctly. When I uploaded the sketch to each Arduino, the shield was in DLINE mode and I set it to UART when trying to use it. My problem is that when I turn the potentiometer, the servo only twitches slightly repeatedly. I have the correct potentiometer resistance. Here are the sketches and pictures.

To Arduino with servo

#include <Servo.h>

int servoPin = 9;
Servo Trevor;

void setup()
{
Serial.begin(9600);
Trevor.attach(servoPin);
delay(500);
}

void loop()
{
while(Serial.available() == 0);
int data = Serial.read() - '0';
int pos = map(data, 0, 9, 0, 180);
pos = constrain(pos, 0, 180);
Trevor.write(pos);
Serial.flush();
}

To Arduino with Potentiometer

int potPin = 0;

void setup()
{
Serial.begin(9600);
}

void loop()
{
int val = map(analogRead(potPin), 0, 1023, 0, 9);
Serial.println(val);
delay(50);
}

Pics

  Serial.println(val);

Do you have a clue what this sends?

Try Serial.print(), instead.

Ok thanks and no I just copied the code from the guys video. Also for some reason I can't attach my pictures to the attach file section.

Did you happen to mean Serial.println(); instead of Serial.print() because the later gives me an error.

while(Serial.available() == 0);

^ delete this from your code. :wink: It's a "blocking" function and is not necessary. Use:

while(Serial.available())
{ 
   //all your executable code
}

instead.

  int val = map(analogRead(potPin), 0, 1023, 0, 9);

^ Why are you mapping the pin reading from 0-1023 to 0-9? you should be mapping the pin reading from 0-1023 to 0-180.

^ Also, you should initialize "val" as a byte instead of an int.

pos = constrain(pos, 0, 180);

^ not needed if you made the circuit correctly and connected the correct pins to the correct wires.

Serial.flush();

^ Don't use this function, period. :slight_smile:

Serial.println(val);

^ Unless you are using the serial port to communicate with your PC, ONLY USE Serial.write(). Serial.println() does a lot of auto-formatting if your data that you don't want.

Here is the revised code that should allow your XBees to work properly. I don't know where you got the code you posted, but I would never go back to him/her for more code. Just use these programs:

TRANSMITTER (TX) Arduino Code:

int potPin = 0;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  byte val = map(analogRead(potPin), 0, 1023, 0, 180);
  Serial.write(val);
  delay(50);
}

RECEIVER (RX) Arduino Code:

#include <Servo.h>

int servoPin = 9;
Servo Trevor;

void setup()
{
  Serial.begin(9600);
  Trevor.attach(servoPin);
}

void loop()
{
  while(Serial.available())
  {
    byte data = Serial.read();
    int position = data & 0xFF;
    Trevor.write(position);
  }
}

Try this and see if it works.

Wow! that works perfectly thank you very much Power_Broker. Where did you learn to program this well? just reading books about Arduino or other sources? I would really like to learn how to program Arduino as well as you do. Thank you very much!!

Thank you so much! :smiley:

I learned to code through several electrical and computer engineering classes at Virginia Tech. The Arduino and XBee specific material came from reading up online from different people's projects in addition to researching the built in classes/functions that the Arduino IDE provides. Before you call a function (i.e. Serial.println()), you need to look it up on the Arduino reference website and get familiar with "what's under the hood" so to speak. Also, doing general research on the types of variables and binary/hex representations of data will help a lot.

Good luck and I'm glad the code works!

Thank you so much. Sounds like you've had a lot of training. I will have to start reading up.