add RTC timestamp bias correction

This commit is contained in:
ivan 2024-07-01 14:14:56 +03:00
parent e0d3ea3647
commit 80d8f29c0f
1 changed files with 41 additions and 8 deletions

View File

@ -33,12 +33,19 @@ unsigned long lastButtonPressed;
SevenSegmentExtended display(PIN_CLK, PIN_DIO);
// Time bias correction variables:
int correctionBias = 1; // Daily clock correction in seconds
long correctionCheck = 300000;
byte correctionHour = 3;
bool correctionReady = true;
// Threads:
Thread pressButtonThread = Thread(); // Create thread for button state checking
Thread correctionThread = Thread(); // Time bias correction thread
void setup() {
Serial.begin(9600); // Initializes the Serial connection @ 9600 baud for debug
serStr("starting setup...");
// serStr("starting setup...");
display.begin(); // Initializes the display
display.setBacklight(100); // Set the brightness to 100 %
display.print("INIT"); // Display INIT on the display
@ -60,21 +67,27 @@ void setup() {
if(timeStatus()!= timeSet)
Serial.println("Unable to sync with the RTC");
else
Serial.println("RTC has set the system time");
// Serial.println("RTC has set the system time");
Serial.println(now());
// Button state cheking thread:
pressButtonThread.onRun(pressButton);
pressButtonThread.setInterval(buttonCheck); // Interval for checking button pressing
// Time bias correction thread:
correctionThread.onRun(correctionLoop);
correctionThread.setInterval(correctionCheck);
delay (1000);
display.off();
serStr("...setup finished");
// serStr("...setup finished");
}
void loop() {
// Threads init:
if (pressButtonThread.shouldRun())
pressButtonThread.run();
if (correctionThread.shouldRun())
correctionThread.run();
if (timeStatus() == timeSet) { // If RTC works - call the checkTime function
checkTime();
@ -297,7 +310,7 @@ void checkTime() {
openDoor();
delay(60000);
}
if (hour() == 22 && minute() == 50) {
if (hour() == 23 && minute() == 20) {
closeDoor();
delay(60000);
}
@ -308,7 +321,7 @@ void checkTime() {
openDoor();
delay(60000);
}
if (hour() == 22 && minute() == 35) {
if (hour() == 23 && minute() == 15) {
closeDoor();
delay(60000);
}
@ -319,7 +332,7 @@ void checkTime() {
openDoor();
delay(60000);
}
if (hour() == 21 && minute() == 30) {
if (hour() == 22 && minute() == 30) {
closeDoor();
delay(60000);
}
@ -338,7 +351,7 @@ void checkTime() {
lightOff();
delay(60000);
}
if (hour() == 20 && minute() == 5) {
if (hour() == 21 && minute() == 35) {
closeDoor();
delay(60000);
}
@ -357,7 +370,7 @@ void checkTime() {
lightOff();
delay(60000);
}
if (hour() == 18 && minute() == 45) {
if (hour() == 19 && minute() == 45) {
closeDoor();
delay(60000);
}
@ -401,3 +414,23 @@ void checkTime() {
}
}
}
void correctionLoop() {
if (hour() == correctionHour) {
if (correctionReady) {
// CORRECTION!
tmElements_t RTCtime;
RTC.read(RTCtime);
time_t RTCtimestamp;
RTCtimestamp = makeTime(RTCtime);
tmElements_t timeNew;
time_t newTimestamp = RTCtimestamp - correctionBias; // -1sec everyday
if ((day() % 5) == 0) newTimestamp = newTimestamp - 2; // -2sec every 5 days (-0.4sec everyday)
breakTime(newTimestamp, timeNew);
RTC.write(timeNew);
setSyncProvider(RTC.get);
// CORRECTION!
correctionReady = false;
}
} else correctionReady = true;
}