Save to SD Card

Hey,

I'm using this self-built function to save a float array to SD card:

void saveToSD(float ax[], float ay[],float az[], int size)
{
  Serial.println("Saving startet..");
    
    // open the file
  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    for (int i = 1; i <= size; i++) {
      dataFile.println(ax[i]);
      //Serial.println(ax[i]);
    }
    dataFile.close();
  }
  
  Serial.println("Saving ended");
}

Is there a better/faster way to do so? (Saving the whole array at once)

Thanks!

Write supports dataFile.write(buf, len);buf would be your array name and the number of bytes to write would be the size of a float x the number of elements. That would write in binary not in ascii though

Careful with your indexes, an array starts at 0 not at 1

Great Thanks!

You know about any possibility to save in a ascii instead of binary?

Have you read the SD library documentation?

ottooo:
You know about any possibility to save in a ascii instead of binary?

As @aarg suggests, double check the documentation or the class interface file when in doubt.

If you want to print each number on a separate line, in ascii then you have to iterate over the array as you did (well with the correct index range). It’s not super slow as you might think as the text actually goes into a memory buffer that gets written in one big blob once full (or when you call flush or close the file).

OK. Thanks, you helped me!