Go Down

Topic: Creating a file name from GPS date for SD Card (Read 2939 times) previous topic - next topic

hblair

I have a project that requires a large amount of data.  I want to create a new file each day using the parsed GPS date.
the following is a snippet of my code.

  fileName += GPS.day;
  fileName += "_";
  fileName += GPS.month;
  fileName += "_";
  fileName += GPS.year;
  fileName += ".csv" ;
  File dataFile = SD.open(fileName, FILE_WRITE);
 
when I print using Serial.println(fileName); I get   20_1_13.csv

but I get an error using the above code.

datacollector.ino: In function 'void loop()':
datacollector:185: error: no matching function for call to 'SDClass::open(String&, int)'
C:\Users\Herb\Desktop\arduino-1.0.3\libraries\SD/SD.h:74: note: candidates are: File SDClass::open(const char*, uint8_t)

any suggestions?



PaulS

Quote
any suggestions?

Yes. Don't use Strings.

Or, if you insist on shooting yourself in the foot anyway, read the error message. It's telling you that the open() method does not have an overload that takes a String. It has one that takes a const char *. So, look at the documentation for the String class and see how you can get a const (or not) char * from the String instance.

sprintf() would be a far better choice.

marco_c

Declare fileName as an array of characters and then
Code: [Select]

sprintf(fileName, "%02d_%02d_%02d.csv", GPS.day, GPS.month, GPS.year);


Better still would be to use a format like
Code: [Select]

sprintf(fileName, "%02d%02d%02d.csv", GPS.year, GPS.month, GPS.day);

because it sorts the files in ascending order when listed rather than day of the month (not too usefule when you have lots of months).
Arduino libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com

Nick_Pyner

I don't use GPS for it, but the compilation of the name would be the same. I use an If then to change it on the first loop after midnight.

Code: [Select]


#include <SD.h>
#include "RTClib.h"
#include <Wire.h>
#include <string.h>

RTC_DS1307 RTC;
char filename[] = "00000000.CSV";
File myFile;

void setup()
{
Serial.begin(9600);
Wire.begin(); //Important for RTClib.h
RTC.begin();

if (! RTC.isrunning()) {

Serial.println("RTC is NOT running!");
return;
}

Serial.print("Initializing SD card...");

pinMode(10, OUTPUT);

if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
}

void loop()
{

getFileName();
createFileName();
delay(3000);
}

void getFileName(){

DateTime now = RTC.now();

filename[0] = (now.year()/1000)%10 + '0'; //To get 1st digit from year()
filename[1] = (now.year()/100)%10 + '0'; //To get 2nd digit from year()
filename[2] = (now.year()/10)%10 + '0'; //To get 3rd digit from year()
filename[3] = now.year()%10 + '0'; //To get 4th digit from year()
filename[4] = now.month()/10 + '0'; //To get 1st digit from month()
filename[5] = now.month()%10 + '0'; //To get 2nd digit from month()
filename[6] = now.day()/10 + '0'; //To get 1st digit from day()
filename[7] = now.day()%10 + '0'; //To get 2nd digit from day()
//filename[8] = filename + ".csv";
}

void createFileName(){
myFile = SD.open(filename, FILE_WRITE);
myFile.close();
}

xKoldFuzionx

I was just running into this problem too.  I got that it was the string that was throwing it all off, but without your help, I wouldn't have known about sprintf().  Thanks!  I'm gonna look that one up right now as well as the const char one for string.

Go Up