From fc9c80e3204a3112559b573f3e50e25939161614 Mon Sep 17 00:00:00 2001 From: zlax Date: Mon, 1 Jul 2024 14:09:25 +0300 Subject: [PATCH] v0.55 add RTC timestamp bias correction --- dclock.ino | 57 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 43 insertions(+), 14 deletions(-) diff --git a/dclock.ino b/dclock.ino index be2e820..271d1df 100644 --- a/dclock.ino +++ b/dclock.ino @@ -1,4 +1,4 @@ -// Erisian digital clock - v0.5 +// Digital erisian clock - v0.55 #include #include #include @@ -9,6 +9,7 @@ const byte PIN_DIO = 3; // DisplaySetting // DIO pin const byte buttonPin = 4; // ButtonSetting // Button pin (a 10K resistor is necessary) SevenSegmentExtended display(PIN_CLK, PIN_DIO); // DisplaySetting Thread mainThread = Thread(); // mainThread +Thread correctionThread = Thread(); // Time bias correction thread long buttonPressed = 0; // Button press time counter long previousButtonMillis = 0; // Time of last button press byte viewMode = 0; // View mode after pressing button @@ -18,22 +19,25 @@ int mainCheck = 108; // The main interval is an eighth of a de byte octavo = 0; // Eight-count counter byte colon = 0; // Colon status float erisiansec; // Erisian second - // Custom settings: int brightnessPercent = 32; // Display brightness percentage value -long timezoneOffset = (-28800); // Erisian timezoneOffset. Formula for calculating: - // ((-5 christian chours) - (GMT hours)) +long timezoneOffset = (-28800); // Erisian timezoneOffset. Formula for calculating: ((-5 christian chours) - (GMT hours)) // Examples: // GMT+3: ( (-5)-(+3) )*60*60 = -28800 // GMT-0: ( (-5)-(0) )*60*60 = -18000 // GMT-3: ( (-5)-(-3) )*60*60 = -7200 // GMT-5: ( (-5)-(-5) )*60*60 = 0 // GMT-7: ( (-5)-(-7) )*60*60 = 7200 - // The beginning of Discordian day (0 hours, 0 minutes, - // 0 seconds) coincides with the beginning of Christian - // day on Easter Island on winter time (GMT-5). + // The beginning of Discordian day (0 hours, 0 minutes, 0 seconds) coincides + // with the beginning of Christian day on Easter Island on winter time (GMT-5). // Details: https://discordia.fandom.com/wiki/Erisian_Time +// Time bias correction variables: +int correctionBias = 11; // Daily clock correction in seconds +long correctionCheck = 300000; +byte correctionHour = 3; +bool correctionReady = true; + void setup() { Serial.begin(9600); // Initializes the Serial connection @ 9600 baud for debug display.begin(); // Display setting, initializes the display @@ -44,19 +48,25 @@ void setup() { Serial.println("Unable to sync with the RTC"); display.print("_time_not_set_"); // Shows warning "time not set" } else { - Serial.println("RTC has set the system time"); +// Serial.println("RTC has set the system time"); + Serial.println(now()); display.print("HAIL_EriS"); // Shows display initialization } pinMode(buttonPin, INPUT); // Button setting - // Main thread initialization: + // Main thread thread initialization: mainThread.onRun(mainLoop); mainThread.setInterval(mainCheck); + // Time bias correction thread initialization: + correctionThread.onRun(correctionLoop); + correctionThread.setInterval(correctionCheck); } void loop() { - // Main thread: + // Threads: if (mainThread.shouldRun()) mainThread.run(); + if (correctionThread.shouldRun()) + correctionThread.run(); // Time check: if (timeStatus() != timeSet) display.print("SEt tiMe"); @@ -159,16 +169,16 @@ void ddate() { display.setColonOn(0); if (viewMode == 2) { if (m == 1 || m == 2) { - daynumber = days[(m - 1)] + d; // for any type of year, it calculate the number of days for January or February + daynumber = days[(m - 1)] + d; // for any type of year, it calculate the number of days for January or February dday = daynumber; dseason = 0; - } // Now, try to calculate for the other months + } // Now, try to calculate for the other months else if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0) { //those are the conditions to have a leap year - daynumber = days[(m - 1)] + d + 1; // if leap year, calculate in the same way but increasing one day + daynumber = days[(m - 1)] + d + 1; // if leap year, calculate in the same way but increasing one day dday = (daynumber % 73) - 1; dseason = (daynumber - 1) / 73; } - else { // if not a leap year, calculate in the normal way, such as January or February + else { // if not a leap year, calculate in the normal way, such as January or February daynumber = days[(m - 1)] + d; dday = daynumber % 73; dseason = daynumber / 73; @@ -218,3 +228,22 @@ void longPress() { display.print("HAIL_EriS"); viewMode = 0; } + +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; + breakTime(newTimestamp, timeNew); + RTC.write(timeNew); + setSyncProvider(RTC.get); + // CORRECTION! + correctionReady = false; + } + } else correctionReady = true; +}