Hi,
I wish to use the date from RTC as filename for a datalogging project. I have spent a day looking at posts on the forum but I still cannot why my code will not work. I tried putting a slash in front of the filename but that did not work. The filename that I want is correct is the serial monitor output which is below:
17:29:47.030 -> Filename 05.05.23.txt
17:29:47.077 -> 05.05.23 17:29:47 247 3567.
Any ideas on how to solve this would be greatly appreciated. code below.
// JL Test code for SD card and RTC
#include <SD.h>
#include <SPI.h>
#include <DS3231.h>
File sdcard_file; // file object - naming the instance of the opened file - only one can be open at once
DS3231 rtc(SDA, SCL);
int CS_pin = 10; // Pin 10 on Arduino Uno
int dist;
int flux;
char filename[] = "00000000.TXT";
void setup() {
flux = 3567;
dist = 247;
Serial.begin(9600);
//pinMode(sensor_pin,INPUT);
pinMode(CS_pin, OUTPUT);
rtc.begin();
// SD Card Initialization
if (SD.begin())
{
Serial.println("SD card is ready to use.");
} else
{
Serial.println("SD card initialization failed");
return;
}
}
void loop() {
sprintf(filename, "%s.txt", rtc.getDateStr(FORMAT_SHORT)); // this creates the desired 12 character filename % is where the variable goes and "S" denotes that it is a string.
Serial.print("Filename ");
Serial.println(filename);
Serial.print(rtc.getDateStr(FORMAT_SHORT)); // use short format to get filename of date.txt down to 12 characters allowable including .TXT
Serial.print(" ");
Serial.print(rtc.getTimeStr());
Serial.print(" ");
Serial.print(dist);
Serial.print(" ");
Serial.println(flux);
sdcard_file = SD.open(filename, FILE_WRITE);
if (sdcard_file) {
sdcard_file.print(rtc.getDateStr(FORMAT_SHORT));
sdcard_file.print(" ");
sdcard_file.print(rtc.getTimeStr());
sdcard_file.print(" ");
sdcard_file.print(dist);
sdcard_file.print(" ");
sdcard_file.println(flux);
sdcard_file.close(); // close the file
}
// if the file didn't open, print an error:
else {
Serial.println("error opening file in loop code");
}
delay(3000);
}
Hello,
I really appreciate the fast response and a clear answer to my issue. I actually only need the date in the month (two characters) as my file name - today is 05.txt. I could get it to work using day of the week (Mon-Sun) which is commented out in the code below. However I am still struggling to get it to work with just the date in the month which is the live code below. The serial monitor output shows that that "05" is being correctly produced by the myString.remove command. Serial monitor output:
// JL Test code for SD card and RTC
#include <SD.h>
#include <SPI.h>
#include <DS3231.h>
File sdcard_file; // file object - naming the instance of the opened file - only one can be open at once
// then sd card commands start with sdcard-file.
DS3231 rtc(SDA, SCL);
int CS_pin = 10; // Pin 10 on Arduino Uno
int dist;
int flux;
char filename[] = "00000000.TXT"; // declare the variable to use for filename
String myString = "Hello String";
void setup() {
flux = 3567;
dist = 247;
Serial.begin(9600);
//pinMode(sensor_pin,INPUT);
pinMode(CS_pin, OUTPUT);
rtc.begin();
// SD Card Initialization
if (SD.begin())
{
Serial.println("SD card is ready to use.");
} else
{
Serial.println("SD card initialization failed");
return;
}
}
void loop() {
//sprintf(filename, "%s.txt", rtc.getDOWStr(FORMAT_SHORT)); // this creates the desired 12 character filename % is where the variable goes and "S" denotes that it is a string.
//Serial.print("Filename ");
//Serial.println(filename);
String myString = rtc.getDateStr();
Serial.println(myString);
myString.remove(2,8); // starting after character 2, remove 8 characters from the string.
Serial.println(myString);
sprintf(filename, "%s.txt", myString ); // this creates the desired filename % is where the variable goes and "S" denotes that it is a string.
Serial.print("Filename ");
Serial.println(filename);
Serial.print(rtc.getDateStr());
Serial.print(" ");
Serial.print(rtc.getTimeStr());
Serial.print(" ");
Serial.print(dist);
Serial.print(" ");
Serial.println(flux);
sdcard_file = SD.open(filename, FILE_WRITE);
if (sdcard_file) {
sdcard_file.print(rtc.getDateStr());
sdcard_file.print(" ");
sdcard_file.print(rtc.getTimeStr());
sdcard_file.print(" ");
sdcard_file.print(dist);
sdcard_file.print(" ");
sdcard_file.println(flux);
sdcard_file.close(); // close the file
}
// if the file didn't open, print an error:
else {
Serial.println("error opening file in loop code");
}
delay(3000);
}
PS the serial monitor output on my screen below the row with "05" actually shows filename as an open bracket followed by two square characters and then .txt