add 1sec offset scetch example

This commit is contained in:
ivan 2024-07-01 15:35:58 +03:00
parent 1428b7923b
commit 563c7ee4cd
3 changed files with 47 additions and 1 deletions

View File

@ -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

View File

@ -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):

43
rtctimestampbias_1sec.ino Normal file
View File

@ -0,0 +1,43 @@
// RTC timestamp bias: Arduino with DS1307RTC exmaple
// https://gitlab.com/simplemicrocontrollers/rtctimestampbias
#include <Thread.h>
#include <DS1307RTC.h>
// 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);
}