Hi guys,
Maybe someone needs to save files to SD card with time stamp for a data logging. Using this topic I modified the code to compile on IDE 1.8.0 (Thank you fat16lib ).
The form for the time stamp is (for example) 17022805.TXT. Year:17, Month: 02, Day:28, Number of file: 05.
#include <SdFat.h>
SdFat sd;
SdFile file;
uint8_t const chipSelect = SS;
void fmt2d(char* str, uint8_t v) {
if (v > 99) return;
uint8_t n1 = v / 10;
uint8_t n0 = v - 10 * n1;
*str++ = '0' + n1;
*str = '0' + n0;
}
bool openByDate(SdFile* file, uint8_t y, uint8_t m, uint8_t d) {
char name[] = "YYMMDD00.TXT";
fmt2d(name, y);
fmt2d(name + 2, m);
fmt2d(name + 4, d);
for (uint8_t n = 0; n < 100; n++) {
fmt2d(name + 6, n);
if (file->open(name, O_CREAT | O_EXCL | O_WRITE)) return true;
}
return false;
}
void setup() {
Serial.begin(9600);
while (!Serial) {
SysCall::yield();
}
Serial.println("type any character");
while (Serial.available() == 0) {}
if (!sd.begin(chipSelect)) {
Serial.println("sd.begin failed");
}
if (openByDate(&file, 12, 3, 30)) {
Serial.print("opened: ");
file.printName();
Serial.println();
} else {
Serial.println("open failed");
}
file.close();
Serial.println("File Closed");
}
void loop() {}
Sorry for my English.