I build a logprogram which write data to a file. Each day a new file is created. But after 7 files it stops creating files and also the last file is empty. First I thought, maybe I need to close the old file yet, but addind this does not seem to make any difference. Does anyone know what the problem might be.
Underneath the part of the code code which creates the files.
void getFileName(){
//Filename op basis van datum
if (! SD.exists(filename)) {
logfile.close();
}
DateTime now = RTC.now();
filename[0] = now.day()/10 + '0'; //To get 1st digit from day()
filename[1] = now.day()%10 + '0'; //To get 2nd digit from day()
filename[2] = now.month()/10 + '0'; //To get 1st digit from month()
filename[3] = now.month()%10 + '0'; //To get 2nd digit from month()
filename[4] = (now.year()/10)%10 + '0'; //To get 3rd digit from year()
filename[5] = now.year()%10 + '0'; //To get 4th digit from year()
Day_old = now.day();
Serial.println (filename);
for (uint8_t i = 0; i < 100; i++) {
filename[6] = i / 10 + '0';
filename[7] = i % 10 + '0';
if (! SD.exists(filename)) {
// only open a new file if it doesn't exist
SdFile::dateTimeCallback(dateTime);
logfile = SD.open(filename, FILE_WRITE);
break; // leave the loop!
}
}
if (! logfile) {
error("couldnt create file");
}
Serial.print("Logging to: ");
Serial.println(filename);
}
// prints the data to logfile and echoos to Serial is ECHO_TO_SERIAL is set
void logData(DateTime now, uint32_t m) {
While it may not be a part of the immediate problem, closing the file in the flow of the programmes unnecessary, bad practice, and is pretty certain to cause you grief eventually.
Pardon my crypticism. As is evident by the way it is written
GetClock();
`"GetClock" is simply a subroutine that returns the variable "day" by whatever means, the most common of which is by use of an RTC - as you are probably already doing.
That variable has two uses:
as a flag that it is midnight and time to change the filename