Pages: [1]   Go Down
Author Topic: Floats stored in an array? Having troubles  (Read 327 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Sr. Member
****
Karma: 1
Posts: 252
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

When I try to store float variables in an array, I get a int values with a few decimal places after them. The code will work with int variables however, that is not what I need. So where is the number getting truncated?

Code:
#include <TextFinder.h>

TextFinder finder(Serial);
const int NUMBER_OF_FIELDS = 28; // how many comma-separated fields we expect
int fieldIndex = 0; // the current field being received
float values[NUMBER_OF_FIELDS]; // array holding values for all the fields

void setup()
{
Serial.begin(57600); // Initialize serial port to send and receive at 57600 baud
}

void loop()
{
for(fieldIndex = 0; fieldIndex < 28; fieldIndex ++)
{
values[fieldIndex] = finder.getValue(); // get a numeric value
}
Serial.print( fieldIndex);
Serial.println(" fields received:");
for(int i=0; i < fieldIndex; i++)
{
Serial.println(values[i]);
}
fieldIndex = 0; // ready to start over
}

Variables I was using...

Code:
0,0.02,-0.01,9.82,2.0,0,1500,1500,1500,1000,1000,2000,0,0, 1000,1000,1000,1000,0,0,0,0,11.3,0\r\n

Any help would be great! thanks!
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 207
Posts: 12910
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


values[fieldIndex] = finder.getFloat(); // get a numeric value
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 613
Posts: 49310
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I get a int values with a few decimal places after them.
No, you don't. Integer values do not have decimal places.
Logged

Offline Offline
Sr. Member
****
Karma: 1
Posts: 252
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Paul, I guess I was trying to say that somewhere a int was being converted to a float. So the 11.3 >int = 11> float = 11.00.

Thanks Coding Badly. That worked perfectly.
Logged

Offline Offline
Sr. Member
****
Karma: 9
Posts: 296
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Another idea:  Why don't you just store byte/integers, example, "113" in the array, and when it's time to use it in your code, divide by 10/100 (depending on how many decimal places you need).  -- that way you're not storing floats in your array (less memory required too)

Logged

Pages: [1]   Go Up
Jump to: