Go Down

Topic: Serial.write a float value (Read 6160 times) previous topic - next topic

HugoPT

Hello
I have a float variable with a value like -99,9
I think I will need 3 bytes to store this value.I also need to transmit it using a Serial.write
How can I decompose the float and then seen it this way

Debian,Mint,Ubuntu
Arduino Mega 2560
Arduino Nano
Arduino Duemilanove
MAC OS Montain Lion
Raspberry PI Model B

AWOL

Why not just send all four bytes of the float?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

HugoPT

Can I do it using Serial.write(floatVar,4) ?
Debian,Mint,Ubuntu
Arduino Mega 2560
Arduino Nano
Arduino Duemilanove
MAC OS Montain Lion
Raspberry PI Model B

Arrch


Can I do it using Serial.write(floatVar,4) ?


The second argument is for passing an array; you're not passing it an array.

HugoPT

#4
Jul 02, 2012, 11:46 pm Last Edit: Jul 02, 2012, 11:51 pm by HugoPT Reason: 1
Thanks for your help
I already try to just Serial.write(floatVariable) but it just print 00 on Serial Port.

I'm also trying on this way
Code: [Select]

 float temp = -11.7;
 byte * b = (byte *) &temp;
 Serial.write(b[3]);
 Serial.write(b[2]);
 Serial.write(b[1]);
 Serial.write(b[0]);


But with this I still missing something since I don get the right values of the four bytes
About the Serial.write(floatVariable) from what I found on the manual it just work for a single byte and I have a float which are 4 bytes.I belive this is the cause why I get 00 running that right?
Quote

Syntax

Serial.write(val)
Serial.write(str)
Serial.write(buf, len)

Arduino Mega also supports: Serial1, Serial2, Serial3 (in place of Serial)

Parameters

val: a value to send as a single byte

str: a string to send as a series of bytes

buf: an array to send as a series of bytes

len: the length of the buffer
Debian,Mint,Ubuntu
Arduino Mega 2560
Arduino Nano
Arduino Duemilanove
MAC OS Montain Lion
Raspberry PI Model B

Arrch

Create a byte pointer and assign the address of the float to it. Then the Serial.write(myBytePointer, 4) should work.

HugoPT

I already try that using this code
Code: [Select]

float temp = -11.7;  
byte * b = (byte *) &temp;
 
 Serial.write(b,4);

On the output I get 33 33 3B - C1
I don't recognize this values
Debian,Mint,Ubuntu
Arduino Mega 2560
Arduino Nano
Arduino Duemilanove
MAC OS Montain Lion
Raspberry PI Model B

Tom Carpenter

#7
Jul 03, 2012, 12:48 am Last Edit: Jul 03, 2012, 12:55 am by TCWORLD Reason: 1
-11.7 is a float.

When converted to binary it becomes:
11000001001110110011001100110011
This in hex is:
C1 3B 33 33

The float is sent in reverse, so you get:
33 33 3B C1

:D


Interestingly, you can achieve the same thing with this:
Code: [Select]
typedef union {
 float floatingPoint;
 byte binary[4];
} binaryFloat;

void setup() {
 // put your setup code here, to run once:
 binaryFloat hi;
 hi.floatingPoint = -11.7;
 Serial.begin(115200);
 Serial.write(hi.binary,4);
}


Unions a so cool! Basically the 'byte binary[4]' and 'float floatingPoint' are joined and share the same memory space, so you can access either, and if you change one, you change the other. You can have so much fun changing the byte array, and seeing what its floating point equivalent is :D
~Tom~

HugoPT

I will try that then.
Just another question how did you convert the -11.7 to binary?(what tool you had used)
I'm trying to discover that on the Calculator but if I put in decimal -11.7 and convert to binary I don't get this value.
Debian,Mint,Ubuntu
Arduino Mega 2560
Arduino Nano
Arduino Duemilanove
MAC OS Montain Lion
Raspberry PI Model B

Tom Carpenter

http://www.h-schmidt.net/FloatConverter/IEEE754.html

Type -11.7 into the Decimal box, and press enter :D
~Tom~

Delta_G



I'm trying to discover that on the Calculator but if I put in decimal -11.7 and convert to binary I don't get this value.



The float type is encoded.  So many bits represent digits and one for the sign and some bits for the exponent part.  But the binary representation of a floating point variable is never going to be numerically equal to the binary representation of the actual number. 

For example take the number 1.0.  Numerically, the binary equivalent of 1 is 1.  Written as a byte it looks like 00000001.  But as a float, it gets encoded into 00111111100000000000000000000000.

HugoPT

Ok I got it.That strange I was tryning that on windows calc but I get the result ?1011,10110011
:smiley-roll-blue:
Perhaps windows have a special calculator:D :smiley-roll-blue:

Debian,Mint,Ubuntu
Arduino Mega 2560
Arduino Nano
Arduino Duemilanove
MAC OS Montain Lion
Raspberry PI Model B

HugoPT

Quote
For example take the number 1.0.  Numerically, the binary equivalent of 1 is 1.  Written as a byte it looks like 00000001.  But as a float, it gets encoded into 00111111100000000000000000000000.

Sounds trouble for me then.Since after transmit it I need to rearrange it back on the receiver side.
Debian,Mint,Ubuntu
Arduino Mega 2560
Arduino Nano
Arduino Duemilanove
MAC OS Montain Lion
Raspberry PI Model B

AndyCC

For a quick and dirty solution, multiply it by 10000 before you send it, cast it to an int, send the characters as ASCII, and then divide by 10000 on the PC side to get back your original float. (Watch for overflows though...)

Tom Carpenter

#14
Jul 03, 2012, 01:25 am Last Edit: Jul 03, 2012, 01:28 am by TCWORLD Reason: 1
You could transmit it the opposite way. E.g. for the example code I gave

Code: [Select]
for (char i = 3; i >= 0; i++){
 Serial.write(hi.binary[i]);
}


But I think the recieving program, e.g. if it was written in C, would expect the bytes in the reverse order as they were sent in the order they were stored in the memory. You haven't said what you are using to recover it, but if it is C code, you could use the same union typedef in you recievers program, and read the incoming bytes to the binary[4] array, and then you would have it available as a float without any extra math.
~Tom~

Go Up