Attached is the file generated on the SD card.
The following line is one line of text which is the correct number of characters.
The first number"10" is the "count" which increments correctly each time the time is read.
The next number,"17" should be the month, the next number is the day and then time, hr, min. sec,.
10-17-33-2023-29-3-0: the RTC reports the correct date and time when viewed on timestamp.ino. The correct data should be 2-20-2023-11-30-22 or something close to the correct time.
Please follow forum rules and post code/text inline, using code tags. Instructions here.
People using smart phones will often ignore posts that don't follow those rules.
By the way, it is a serious mistake to open the SD card data file, write a line or two of data, and then close it again. That vastly increases the current consumption of the project, the SD card error rate and can lead to brown outs and processor resets in some cases.
Open the file once in setup() and close it again when done writing data to the card. To protect data from unexpected shutdowns, every few minutes or hours issue a command like the following, to update the file pointers.
dataFile.flush();
To test correct RTC library function, replace the dataFile.print() statements with Serial.print() statements.
Thanks for advice, I changed to dataFile.print to serial and the following is what was printed.
target == HIGH
Count = 4
opening data fiLe
Writing to SD card
3
4-18-40-2023-29-3-0:
target == HIGH
Count = 5
opening data fiLe
Writing to SD card
3
5-18-40-2023-29-3-0:
Again the "count" is incrementing but time is not correct, the time when I took these readings was 2:05 PM EST.
My application records data at random time periods. I might get 1000 data writes one day and none of them are closer that 5 seconds. The system could stay idle for 3-4 days and then start recording again. IIs like counting people entering a building, real busy for a while and dead for hours. I have not figured the best approach but I will incorporate you comments.
This leads to a PCF8523 based board, not a DS1307. Ok, it doesn´t explain why data is correct at Serial Monitor and wrong in the SD card, but can be an issue.
// When the RTC was stopped and stays connected to the battery, it has
// to be restarted by clearing the STOP bit. Let's do this to ensure
// the RTC is running.
rtc.start();
I changed RTC_DS1307 to RTC_PCF8523 and it looks like this fixed the problem. I still have some testing to do so keep this post open in case I run into another glitch.
Thank you for your help.