2

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:

   Serial.print(fileContents);

I receive the following float values that are correct:

   .63
   15.63
   16.60
   16.60
   16.60
   16.60
   etc

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:

      46.00
      54.00
      51.00
      13.00
      10.00
      49.00
      53.00
      etc

Any comment is welcomed !

Thank you in advance !

Paolo Zanchi
  • 951
  • 8
  • 22
G.V.
  • 137
  • 1
  • 4

1 Answers1

2

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();
    Serial.println(dtemp[i]);
}
if (i < 320)
    Serial.println("WARNING: Premature end of file.");
Edgar Bonet
  • 45,094
  • 4
  • 42
  • 81