From 563c7ee4cdea92bf68ab2c1479cfd03257c4a5cb Mon Sep 17 00:00:00 2001 From: zlax Date: Mon, 1 Jul 2024 15:35:58 +0300 Subject: [PATCH] add 1sec offset scetch example --- README.md | 3 ++- rtctimestampbias.py | 2 ++ rtctimestampbias_1sec.ino | 43 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 rtctimestampbias_1sec.ino diff --git a/README.md b/README.md index fcbaa99..df01a38 100644 --- a/README.md +++ b/README.md @@ -3,4 +3,5 @@ Saving and comparing timestamps of the microcontroller's real time clock - `rtctimestampbias.py` - main script for saving and comparing timestamps (`serial` and `csv` packages are required) -- `rtctimestampbias.ino` - sample sketch for Arduino with DS1307RTC +- `rtctimestampbias.ino` - sample sketch for Arduino with DS1307RTC, in case of a daily single offset by a fixed number of seconds +- `rtctimestampbias_1sec.ino` - sample sketch for Arduino with DS1307RTC, in the case of a one-second offset by the number of milliseconds \ No newline at end of file diff --git a/rtctimestampbias.py b/rtctimestampbias.py index 0a44ac1..a96dc52 100755 --- a/rtctimestampbias.py +++ b/rtctimestampbias.py @@ -78,6 +78,8 @@ def CalculateCSVdata(option, opt, value, parser): print(f"Host diff: {hostDiff}; device diff: {deviceDiff}; host/device total diff: {totalDiff}") resultBias = (86400 / deviceDiff) * totalDiff print(f"Resulting daily clock bias in seconds: {resultBias}") + resultBiasMs = int((86400 / resultBias) * 1000) + print(f"Offset in one second per the number of milliseconds: {resultBiasMs}") def CMDOptions(): class FormatedParser(OptionParser): diff --git a/rtctimestampbias_1sec.ino b/rtctimestampbias_1sec.ino new file mode 100644 index 0000000..4e571b3 --- /dev/null +++ b/rtctimestampbias_1sec.ino @@ -0,0 +1,43 @@ +// RTC timestamp bias: Arduino with DS1307RTC exmaple +// https://gitlab.com/simplemicrocontrollers/rtctimestampbias +#include +#include + +// Time bias correction variables: +long correctionCheck = 43200000; // Offset in one second per the number of milliseconds +int correctionBias = 1; +Thread correctionThread = Thread(); // Time bias correction thread + +void setup() { + Serial.begin(9600); // Initializes the Serial connection @ 9600 baud for debug + while (!Serial); // Wait until Arduino Serial Monitor opens + setSyncProvider(RTC.get); // The function to get the time from the RTC + if(timeStatus()!= timeSet) { // Checking the time setting + Serial.println("Unable to sync with the RTC"); + } else { +// Serial.println("RTC has set the system time"); + Serial.println(now()); // Sending a timestamp to the serial port if a RTC is detected + } + + // Time bias correction thread initialization: + correctionThread.onRun(correctionLoop); + correctionThread.setInterval(correctionCheck); +} + +void loop() { + // Threads: + if (correctionThread.shouldRun()) + correctionThread.run(); +} + +void correctionLoop() { + 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); +} \ No newline at end of file