XBee responding to previous character

Hi,

This is kind of a follow on to a previous thread, but with different hardware and a bit more detail...

This time I'm using an XBee Series 2 Co-ordinator connected to my laptop via a XBee Explorer USB and the corresponding router is connected to a Real Bare Bones Board in turn connected to my laptop via USB.

RO/ Packetisation Timeout is set to 0

The sketch I'm running should echo back the number sent for 1-3 and 'misc' for everything else.

With the code below, I'm finding that when I send a digit the first one doesn't have an effect, and thereafter each digit sent triggers the appropriate response to the previous digit. Here's the output from the X-CTU terminal on the coordinator (red from the router, blue is what sending to the router)

0
.misc
.misc
.10
.0
.0
.21
.1
.32
.2
.2
.2
.4misc
.misc
.0misc
.misc
.misc
.10
.0
.111
.1
.221
.2
.32
.3misc
.misc

#include <SoftwareSerial.h> //includes the software serial library
#include <TinyGPS.h>  // includes the TinyGPS library

SoftwareSerial XbeeSerial(9, 10);

// a byte to receive data:
char inByte = 0;

int Xbeepower = 6;

void setup() 
{
  Serial.begin(115200);  
  XbeeSerial.begin(9600);
  pinMode(Xbeepower, OUTPUT); 
  digitalWrite(Xbeepower, HIGH);
} //end setup


void loop() 
{

  // get any incoming data from xbee:
  if (XbeeSerial.available() > 1) {
    // read a byte
    inByte = XbeeSerial.read();

  }

  if (inByte == '0') {
    Serial.println("0");
    XbeeSerial.println("0");
  }

  else if (inByte == '1') {
    Serial.println("1");
    XbeeSerial.println("1");
  }  

  else if (inByte == '2') {
    Serial.println("2");
    XbeeSerial.println("2");
  }  


  // do nothing if anything else was received 
  else {

    Serial.println("misc");
    XbeeSerial.println("misc");
  }



  delay(2000);

} //end loop

So, sending each digit twice kind of works as a workaround, but it would be good to understand what's causing this and, ideally, just send each digit once. (I'm hoping to build up to sending arrays.)

Am I sending the digits in the wrong format or something like that?

Thanks

I wouldn't think delay(2000) is necessary or desirable. Why is it there? What happens if it's removed?

It's there for you :slight_smile:

(To bring the terminal output down to something manageable that conveyed what's going on - same spirit as stripping surplus stuff out of the code)

Without any delay it zips past too fast to see what's going on, but with a 200ms delay, the same behaviour can clearly be seen.

But isn't it only responding to what it's sent, and the data that's being sent is being typed into the X-CTU terminal?

I moved everything into the
if (XbeeSerial.available() > 1) {
statement and I think it's now doing what you think it's doing - just one response for every key pressed (the original code repeats the output until another key press)

Terminal output looks like this (I hit enter a few times at the start)

misc
.misc
.0misc
.10
.21
.32
.4misc
.5misc
.0misc
.10
.21
.32
.4misc
.

#include <SoftwareSerial.h> //includes the software serial library
#include <TinyGPS.h>  // includes the TinyGPS library

SoftwareSerial XbeeSerial(9, 10);

// a byte to receive data:
char inByte = 0;

int Xbeepower = 6;

void setup() 
{
  Serial.begin(115200);  
  XbeeSerial.begin(9600);
  pinMode(Xbeepower, OUTPUT); 
  digitalWrite(Xbeepower, HIGH);
} //end setup


void loop() 
{

  // get any incoming data from xbee:
  if (XbeeSerial.available() > 1) {
    // read a byte
    inByte = XbeeSerial.read();

  

  if (inByte == '0') {
    Serial.println("0");
    XbeeSerial.println("0");
  }

  else if (inByte == '1') {
    Serial.println("1");
    XbeeSerial.println("1");
  }  

  else if (inByte == '2') {
    Serial.println("2");
    XbeeSerial.println("2");
  }  


  // do nothing if anything else was received 
  else {

    Serial.println("misc");
    XbeeSerial.println("misc");
  }

}

  delay(200);

} //end loop

Try changing

  if (XbeeSerial.available() > 1) {

to

  if (XbeeSerial.available() > 0) {

Probably delay() can be totally removed, with the recent changes.

Brilliant! That seems to have done the trick! Thank you!

nikki:
Brilliant! That seems to have done the trick! Thank you!

LOL, glad to help a bit. It would have been more brilliant had I seen it earlier! One of those obvious things that's easy to miss :smiley: