I am working on a datalogger that uses the UNO and Adafruit datalogger shield and I am having an issue when opening a file to an SD card. I am using the RTC to get the date info in order to name a .csv file. The file name info is saved to an array and prints out fine but as a block, (i.e "YYYMMDD.csv") but when I add dashes to format the file name better ("YYYY-MM-DD.csv") it no longer writes the file to the SD card but the file name prints out no problem so it is being saved to the array. Is this an invalid file name format or am I writing to the array the wrong way? Here is the code I have been using. Thanks in advance for your help.
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 RTC; // define the Real Time Clock object
// for the data logging shield, we use digital pin 10 for the SD cs line
const int chipSelect = 10;
// the logging file
File dataFile;
String dataFilename = "";
char fn[] = "YYYY-MM-DD.CSV";
// Function to call time stamp for naming the log file.
void dateTime(uint16_t* date, uint16_t* time) {
DateTime now = RTC.now();
// return date using FAT_DATE macro to format fields
*date = FAT_DATE(now.year(), now.month(), now.day());
// return time using FAT_TIME macro to format fields
*time = FAT_TIME(now.hour(), now.minute(), now.second());
}
void error(char *str)
{
Serial.print("error: ");
Serial.println(str);
// red LED indicates error
digitalWrite(redLEDpin, HIGH);
while(1);
}
void setup(void)
{
Serial.begin(9600);
Serial.println();
// connect to RTC
Wire.begin();
if (!RTC.begin()) {
dataFile.println("RTC failed");
Serial.println("RTC failed");
}
// initialize the SD card
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
//SdFile::dateTimeCallback(dateTime);
// following line sets the RTC to the date & time this sketch was compiled
// RTC.adjust(DateTime(__DATE__, __TIME__));
DateTime now = RTC.now();
// Formats and adds the year to the file name string
if (now.year() < 10) {
dataFilename = String(dataFilename + '0' + String(now.year(), DEC));
}
else {
dataFilename = String(dataFilename +String(now.year(), DEC));
}
// Add the dash after the year info
dataFilename = String(dataFilename + '-');
// // Formats and adds the month to the file name string
if (now.month() < 10) {
dataFilename = String(dataFilename + '-' + '0' + String(now.month(), DEC));
}
else {
dataFilename = String(dataFilename + '-' + String(now.month(), DEC));
}
// Add second dash to file name string
dataFilename = String(dataFilename + '-');
// Formats and adds the day to the file name string
if (now.day() < 10) {
dataFilename = String(dataFilename + '-' + '0' + String(now.day(), DEC));
}
else {
dataFilename = String(dataFilename + '-' + String(now.day(), DEC));
}
dataFilename = String(dataFilename + ".CSV");
// Serial.println(dataFilename);
// Serial.println(dataFilename.length());
for (int i=0;i<=dataFilename.length();i++) {
fn[i] = dataFilename.charAt(i);
//Serial.print(dataFilename.charAt(i));
}
Serial.print ("LOOK HERE");
Serial.println(fn);
/*
if (! SD.exists(fn)) {
// only open a new file if it doesn't exist
dataFile = SD.open(fn, FILE_WRITE);
break; // leave the loop!
}
*/
dataFile = SD.open(fn, FILE_WRITE);
//dataFile.close();
//Serial.println("Done");