Save Name

hey,

so far I use to save my data like this:

    File dataFile = SD.open("datalogx.txt", FILE_WRITE);

But I want to do it like this:

char saveName[ ] = "datalogx.txt";
File dataFile = SD.open(saveName, FILE_WRITE)

Which does not work. Could you help me, to fix that?

Thanks

Great work on posting your code in code tags, but could you please post all of your code and the error you are getting?

Kind regards,

Zeb

"does not work"... how exactly?

thanks for your reply! I thought the mistake is in the posted lines, that’s why I just posted these…

There is no error, the file file just not stored on the SD Card.

The whole function looks like:

void saveToSD(float acc[], int size, int orient)
{
  Serial.println("Saving startet..");
  Serial.print("size of buffer (in bytes):");
  Serial.println(sizeof(acc));
  
    
    // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  
  if (orient == 1) {
    char saveName[ ] = "datalogx.txt";
  }
  else if (orient == 2) {
    char saveName[ ] = "datalogy.txt";
  }
  else if (orient == 3) {
    char saveName[ ] = "datalogz.txt";
  }
    
    File dataFile = SD.open(saveName, FILE_WRITE);
    
    // if the file is available, write to it:
    if (dataFile) {
      for (int i = 1; i <= size; i++) {
        dataFile.println(acc[i]);
      }
      dataFile.close();
    }
    
      //if the file isn't open, pop up an error:
    else {
      Serial.println("error opening datalog.csv");
    }   
    
  Serial.println("Saving ended");
}

and the function call:

      //Save
    int orient = 1;
    saveToSD(axx, size, orient);

The whole function looks like:

That is still not the whole sketch

Incidentally, I assume that you realise that

  Serial.print("size of buffer (in bytes):");
  Serial.println(sizeof(acc));

is not actually printing what its says

You need to read up on scope. The variable saveName in below is only known inside the if; you have another (global) variable saveName declared somewhere and that one never gets updated.

  if (orient == 1) {
    char saveName[ ] = "datalogx.txt";
  }
  ...

When you pass an array to a function, inside the function the size of the array argument is not known; that's why you pass the size as a separate argument to the function. As UKHeliBob states, you will not get what you think you will get with sizeof(someArray).

Doesn’t compile for me. You create three different versions of ‘saveName’ but none of them are in scope when you try to use the value.

Try this instead:

#include <SD.h>


float axx[10];


void saveToSD(float acc[], size_t size, int orient)
{
  Serial.println("Saving startet..");
  Serial.print("size of buffer (in bytes):");
  Serial.println(size);
  const char * saveName;


  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.


  if (orient == 1)
  {
    saveName = "datalogx.txt";
  }
  else if (orient == 2)
  {
    saveName = "datalogy.txt";
  }
  else if (orient == 3)
  {
    saveName = "datalogz.txt";
  }


  File dataFile = SD.open(saveName, FILE_WRITE);


  // if the file is available, write to it:
  if (dataFile)
  {
    for (size_t i = 0; i < size; i++)
    {
      dataFile.println(acc[i]);
    }
    dataFile.close();
  }


  //if the file isn't open, pop up an error:
  else
  {
    Serial.println("error opening datalog.csv");
  }


  Serial.println("Saving ended");
}


void setup()
{
  //Save
  int orient = 1;
  saveToSD(axx, sizeof axx / sizeof axx[0], orient);
}


void loop() {}

johnwasser:
Try this instead: ...

Super thanks, that was helpful!