Hello,
I am using a DS3231, and an SD card to record the time. I want to record time on SD card while connecting and disconnecting power. But, each time after reconnecting the power all previous recorded data is erased and new data is recorded on SD card.
Can anyone please tell me how to solve the problem to be able to save the previously recorded data and continuing writting data at end of the file?
#include <DS3231.h>
// Init the DS3231 using the hardware interface
DS3231 rtc(SDA, SCL);
///SD Card*******
#include <SPI.h>
#include <SD.h>
File myFile;
// change this to match your SD shield or module;
const int chipSelect = 10;
void setup()
{
// Setup Serial connection
Serial.begin(9600);
// Uncomment the next line if you are using an Arduino Leonardo
//while (!Serial) {}
// Initialize the rtc object
rtc.begin();
// The following lines can be uncommented to set the date and time
//rtc.setDOW(WEDNESDAY); // Set Day-of-Week to SUNDAY
//rtc.setTime(12, 0, 0); // Set the time to 12:00:00 (24hr format)
//rtc.setDate(21, 7, 2021); // Set the date to January 1st, 2014
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.print("Initializing SD card...");
if (!SD.begin()) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE);
}
void loop()
{
// Send Day-of-Week
Serial.print(rtc.getDOWStr());
Serial.print(" ");
// Send date
Serial.print(rtc.getDateStr());
Serial.print(" -- ");
// Send time
Serial.println(rtc.getTimeStr());
//SD Card *****************************************************************************
myFile = SD.open("test.txt", FILE_WRITE);
//Clock *********************
// Send Day-of-Week
myFile.print(rtc.getDOWStr());
myFile.print(" ");
// Send date
myFile.print(rtc.getDateStr());
myFile.print(" -- ");
// Send time
myFile.println(rtc.getTimeStr());
myFile.close();
// Wait one second before repeating :)
delay (1000);
}