Portugal
Offline
God Member
Karma: 9
Posts: 758
Tomorrow I will know a BIT more than yesterday
|
 |
« on: July 02, 2012, 11:38:18 am » |
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
|
|
|
|
|
Logged
|
Debian,Mint,Ubuntu Arduino Mega 2560 Arduino Nano Arduino Duemilanove MAC OS Montain Lion Raspberry PI Model B
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #1 on: July 02, 2012, 11:45:17 am » |
Why not just send all four bytes of the float?
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Portugal
Offline
God Member
Karma: 9
Posts: 758
Tomorrow I will know a BIT more than yesterday
|
 |
« Reply #2 on: July 02, 2012, 11:47:20 am » |
Can I do it using Serial.write(floatVar,4) ?
|
|
|
|
|
Logged
|
Debian,Mint,Ubuntu Arduino Mega 2560 Arduino Nano Arduino Duemilanove MAC OS Montain Lion Raspberry PI Model B
|
|
|
|
California
Offline
Edison Member
Karma: 41
Posts: 1872
|
 |
« Reply #3 on: July 02, 2012, 12:26:34 pm » |
Can I do it using Serial.write(floatVar,4) ?
The second argument is for passing an array; you're not passing it an array.
|
|
|
|
|
Logged
|
|
|
|
|
Portugal
Offline
God Member
Karma: 9
Posts: 758
Tomorrow I will know a BIT more than yesterday
|
 |
« Reply #4 on: July 02, 2012, 04:46:07 pm » |
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 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? 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
|
|
|
|
« Last Edit: July 02, 2012, 04:51:15 pm by HugoPT »
|
Logged
|
Debian,Mint,Ubuntu Arduino Mega 2560 Arduino Nano Arduino Duemilanove MAC OS Montain Lion Raspberry PI Model B
|
|
|
|
California
Offline
Edison Member
Karma: 41
Posts: 1872
|
 |
« Reply #5 on: July 02, 2012, 05:34:57 pm » |
Create a byte pointer and assign the address of the float to it. Then the Serial.write(myBytePointer, 4) should work.
|
|
|
|
|
Logged
|
|
|
|
|
Portugal
Offline
God Member
Karma: 9
Posts: 758
Tomorrow I will know a BIT more than yesterday
|
 |
« Reply #6 on: July 02, 2012, 05:43:57 pm » |
I already try that using this code 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
|
|
|
|
|
Logged
|
Debian,Mint,Ubuntu Arduino Mega 2560 Arduino Nano Arduino Duemilanove MAC OS Montain Lion Raspberry PI Model B
|
|
|
|
Leeds, UK
Offline
God Member
Karma: 35
Posts: 986
Once the magic blue smoke is released, it won't go back in!
|
 |
« Reply #7 on: July 02, 2012, 05:48:58 pm » |
-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  Interestingly, you can achieve the same thing with this: 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 
|
|
|
|
« Last Edit: July 02, 2012, 05:55:39 pm by TCWORLD »
|
Logged
|
~Tom~
|
|
|
|
Portugal
Offline
God Member
Karma: 9
Posts: 758
Tomorrow I will know a BIT more than yesterday
|
 |
« Reply #8 on: July 02, 2012, 06:07:04 pm » |
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.
|
|
|
|
|
Logged
|
Debian,Mint,Ubuntu Arduino Mega 2560 Arduino Nano Arduino Duemilanove MAC OS Montain Lion Raspberry PI Model B
|
|
|
|
Leeds, UK
Offline
God Member
Karma: 35
Posts: 986
Once the magic blue smoke is released, it won't go back in!
|
 |
« Reply #9 on: July 02, 2012, 06:08:21 pm » |
|
|
|
|
|
Logged
|
~Tom~
|
|
|
|
Offline
Sr. Member
Karma: 6
Posts: 399
|
 |
« Reply #10 on: July 02, 2012, 06:14:27 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Portugal
Offline
God Member
Karma: 9
Posts: 758
Tomorrow I will know a BIT more than yesterday
|
 |
« Reply #11 on: July 02, 2012, 06:14:55 pm » |
Ok I got it.That strange I was tryning that on windows calc but I get the result −1011,10110011  Perhaps windows have a special calculator:D 
|
|
|
|
|
Logged
|
Debian,Mint,Ubuntu Arduino Mega 2560 Arduino Nano Arduino Duemilanove MAC OS Montain Lion Raspberry PI Model B
|
|
|
|
Portugal
Offline
God Member
Karma: 9
Posts: 758
Tomorrow I will know a BIT more than yesterday
|
 |
« Reply #12 on: July 02, 2012, 06:19:22 pm » |
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.
|
|
|
|
|
Logged
|
Debian,Mint,Ubuntu Arduino Mega 2560 Arduino Nano Arduino Duemilanove MAC OS Montain Lion Raspberry PI Model B
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 60
|
 |
« Reply #13 on: July 02, 2012, 06:25:01 pm » |
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...)
|
|
|
|
|
Logged
|
|
|
|
|
Leeds, UK
Offline
God Member
Karma: 35
Posts: 986
Once the magic blue smoke is released, it won't go back in!
|
 |
« Reply #14 on: July 02, 2012, 06:25:41 pm » |
You could transmit it the opposite way. E.g. for the example code I gave 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.
|
|
|
|
« Last Edit: July 02, 2012, 06:28:47 pm by TCWORLD »
|
Logged
|
~Tom~
|
|
|
|
|