Date and Time are not recording properly after few days

Hi,

i am using aurduino UNO R3, RTCi2C module,5v ready microSD card breakout board+ and proximity sensor.

it will record actual date and time for three days without issues. but after three days, date will move back to two days or one day and start to record with different time.

2019/11/8,14:27:25,3,2
2019/11/8,14:28:10,1,5
2019/11/8,14:28:21,3,10
2019/11/8,14:28:33,1,5
2019/11/8,14:30:6,3,17
2019/11/8,14:30:28,3,9
2019/11/8,14:36:30,2,15
2019/11/8,14:39:47,4,26
2019/11/8,14:44:47,3,11
2019/11/8,14:46:34,1,14
2019/11/8,14:46:38,4,100
2019/11/8,14:46:47,4,8
2019/11/8,15:9:32,2,21
2019/11/8,15:11:1,2,6
2019/11/7,11:48:45,3,21
2019/11/7,11:49:2,3,12
2019/11/7,11:50:50,2,1
2019/11/7,11:50:53,1,2
2019/11/7,12:3:37,2,3
2019/11/7,12:4:32,4,3
2019/11/7,12:7:28,4,3
2019/11/7,12:8:58,1,4
2019/11/7,12:8:59,1,0
2019/11/7,12:9:4,2,9
2019/11/7,12:25:41,2,47
2019/11/7,12:37:7,4,6
2019/11/7,12:43:32,4,14
2019/11/7,13:7:53,3,2
2019/11/7,13:10:59,1,4

Please give suggestions to fix this issue.

Thanks.

What code are you running? Please post your code.

Exactly how far wrong is the time? I am guessing that the error is 100000 seconds, or very close to that. Notice that 100000 seconds equals 27 hours, 46 minutes, and 40 seconds. Perhaps some count of seconds has reached 99999 and is "rolling over" to 0? Given that this is an Arduino project, that is a strange number to roll over at. Usually, you would expect a number like 65535 (which, in binary, is 1111111111111111, and electronics usually count in binary). But, if the number is being kept track of in decimal (which is occassionally done, for a number of reasons), then it might indeed roll over at a number like 100000.

Thank you very much for reply.

this is the code.

#include "RTClib.h"
#include <Arduino.h>
#include <SPI.h>
#include <SD.h>
#include <Wire.h>

struct sensor {
int id;
int sensor_pin;
volatile unsigned long timer;
bool doorOpen;
};

RTC_DS1307 rtc;
int proximity;
int pinCS = 10;
File myFile;
sensor sensors[6];

void setup() {
Serial.begin(9600);
while (!Serial) { ;}
delay(2000);
Wire.begin();
rtc.begin();

for (int i = 0; i < 6; i++) {
sensors*.id = i + 1;*
_ sensors*.timer = 0;_
_ sensors.doorOpen = true;_
sensors.sensor_pin = i + 2;
pinMode(sensors.sensor_pin, INPUT_PULLUP);
_ }
pinMode(pinCS, OUTPUT);*_

* Serial.print("Initializing SD card.");*
* if (!SD.begin(10)) {*
* Serial.println("initialization failed!");*
* }*
* else { Serial.println("initialization done."); }*

* if (! rtc.begin()) {*
* Serial.println("Couldn't find RTC");*
* while (1);*
* }*
* rtc.adjust(DateTime(F(DATE), F(TIME)));*

* TCCR0A = (1 << WGM01);*
* OCR0A = 0xF9; // iterate at 1 ms*
* TIMSK0 |= (1 << OCIE0A); //interrupt request*
* sei(); //Enable interrupt*
* TCCR0B |= (1 << CS01); //prescalar 64*
* TCCR0B |= (1 << CS00);*
}

void loop() {
* for (int i = 0; i < 6; i++) {*
* DateTime now = rtc.now();*
proximity = digitalRead(sensors*.sensor_pin);
_ if (proximity == HIGH) {
sensors.doorOpen = true;
}
if (proximity == LOW) {
sensors.doorOpen = false;
}
if (!sensors.doorOpen) {
if (sensors.timer > 500) {_

myFile = SD.open("test.txt", FILE_WRITE);
_ if (myFile) {
showDate(now, myFile);
Serial.print(sensors.id);
myFile.print(sensors.id);
Serial.print(",");_

write_data(sensors.timer, myFile);
_ myFile.close();
} else {
Serial.println("oops");
}
}
sensors.timer = 0;
}
}
}*_

void write_data(unsigned long timeOpen, File myFile) {
* Serial.println(timeOpen / 1000);*
* myFile.print(",");*
* myFile.println(timeOpen / 1000);*
}

void showDate(DateTime& now, File myFile) {
* myFile.print(now.year(), DEC);*
* myFile.print('/');*
* myFile.print(now.month(), DEC);*
* myFile.print('/');*
* myFile.print(now.day(), DEC);*
* myFile.print(",");*
* myFile.print(now.hour(), DEC);*
* myFile.print(':');*
* myFile.print(now.minute(), DEC);*
* myFile.print(':');*
* myFile.print(now.second(), DEC);*
* myFile.print(",");*
}

ISR(TIMER0_COMPA_vect) {
* for (int i = 0; i < 6; i++) {*
_ if (sensors*.doorOpen) {
sensors.timer++;
}
}
}
please find attached recorded data. Data recorded properly from 6th nov to 8th nov and then jump back to 7th.
Same program and setup had given proper data for 20 days. but now giving issues repeatedly.
Thanks.
TEST.TXT (30.6 KB)*_

Now post the code without italics.

Or just edit the previous post to add the code tags you forgot.

That in ASCII takes a lot of memory, consider using Unix or UTC time. They require an unsigned 32 bit integer, since your code is not complete maybe you are doing something like this but inadvertently have a signed integer. There are a lot of sources including here that have the code to do the conversion. Your RTC (Real Time Clock) can also operate in Unix time simplifying all the clock reading as one statement, you then have all the data i one unsigned integer. The code just posted while I was typing will support Unix time.
Good Luck & Have Fun!
Gil