I'm using the Adafruit data logging shield to write log files to an SD card. Like the example code in the documentation, I want a new log file to be created every time the Arduino powers on. Originally I used a modified form of the example:
// create a new file
char filename[] = "LOGGER00.CSV";
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
logfile = SD.open(filename, FILE_WRITE);
break; // leave the loop!
}
}
However, my application needs to be able to support a thousand files instead of a hundred, so even with optimization, this loop was too slow. Instead, I am using a master file to keep track of which log file number I am on to make log file creation easy and fast.
I put the code in the loop function to test it. I should be able to start it and let it create every file from TEST000 to TEST999, but random file creation errors occur every time I run it. I tried including the loop method as a backup to run if the master file failed, but even with this backup, file creation often fails.
My full code is attached; it was a little too big for the post
Last time I ran the code, this was the readout:
Debug: filepath = TESTLOGS/TEST054.TXT
Opening file
Debug: logNumber = 055
Debug: logNumber[0] = 0
Debug: logNumber[1] = 5
Debug: logNumber[2] = 5
File creation successful
Debug: filepath = TESTLOGS/TEST055.TXT
Opening file
Debug: logNumber = 056
Debug: logNumber[0] = 0
Debug: logNumber[1] = 5
Debug: logNumber[2] = 6
File creation successful
Debug: filepath = TESTLOGS/TEST056.TXT
Opening file
Error: file creation failed, retrying...
Debug: filepath = TESTLOGS/TESTÿÿÿ.TXT
Error: file creation failed again, attempting recovery cycle
Attempting to recover
Main loop: 1
Secondary loop 1: 1
Secondary loop 1: 2
Secondary loop 1: 3
Secondary loop 1: 4
Secondary loop 1: 5
Secondary loop 1: 6
Tertiary loop 1a: 1
Tertiary loop 1a: 2
Tertiary loop 1a: 3
Tertiary loop 1a: 4
Tertiary loop 1a: 5
Tertiary loop 1a: 6
Tertiary loop 1a: 7
Attempting to create file: TESTLOGS/TEST057.TXT
Hello
Break 4
Error: File not created, trying again...
File creation failed, program terminated
File creation failed
It successfully created files 000-055, then failed on 056. It moved on to my recovery loop, which tried to create 057, but that also failed, so the program terminated. I have also had programs that randomly stop after a call to logFile = SD.open(filepath, FILE_WRITE). Another very common error is a read error from the master file, which did happen in the readout above. After failing to create file 056, it attempted to reread the number from the mater file. When it did so, some error occurred, because instead of getting "056" again, it ended up with "ÿÿÿ". I checked the master file on my computer, and it was not corrupted; it still contains the string "056". Also, both files 056 and 057 were created. It seems that the problem may be with assigning the file to the variable logFile.
I can't find anything in my code that would cause problems like this. It seems that they must be bugs, but if anyone has any ideas, I would greatly appreciate it.
Thanks!
SD_Library_Test.ino (9.59 KB)