Hello, I am working on a binary communication between arduino.
and I have faced some problem, I can't retrieve the correct data by using left shift function.
The following code is what I do for sending and receiving binary data, to make troubleshoot easy, i made it in one program.
First, I break the data to 4 byte from first arduino
Second, I will collect back the data using second arduino and combine the 4bytes back into one value.
I am totally have no idea why is it not giving me back my value, please let me know if you can solve it, thanks!
long val = 12980992;
byte buffer[4];
long retrieve;
void setup(){
Serial.begin(38400);
}
void loop(){
/*to send out from first arduino*/
buffer[0] = val&0xff;
buffer[1] = (val >> 8)&0xff;
buffer[2] = (val >> 16)&0xff;
buffer[3] = (val >> 24)&0xff;
long num;
int start = 0;
/*to retrieve by second arduino*/
num = buffer[start];
num = (buffer[start + 1] <<8) | num;
num = (buffer[start + 2] <<16) | num;
num = (buffer[start + 3] <<24) | num;
Serial.println(num);
delay(500);
}
Best,
Jack