Pages: [1] 2   Go Down
Author Topic: Serial.write a float value  (Read 4539 times)
0 Members and 1 Guest are viewing this topic.
Portugal
Offline Offline
Edison Member
*
Karma: 37
Posts: 1545
Pretending you know everything then you will learn nothing.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Brattain Member
*****
Karma: 291
Posts: 25847
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Portugal
Offline Offline
Edison Member
*
Karma: 37
Posts: 1545
Pretending you know everything then you will learn nothing.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Faraday Member
**
Karma: 88
Posts: 3368
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Edison Member
*
Karma: 37
Posts: 1545
Pretending you know everything then you will learn nothing.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
 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
« 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 Offline
Faraday Member
**
Karma: 88
Posts: 3368
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Portugal
Offline Offline
Edison Member
*
Karma: 37
Posts: 1545
Pretending you know everything then you will learn nothing.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I already try that using this code
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 Offline
Edison Member
*
Karma: 78
Posts: 1719
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

-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

smiley-grin


Interestingly, you can achieve the same thing with this:
Code:
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 smiley-grin
« Last Edit: July 02, 2012, 05:55:39 pm by TCWORLD » Logged

~Tom~

Portugal
Offline Offline
Edison Member
*
Karma: 37
Posts: 1545
Pretending you know everything then you will learn nothing.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Edison Member
*
Karma: 78
Posts: 1719
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Type -11.7 into the Decimal box, and press enter smiley-grin
Logged

~Tom~

Offline Offline
God Member
*****
Karma: 17
Posts: 723
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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 Offline
Edison Member
*
Karma: 37
Posts: 1545
Pretending you know everything then you will learn nothing.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

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


Portugal
Offline Offline
Edison Member
*
Karma: 37
Posts: 1545
Pretending you know everything then you will learn nothing.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

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


Offline Offline
Jr. Member
**
Karma: 1
Posts: 73
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Edison Member
*
Karma: 78
Posts: 1719
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You could transmit it the opposite way. E.g. for the example code I gave

Code:
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~

Pages: [1] 2   Go Up
Jump to: