Hello, can someone explain to me why I'm getting a different value after a second sprintf call?
#include <SPI.h>
#include <SD.h>
#include <DS3232RTC.h>
File myFile;
#define pinCS 53 // SD-Card CS-pin
char dateTimeStart[15] = "00-00-00,00:00";
char dateTimeEnd[15] = "00-00-00,00:00";
signed char round5delta[5] = {0, -1, -2, -3, -4}; // difference to the "rounded to nearest 5" value
long round5(long no) {
return no + round5delta[no % 5];
}
void setup() {
Serial.begin(9600);
Serial1.begin(115200);
setSyncProvider(RTC.get); // the function to get the time from the RTC
if (timeStatus() != timeSet)
Serial.println("Unable to sync with the RTC");
else
Serial.println("RTC has set the system time");
Serial.print("Initializing SD card...");
if (!SD.begin(53)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
Serial1.println("Arduino_ready|"); Serial.println("Arduino_ready|");
delay(500);
readSDcard(0, 6);
}
void loop() {
}
void readSDcard(byte line, byte timeFrame) {
Serial.println("Start SDRead");
if (timeFrame > 0) {
tmElements_t sdElements = {second(), minute(), hour(), weekday(), day(), month(), (year() - 1970) };
time_t sdTime = makeTime(sdElements);
sdTime = sdTime - (timeFrame * 3600UL);
breakTime(sdTime, sdElements);
sprintf(dateTimeStart, "%02d-%02d-%02d,%02d:%02d", (sdElements.Year + 1970), sdElements.Month, sdElements.Day, sdElements.Hour, round5(sdElements.Minute));
Serial.println(dateTimeStart);
sprintf(dateTimeEnd, "%02d-%02d-%02d,%02d:%02d", year(), month(), day(), hour(), round5(minute()));
}
Serial.println(dateTimeStart);
Serial.println(dateTimeEnd);
}
RTC has set the system time
Initializing SD card...initialization done.
Start SDRead
2018-10-15,15:00
0
2018-10-15,21:00
The first print of dateTimeStart gives the result I'm expecting. But the second isn't.