I am trying to include the current date, using the "YYYY-mm-dd.txt" format, into the file name of a data logging sketch. I would also like to check to see if an existing file exists for the current date. If so, I want the sketch to append to the existing file, otherwise I want to create the file if it does not already exist.
I've followed a handful of existing examples and have had to pick and choose and modify code to get things working (kinda) with my DS3231 real time clock module. My current version compiles successfully, but is not behaving as expected.
Here's some examples that I'm drawing from:
My example below successfully initializes the SD card, but fails to write the new file each loop, and then prints to the serial monitor that the current day's filename does not exist. When checking the SD card, there isn't any new file being written either. So I suspect there is something wrong with my SD.open() method.
How can I ensure that the sketch actually creates the file with the current day's file name?
This is my first post to the forum and your patience is appreciated.
Thank you!
#include <SD.h>
#include <DS1307RTC.h>
#include <string.h>
#include <SPI.h>
#include <Wire.h>
#include <TimeLib.h>
const int chipSelect = 10; // chip select pin for teensy with SD module
char filename[16] = {0}; // character array variable to store our converted date string for SD.open()
File dataFile; // file object
tmElements_t tm;
void setup()
{
//initialize serial ports
Serial.begin(9600);
Serial.setTimeout(1500); //set the Timeout to 1500ms, longer than the data transmission periodic time of the sensor
Serial1.begin(9600);
Serial1.setTimeout(1500); //set the Timeout to 1500ms, longer than the data transmission periodic time of the sensor
setSyncProvider(RTC.get);
if(timeStatus() != timeSet)
Serial.println("Unable to sync with RTC");
else
Serial.println("RTC has set the system time");
//lines through Serial.println("card initialized") from Teensy Datalogger example
//to initialize the SD card, which is on the Teensy Audio board
//chipSelect should be set to 10
Serial.print("Initializing SD card...");
SPI.setMOSI(11);
SPI.setMISO(12);
SPI.setSCK(13);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
//while (1); // wait until card is recognized...
return;
}
Serial.println("card initialized.");
}
void loop() {
getFileName();
createFileName();
//Serial.println("writing file name");
delay(1000);
}
// creates a new file name with today's date
void getFileName() {
// store the date in this empty string
String date = "";
// check the day each loop
tmElements_t tm;
if (RTC.read(tm)) {
date = String(tmYearToCalendar(tm.Year)) + "-" +
String(tm.Month) + "-" +
String(tm.Day) + ".txt";
date.toCharArray(filename, 16);
Serial.println("filename: " + String(filename));
//dataFile = SD.open(str, FILE_WRITE);
}
}
// creates a new file name each day if it doesn't already exist
void createFileName() {
//Check if file exists?
if (SD.exists(filename)) {
Serial.println("exists");
Serial.println("appending to existing file");
}
else {
Serial.println("doesn't exist");
Serial.println("Creating new file.");
Serial.println(filename);
dataFile = SD.open(filename, FILE_WRITE);
dataFile.close();
}
}
// adds leading zeros to single digit month and day characters
String as2digits(int number) {
String answer = "";
if (number >= 0 && number < 10) {
answer += ('0');
}
answer += String(number);
return(answer);
}
Here's what my serial monitor output looks like:
RTC has set the system time
Initializing SD card...card initialized.
filename: 2018-4-25.txt
doesn't exist
Creating new file.
2018-4-25.txt
filename: 2018-4-25.txt
doesn't exist
Creating new file.
2018-4-25.txt