Go Down

Topic: Floats stored in an array? Having troubles (Read 390 times) previous topic - next topic

RobDrizzle

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: [Select]
#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: [Select]
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!

Coding Badly

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

PaulS

Quote
I get a int values with a few decimal places after them.

No, you don't. Integer values do not have decimal places.

RobDrizzle

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.

vasquo

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)


Go Up