I'd like to write a new file to the SD card. every day
The file name is derived from the real time clock, to like so YYYYMMDD.CSV
However, while the file name prints on the serial monitor, the file is not created on the SD card and responds with "error opening". When I hard code the file name it works.
What I am I missing?
Any hint appreciated.
#include <Wire.h>
#include <DS3231.h> // RTC library
#include <SPI.h>
#include <SD.h>
#define PIN_SD_CHIP_SELECT 4
const unsigned int BAUD_RATE_HW = 9600; // hardware serial baud rate
const unsigned long MEASUREMENT_INTERVAL = 10000; // [ms] time interval for taking measurements
DS3231 clock; // RTC
RTCDateTime dt; // instantiate RTC
File dataFile; // file to write to
void logData() {
char fileName[13];
strcat(fileName, clock.dateFormat("Ymd", dt));
strcat(fileName, ".CSV");
Serial.print("fileName: ");
Serial.println(fileName);
dataFile = SD.open(fileName, FILE_WRITE);
if (dataFile) {
Serial.print(F("-> Writing data to log file: "));
Serial.println(fileName);
dataFile.println("recordOut");
dataFile.close();
} else {
Serial.println(F("-> Error opening "));
}
}
void setup() {
pinMode(PIN_SD_CHIP_SELECT, OUTPUT);
pinMode(SS, OUTPUT); // Docu says to do this even if not used
Serial.begin(BAUD_RATE_HW); // start hardware serial port with 9600 bps
delay(500);
Wire.begin();
Serial.println(F("-> Clock initialised ..."));
clock.begin(); // Initialise clock object
Serial.print(F("-> Initialising SD card ... "));
if (!SD.begin(PIN_SD_CHIP_SELECT)) {
Serial.println(F("initialisation failed!"));
return;
}
Serial.println(F("initialisation done."));
}
void loop() {
static unsigned long lastTimeMeassured = 0; // last time measured value
unsigned long timeStamp = millis(); // memorise current time
if (timeStamp - lastTimeMeassured > MEASUREMENT_INTERVAL) {
lastTimeMeassured = timeStamp; // present time
dt = clock.getDateTime(); // get RTC datum
logData();
}
}