Hi,
I'm trying to send a double value from one Arduino Pro Mini to another one and after some search I found this Arduino to Arduino Serial Communication.
After changing the code to, Sender:
//Sender Code
void setup() {
Serial.begin(9600);
}
void loop() {
double value=0.3623;
serialDoubleWrite(value);
}
void serialDoubleWrite(double d) {
byte * b = (byte *) &d;
//Serial.print("d:");
Serial.write(b,4);
/* DEBUG *
Serial.println();
Serial.print(b[0], HEX);
Serial.print(b[1], HEX);
Serial.print(b[2], HEX);
Serial.println(b[3], HEX);
//*/
}
And Receiver:
//Receiver Code
byte array[4];
void setup() {
Serial.begin(9600);
}
void loop() {
int i=0;
if (Serial.available()) {
delay(100);
while(Serial.available() && i<4) {
array[i++] = Serial.read();
/*DEBUG*/
Serial.print((int) i);
Serial.println(array[i-1], HEX);
//*/
}
array[i++]='\0';
}
if(i>0) {
double intbit;
intbit = (array[3] << 24) | ((array[2] & 0xff) << 16) | ((array[1] & 0xff) << 8) | (array[0] & 0xff);
Serial.println(intbit, 4);
}
}
The Sender seemed ok since from the debug I see he's sending 63 7F B9 3E that backwards corresponds to the value 0.3623. But I'm having issues, because the debug in Receiver shows that the second Arduino is getting the array in bad order, randomly difference each time, can someone explain me why and how to fix it? ![]()
And I think the last piece of code in Receiver to make the conversion between the bytes array to double is wrong.