Hello, do you know how float number are represented in arduino? I know it uses 4 byte but is fixed or mobile comma? how many bits for exponent and how for the base?
I ask this because I need to send a float value on the Xbee in API mode, this is my code for send
unsigned long d = *(unsigned long *)&myValue;
payload[1] = d & 0x00FF;
payload[2] = (d & 0xFF00) >> 8;
payload[3] = (d & 0xFF0000) >> 16;
payload[4] = (d & 0xFF000000) >> 24;
and this the one for rebuild the float once received 4 bytes:
I always obtain valRilevato=0.0 so I have done some mistakes but without knowing how float are represented it becams difficult for me to debug.
Thanks!
without knowing how float are represented it becams difficult for me to debug.
You are converting the float into 4 bytes. Use the Serial.print() function to print the 4 bytes to the Serial Monitor. Print the value of the float, too.
On the receiving end, print the 4 bytes received. Are they the same?
If not, then there is a storage/transmission problem. If so, then it is a disassemble/reassemble issue. Knowing where the problem lies is half the battle.
Thank you very much I will chek out on IEEE 754. The index starts from 1 because on index 0 i put another information. There are no trasmission problems what I send is the same I recived. For example if my float value is 1.1 value sent (an received) in hex are CD CC 8C 3F if i send 13.26 value sent (an received) are F6 28 54 41 but converting I always obtain 0.0!
How is d declared on the receiving end? What do you get if you print d on the sender, in hex (the unsigned long)? What do you get in d on the receiver, in hex?
The conversion from d to valRilevato looks questionable.
PaulS:
How is d declared on the receiving end? What do you get if you print d on the sender, in hex (the unsigned long)? What do you get in d on the receiver, in hex?
The conversion from d to valRilevato looks questionable.
You are right! I have done some test and all is fine except the conversion from d to valRilevato!
Thanks to AWOL i have understood how float numbers are stored, thanks to PaulS I have found my error: i was copying the MSB for first instead of LSB.
So thank you very much