Pages: [1]   Go Down
Author Topic: XBee responding to previous character  (Read 588 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 38
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Code:
#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
Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4095
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

0
Offline Offline
Newbie
*
Karma: 0
Posts: 38
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It's there for you smiley

(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.

Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4095
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

0
Offline Offline
Newbie
*
Karma: 0
Posts: 38
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
.



Code:
#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


Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4095
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Try changing

Code:
  if (XbeeSerial.available() > 1) {

to

Code:
  if (XbeeSerial.available() > 0) {

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

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

0
Offline Offline
Newbie
*
Karma: 0
Posts: 38
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4095
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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-grin
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Pages: [1]   Go Up
Jump to: