I have logged some data in my sdcard/FAT32 (format). I can read and display them on the serial monitor but additionally I want to use these data as float.

Here is my code:

       byte index = 0;

       Tfile = SD.open("DATALOG.TXT",FILE_READ); 

       while (Tfile.available()) {

         x[index++] = Tfile.read();
         x[index] = '\0'; // NULL terminate the array

Printing the data using:


I receive the following float values that are correct:


Now I want to work with values so i need to store them on a double index:

    double dtemp[320];
    for(index = 0, i=0; index < 320; index++, i++) {
       dtemp[i] =  fileContents[index];
       Serial.println(dtemp[i]); }

but i receive fault values:


Any comment is welcomed !

Thank you in advance !

  • The "fault" values are the ASCII code...ok there is a solution reading the valid values(excluding the new line and carriage return) an putting them in an index 5x320, each line will be whole number and then I can store it in a float... but is there a faster way?
    – G.V.
    Mar 2 '15 at 6:40

You have to parse the character array in order to convert it to floats. There is no fast way to do this: since the Arduino has no FPU, anything involving floats is slow.

Unless you really have to, I would not recommend slurping the whole file into RAM. You will save memory by parsing the file on the fly. This should be as simple as calling the parseFloat() method of the File object:

double dtemp[320];
int i;
for (i = 0; Tfile.avalable() && i < 320; i++) {
    dtemp[i] = Tfile.parseFloat();
if (i < 320)
    Serial.println("WARNING: Premature end of file.");
  • Be careful with i declaration, byte is too small tp hold a value as big as 320.
    – jfpoilpret
    Mar 2 '15 at 11:28
  • @jfpoilpret: Oops! My bad. Just fixed it, thanks. Mar 2 '15 at 12:14
  • using double or float, arduino (UNO in my case) stops responding and it doesn't print anything in the serial monitor. Using int, it parses the integer part of the number correctly....
    – G.V.
    Mar 4 '15 at 7:10
  • 1
    This is very weird. The parseInt() and parseFloat() functions are extremely similar, and they both implement timeouts. Neither should hang your Uno. I suspect your error lies somewhere else in your code. Could you please update your question with a minimal – but complete – sketch that reproduces the problem? Both the int version that works and the float version that does not please. Mar 4 '15 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.