Hello, I'm new to Arduino and I have a similar problem. I'm sending exactly 15 characters to the Arduino via XBee pro. I think the XBee pro is setup correctly because if I send exactly 15 characters at one time, the Arduino can read it perfectly. However if I start sending it multiples times, I get erratic data. The format of my data being sent is as follows "xxx:yyy:nnn:sss" I tried to implement a checksum to only get the right data where sss = xxx+yyy+nnn, but this only works 95-97% of the time which is not good enough. I think by coincident, sometimes sss = xxx+yyy+nnn. This 3-5% error mess up my entire project. I'm using a language called Autoit to communicate to the Arduino. Any help is appreciated.
#include <SoftwareSerial.h>
#include <SabertoothSimplified.h>
#define SABER_TX 13
#define XBEE_RX 2
#define XBEE_TX 3
#define PARAMETERS 4
SoftwareSerial SWSerial(NOT_A_PIN, SABER_TX);
SoftwareSerial XBee(XBEE_RX, XBEE_TX);
SabertoothSimplified ST(SWSerial);
// Parse command from XBee depending on how many parameters.
// Each parameter must be 3 characters in length. Delimeter of ':' must be used between 2 or more parameters.
// Single or double digit must be padded with zeroes. ie: "003:030:033"
// If 2 parameters format is "xxx:yyy". 3 parameters format is "xxx:yyy:nnn" etc...
// Since we have 4 parameters the INPUT_SIZE is 4*4-1 = 15 "xxx:yyy:nnn:sss"
// Hence we read in 15 characters at a time from XBee Serial.
void GetCommand() {
int INPUT_SIZE=PARAMETERS*4-1, i=0, val[PARAMETERS];
int checksum=0;
char input[INPUT_SIZE + 1];
byte size = XBee.readBytes(input,INPUT_SIZE);
input[size]=NULL;
char* command = strtok(input, ":"); // Split string with ':' delimeter and store to array.
while (command != NULL) {
val[i]=atoi(command);
i++;
command = strtok (NULL, ":");
}
for (int i=0;i<PARAMETERS-1;i++){ //Calculate checksum to make sure data is correct.
checksum+=val[i];
}
if (checksum==val[PARAMETERS-1]){
timeOfLastGoodPacket = currentTime;
ST.drive(val[1]-127); //Since the motor driver expects range from -127(reverse) to +127(forward)
ST.turn(val[0]-127); //We must subtract -127 because the value received range from 0 to 254.
delay(10);
if (val[2]) {XBee.write("200");} //Send back to get next data stream
Serial.print('\n');
Serial.print(val[0]-127); Serial.print(',');
Serial.print(val[1]-127); Serial.print(',');
Serial.print(val[2]); Serial.print(',');
Serial.print(val[3]-254);
}
}