Can someone provide some clarity about this?
I want to use the RTC functions in the Time library to create an RTC whose accuracy is a "don't care", I just need something to allow me to use "now()" and give me relative time stamps while running and debugging code that is event driven.
This should work for this purpose right? I have used SetTime() to set up the clock and do note that now() is showing a time_t value that increments by the seconds. Do I need to use setSyncProvider() and setSyncInterval() to keep things going correctly and convert to date and time strings?
I do not care about the absolute accuracy of the Arduino system clock based RTC, and I don't care that it restarts when the Arduino power cycles or resets. I just want the basic timing and timestamp constructs without having to write my own.
Yes, that will work.
Some Arduino boards have a crystal, then it is more accurate than with a resonator.
The Adafruit RTClib can use the compiler date and time.
If you want to use the compiler date and time with the TimeLib, then you have to write your own function.
I used a clock for a few years this way. But in the end I wanted to overcome a power failure, so I had to add a RTC. The DS3231 is also more accurate than a 16 MHz crystal.
If you just want to keep track of time without an external RTC, then it is easy enough to make your own clock. This works:
const unsigned long ONE_SECOND = 1000000UL;
unsigned long microsAtLastSecond = 0UL;
byte hh = 0, mi = 0, ss = 0;
void setup() {
Serial.begin(115200);
}
void loop() {
// check for beginning of a new second
if ((micros() - microsAtLastSecond) >= ONE_SECOND) {
// advance the time to the next second
microsAtLastSecond += ONE_SECOND;
ss++;
if (ss >= 60) {
ss -= 60;
mi++;
}
if (mi >= 60) {
mi -= 60;
hh++;
}
if (hh >= 24) {
hh -= 24;
}
// output the time once per second
Serial.print("Time: ");
if (hh<10) Serial.print('0');
Serial.print(hh);
Serial.print(':');
if (mi<10) Serial.print('0');
Serial.print(mi);
Serial.print(':');
if (ss<10) Serial.print('0');
Serial.println(ss);
}
}
If you simply wish to count seconds, then this will suffice:
const unsigned long ONE_SECOND = 1000000UL;
unsigned long microsAtLastSecond = 0UL;
unsigned long sec = 0;
void setup() {
Serial.begin(115200);
}
void loop() {
// check for beginning of a new second
if ((micros() - microsAtLastSecond) >= ONE_SECOND) {
// advance the time to the next second
microsAtLastSecond += ONE_SECOND;
sec++;
// output the time once per second
Serial.print("Time: ");
Serial.print(sec);
Serial.println(" sec.");
}
}
There is no need to use microseconds. Using the millis() is exactly as accurate as the 16MHz crystal. Even if the sketch has a delay of a few seconds now and then, then the clock is still accurate.
I have an example millis_clock.ino which is the same as reply #5 by odometer, but with millis().
robodlc, some libraries turn off the interrupts for more than 1 milliseconds. Then millis() could skip a beat.