Would appreciate some help on my Xbee project!

Hi all,

This is my first Xbee project, and I'm having some trouble getting them to communicate :frowning:

All I'm trying to do at the moment is to have the transmitting Arduino send '1111', wait a second, and then send '000', wirelessly through its Xbee. The receiving Arduino then received this data from it's own Xbee, and switches the onboard LED on, if it reads '1111' and off if it reads anything else.

I'm using 2x Arduino UNOs with Xbee shields and 2xSeries 1 Xbee Trace Antenna Modules. When both Arduinos (with Xbees) are connected to power, the Xbee shield associate LED on both shields switches on and off every second, so I'm assuming this means that they are connected to each other.

What is happening is, that although I can see the TX led on the transmitting Arduino switch on every second (meaning it is transmitting data as it's supposed to), the Pin 13 LED on the receiving Arduino doesn't switch on or off every second. Rather it either stays off, or when I move the external 9v power supply around, it just suddenly switches on and off very quickly (and erratically) until it's at rest again. I checked to see if this was a power supply / wiring issue, but it was clear that it wasn't. Perhaps this means that something is wrong with my Xbee module? Or maybe there is way too much interference around somehow? I'm stumped :frowning:

For reference, here's my transmitting code:


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

void loop(){
  
Serial.println(1111);
delay(1000);
Serial.println(000);
delay(1000);  

}

And here's my receiving code:


int ledPin = 13;

void setup()
{
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
}

void loop()
{
while(Serial.available() == 0);
int data = Serial.read() - '0';

if (data == 1111){
digitalWrite(13,HIGH);
}
else
digitalWrite(13,LOW);

}

If anyone has had any experience with Xbee modules, I would love to hear from you!

Thanks for reading!

You're not reading the incoming data in such way that you can ever get 1111. Try using 1 and 0 to get the thing working.

Hey wildbill, yes I did this, now the transmitting arduino only sends 1s and 0s, but the receiving arduino is still not picking anything up :frowning:

Other changes I've made:
I've changed the led pin to be an external LED. Not that this should make any difference whatsoever :
I've also tried out both arduinos running simple code just to check if they're both functional, and they are. I've also tried both arduinos with the external power supply to see if that's functioning properly, and it is.

I guess the only thing left is that perhaps my xbees are defective?

I guess the only thing left is that perhaps my xbees are defective?

Perhaps. I have a vague recollection that when I had some series one XBees that I had to do some configuration with X-CTU to get them to talk to each other. I think the later ones just work out of the box.

Or it's your code - post your revised versions.

Here's my revised code. It hasn't changed much:

Transmitting code:

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

void loop(){
  
Serial.println(1);
delay(1000);
Serial.println(0);
delay(1000);  

}

Receiving code:

int ledPin = 8;

void setup()
{
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
}

void loop()
{
while(Serial.available() == 0);
int data = Serial.read() - '0';

if (data == 1){
digitalWrite(ledPin,HIGH);
}
else
digitalWrite(ledPin,LOW);
Serial.flush();
}

When I attach the receiving Arduino over USB to my laptop and use the Serial Monitor to send a 1 to the serial port, it switches the LED on, so the problem is likely to be with the Xbees.

Did you mean Serial.print(‘1‘);
See the single quotes?

You are not taking into account the carriage return and line feed that Serial.println() sends. You need to exclude them from your variable input. This demo shows one way of doing this. It also depends on the line feed character being present to work correctly.

/*
  read incoming serial with newline as ending delimiter
  convert ascii numeric to integer and test

*/

unsigned long intVal = 0;

char inChar;

int ledPin = 13;

void 
setup()
{
  
   Serial.begin ( 9600 );
   pinMode(ledPin, OUTPUT);
   
} // setup


void 
loop ()
{
  
  while ( Serial.available () > 0 ) // note lack of semicoln !!
  {
    inChar = Serial.read ();  

    switch ( inChar )
    {
      
      case '\n':   // end of input, use intVal for something
        
          if (intVal == 1)
          {
            digitalWrite(ledPin,HIGH);
          }
          else
          {
            digitalWrite(ledPin,LOW);
          }
        
        // reset buffer
        intVal    = 0;
        
      break;
        
      case '0' ... '9': // capture only the numeric ascii input
      
        intVal = ( intVal * 10 ) + ( inChar - '0' ); // ascii -> decimal
        
      break; 
       
    }  // switch
      
  }  // while 
  
} // loop

Hi liudr,

I don't think that that is the problem, but I tried it anyway, and it didn't work either :frowning:
I'm following the general approach of the tutorial below, my project is very similar except instead of sending values from 0 to 9, I'm just sending either a 1 or a 0. Notice how the val parameter in the tutorial isn't in quotation marks:

Thanks for your reply!

Have you gotten the arduinos to communicate just using wires instead of the xbees? If not, then you might want to work out your tx and rx code using a wired connection first, then move to the xbees.

data = Serial.read() - '0';

The above line in your code assumes you are sending ASCII characters, not a plain zero. If you don't understand what this line is doing with the -'0'; it's time to hit the book.

Problem solved!

Turned out a certain tiny switch on my Xbee shield had to be in the 'Run' mode and not 'Prog' mode.

But now I have another problem :frowning: My receiving Arduino only receives the signal on the third time it's transmitted. So, say, I'm transmitting a signal with 2 seconds between each one. The receiving Arduino only acts on it on the third time its received.

Any thoughts on why this is occurring?

Thanks for all your help!

You can first do a simple test of your 3rd theory. Hope you can use software serial for the xBee and hardware serial for debug info printout to PC. Assume you use software serial on xbee and call it xBeeSerial, have the receiver print out the received characters like

if (xBeeSerial.available()) Serial.write(xBeeSerial.read());

Just to make sure that when you send 3 times, you receive 3 times. If it seems to work, post your latest code for some critics.