Hi,
I need to transfer some variables(3bytes of data) over the XBee connected to Fio on one side and XBee connected to Uno on the other side.
I have tried the EasyTransfer libraryhttp://www.billporter.info/easytransfer-arduino-library/.But this has quite long response for my 4WD robot.
So I have written my code to send the variables.It is sending the Start byte(255) the ID byte(to identify which data I am sending) 3 bytes of data(3 variables 0-255) and two bytes of some check if the data was recieved correctly. It is working but only when I send this packet one per +-100ms which is even worse than the EasyTransfer.
What could be the problem?
Hardware:
Arduino Uno
Arduino Fio
XBee S2 with XB24-ZB firmware(Coordinator and end device)
Software:
TX side
int x=0;
int y=5;
int z=10;
int a=0;
int b=5;
int c=10;
int SB = 255;
void setup() {
Serial.begin(9600);
}
void loop() {
//increase the values to simulate variables change
x++;
y++;
z++;
SEND(2,x,y,z); // send variables x,y,z with ID=2
delay(100);
SEND(3,a,b,c); // send variables a,b,c with ID=3
delay(100);
}
void SEND(int ID, int A, int B, int C){
//count the checksum
int CB1 = (A+B+C)/255;
int CB2 = (A+B+C)%255;
Serial.write(SB); // startbyte
Serial.write(ID); // ID byte
Serial.write(A); //data
Serial.write(B); //data
Serial.write(C); //data
Serial.write(CB1); //checksum1
Serial.write(CB2); //checksum2
}
RX side
#include <SoftwareSerial.h>
SoftwareSerial mySerial(5, 6); // RX, TX
int SB;
int ID;
int data1;
int data2;
int data3;
int CB1;
int CB2;
int a;
int b;
int c;
int x;
int y;
int z;
void setup() {
mySerial.begin(9600); // for the XBee
Serial.begin(9600); // for the PC terminal
}
void loop() {
if(mySerial.available()==7){ //check if 7 bytes are available
serialread();
setparameters(ID);
}
}
void serialread(){
//read all bytes
SB = mySerial.read();
ID = mySerial.read();
data1 = mySerial.read();
data2 = mySerial.read();
data3 = mySerial.read();
CB1 = mySerial.read();
CB2 = mySerial.read();
}
boolean check(){
int crc1 = data1+data2+data3; // count the "crc"
int crc2 = (CB1*255)+CB2;
if(crc1==crc2){ // if "crc" is the same return true
return true;
}
else{
return false;
}
}
void setparameters(int ID){
if(check()){ // if "crc" is true set the
if(ID==2){ // recieved data to local variables
x = data1; // for each ID
y = data2;
z = data3;
}
if(ID==3){
a = data1;
b = data2;
c = data3;
}
printout(); // print data to terminal
}
else
RESET(); // if "crc" is false
}
void printout(){
Serial.println("ABC");
Serial.println(a);
Serial.println(b);
Serial.println(c);
Serial.println(" ");
Serial.println("xyz");
Serial.println(x);
Serial.println(y);
Serial.println(z);
Serial.println(" ");
Serial.println(" ");
Serial.println(" ");
}
void RESET(){ //set all local variables to zero
a = b = c = x = y = z = 0;
}
I hope someone will find where is the problem.Thank you very much for every input