Go Down

Topic: PROBLEM: Dynamic filename creation with SD card (Read 2017 times) previous topic - next topic


Hi all,

I have been spending a lot time browsing this forum while working on my school project, it has been very helpful thus far and thank you all for that!! I have been unable to find a solution to my current problem though.

I want to create a new file every month for logging data using an RTC. I am aware that the SD.open function only accepts character arrays, the following code is a result of my research to solve the problem, however, it still doesn't work. The only thing I can get to work is when I physically type in a desired file name with quotes i.e. ... SD.open("FILE NAME.TXT" , FILE_WRIGHT); I don't understand why SD.open won't accept a char variable as an argument, or in this instance at least.

Here is the function I have written to try and accomplish this, any help is greatly appreciated!!!

Code: [Select]

void create_file()
  DateTime now = rtc.now();
  t_year = now.year();
  t_month = now.month();
  if(t_month != time_month_past) file_flag = 0;

  if(file_flag == 0)
  file_flag = 1;   
  String temp;
  temp = String("Data-");
  temp += String(t_month);
  temp += String("-");
  temp += String(t_year);
  temp += String(".txt");
  char filename[temp.length()+1];
  temp.toCharArray(filename, sizeof(filename));
  // Open/create the log file
  dataFile = SD.open(filename, FILE_WRITE);
  if (! dataFile)
    // Wait forever, cant write data
    while (1) ;
  time_month_past = t_month;

Arduino Leonardo
Adafruit Assembled Data Logger (http://www.adafruit.com/products/1141#Description)


The filename on an SD card can only be 8.3 format. Even if t_year is 2 chars you are generating a 10.3 name.
I'd also suggest that you put the year before the month in the filename so that a directory listing will have the files in the proper order.



Use sprintf() to populate a char array instead of wasting resources with the String class.


I found a solution !! just to let anyone know out there who was taking a look at this for me !

Basically my file name was simply too long due to the fact my SD card is FAT16 which only supports 8.3 filename format below is a link to the forum where I found this information and a little wiki link to 8.3 file format.





Hey Pete,

When I replied I hit the "My Latest Posts" button (first time poster) and didn't see your reply at the time, thanks again for getting back to me so quickly, much appreciated.


Go Up