Hey all.
I’ve been searching but cannot find and maybe someone could point me at the right direction: I using 2 XbeeS1 and I’m trying to send an integer from one arduino to a second one. my though was to split the int to 2 bytes but I do not know how to keep the order at which they are put together on the receiving side. Would appreciate any help/pointers.
The Xbee S1 will move 2 bytes anyway you send them.
Now, in AT mode, 99.9% of the time if you send them with no time gap, they will show up at the remote node as 2 bytes in the same radio packet (with no added time gap). however, in rare occasions they may move as 2 different packets.
So you may want to create a small ‘protocol’ with a header, length byte, your data and perhaps even a small CRC. Sending 5 or 6 bytes costs abotu the same as sending 2.
Thanks lynnl - that is what I’m trying to do but something’s not quite working - not sure why… Here are the codes - would appreciate your help:
Send:
void setup()
{ Serial.begin(38400);
} void loop() {
int someValue = random(0, 32767);
outputBytes("P", someValue);
} void outputBytes(char* header, int value){
//translate the value into a header + 2 data bytes
//Works for numbers upto 32767 / -32767
//this way we know that the value is always exactly 6 bytes //makes it much easier to receive on the other end
int MSB;
int LSB;
MSB = value >> 8;
//get just the first byte
LSB = value & 0b0000000011111111;
//get just the second byte
Serial.print(header);
Serial.write(MSB);
Serial.write(LSB);
Serial.write(LSB);
Serial.write(MSB);
Serial.print(header);
}
Receive:
void setup()
{ Serial.begin(38400)
; } void loop() {
while(Serial.available() > 6){
byte header = Serial.read();
//read header byte - 80 == P
if(header == 80){
byte MSB = Serial.read();
byte LSB = Serial.read();
byte LSB1 = Serial.read();
byte MSB1 = Serial.read();
byte foot = Serial.read();
//make sure it fits the pattern
if(MSB == MSB1 && LSB == LSB1 && header == foot){
int positionValue = convertBytes(MSB, LSB);
Serial.print("got a value: ");
Serial.println(positionValue);
}
else
{ Serial.println("that didnt work");
Serial.print("MSB: ");
Serial.println(MSB);
Serial.print("LSB: ");
Serial.println(LSB);
Serial.print("LSB1: ");
Serial.println(LSB1);
Serial.print("MSB1: ");
Serial.println(MSB1);
Serial.print("foot: ");
Serial.println(foot);
Serial.println("/////////////////"); }
}
else
{
Serial.print("HEADER WAS NOT A P");
Serial.print("header: ");
Serial.println(header);
Serial.println("/////////////////");
} } }
int convertBytes(int MSB, int LSB){
//99% sure this is right
return(MSB << 8) | LSB;
//get just the first byte
} void doMotorThing(int positionValue)
{
//do something with the positionValue
}