Pages: [1]   Go Down
Author Topic: PROBLEM: Dynamic filename creation with SD card  (Read 1053 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.reserve(25);
  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;
}

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

Offline Offline
Edison Member
*
Karma: 43
Posts: 1556
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Pete
Logged

Where are the Nick Gammons of yesteryear?

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

http://forum.arduino.cc/index.php/topic,148888.0.html

http://en.wikipedia.org/wiki/8.3_filename
Logged

Offline Offline
Edison Member
*
Karma: 43
Posts: 1556
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That's exactly what I said yesterday.

Pete
Logged

Where are the Nick Gammons of yesteryear?

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Andrew
Logged

Pages: [1]   Go Up
Jump to: