Pages: [1]   Go Down
Author Topic: storing Serial bytes to a float variable  (Read 436 times)
0 Members and 1 Guest are viewing this topic.
Portland, OR
Offline Offline
Sr. Member
****
Karma: 5
Posts: 260
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm reading values from a sensor, coming in as serial data.

The values come in multiple Serial bytes as follows:
For example a sensor reading of 2.16 comes in as the following four bytes: '2', '.', '1', '6'
Fairly simple, and it's been easy to print those 4 bytes out individually just using mySoftSerial.read() for each incoming byte; and then Serial.print(...) the 4 bytes to the terminal.

However, I would like to STORE altogether the four separate incoming Serial bytes of the value "2.16"... specifically, I want to store into one float variable so I can do arithmetic with it.
What would be an effective way to accomplish that?

Seems like a common thing to do, but I haven't come across any simple method.

[sidenote: I tried the approach discussed in a past thread, of using a union that has a float and a byte array, but that didn't solve the problem here because it appears the incoming data also has to have been sent out from a byte array originating from a union, which is not the case here]
« Last Edit: October 15, 2011, 04:12:27 am by giantsfan3 » Logged

United Kingdom
Offline Offline
Tesla Member
***
Karma: 220
Posts: 6587
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

If you store the characters received from the sensor in a character array, and append a null character when you have finished receiving characters, then you can use the atof() function to convert them to a double, and thence to a float.
Logged

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

0
Offline Offline
Shannon Member
****
Karma: 162
Posts: 10496
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

On the Arduino I believe double and float are the same thing, an IEEE 32 bit float.
Logged

[ I won't respond to messages, use the forum please ]

Portland, OR
Offline Offline
Sr. Member
****
Karma: 5
Posts: 260
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That worked successfully. Thanks dc42. Why the null character at the end by the way?

And indeed arduino's implementation of double and of float appear to be identical:
"The double implementation on the Arduino is currently exactly the same as the float, with no gain in precision." [http://arduino.cc/en/Reference/Double]
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24454
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The null makes it a C string, and shows the string routines like atof or atoi where the string ends.
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.

Pages: [1]   Go Up
Jump to: