I am using RTClib.h and TimeLib.h with an Uno.
Have been struggling for days in understanding the various RTC libraries available and how to use them with the Arduino Time Library.
Question 1.
If I do not want to create a new now() object each time through the loop, how do I sync the Time Library to the RTC every x seconds? Any time I try to use the RTClib function now.hour now.minute etc it requires me to use DateTime now = RTC.now() creating the now() object.
Is there an equivalent function in RTCLib as setSyncProvider(RTC.get) in DS1307RTC library?
Is there an equivalent to setSyncInterval(5000)
Question 2.
Which RTCLib should I call out? There are several used in examples:
<RTClib.h>
"RTClib" using quotes
<RTCLib.h> with capital L
and sometimes also calling out one of these:
RTC_DS1307 RTC
or RTC_DS1307 rtc
or RTC_DS3231 rtc
Question 3
a. Are tmElements_t tm part of the DS1307RTC library?
b. Are these tm.Hour tm.Minute from the RTC?
c. Is there an equivalent in the RTClib.h ?
And finally I have posted some code below just to show my usage:
#include "RTClib.h"
#include <Wire.h>
#include <TimeLib.h>
RTC_DS1307 RTC;
void setup() {
Serial.begin(9600);
Wire.begin(); //sets up the I2C
RTC.begin(); //initializes the I2C to the RTC
if (! RTC.begin()) {
Serial.println("Couldn't find RTC");
}
// Set the RTC Time to 5:10:30 Nov 3 2020
RTC.adjust(DateTime(2020,11,3,5,10,30));
//Set Arduino Time Library different than RTC time to see which is which
setTime(9, 27, 05, 14, 07, 2015);
}
void loop() {
/*
// How do I sync Time Library to RTC after x seconds?
setSyncProvider(RTC.get); // This is in the DS1307RTC.h Library
setSyncInterval(5000); // Are there such functions in RTClib ?
*/
DateTime now = RTC.now(); // Pulls RTC time into now(). Does not sync with Time Library
// Do not want to do this every loop.
//Print RTC Time
Serial.println();
Serial.print("RTC now.hour ");
Serial.println(now.hour());
Serial.print("RTC now.minute ");
Serial.println(now.minute());
Serial.print("RTC now.second: ");
Serial.println(now.second());
Serial.println();
Serial.println();
//Print Arduino TimeLib Time
Serial.print("Time Lib Hour: ");
Serial.print(hour());
Serial.println();
Serial.print("Time Lib Min: ");
Serial.print(minute());
Serial.println();
Serial.print("Time Lib Sec: ");
Serial.print(second());
Serial.println();
Serial.println();
delay(1000);
}
Updated 5 Nov 2020
I think I almost understand. Please let me know if this code is doing what I think it is doing, specifically:
In Setup:
Set the RTC to 5:10:30.
Set Time Library to 9:27:05 with setTime().
Set Time Library to RTC time with DateTime now (over writing 9:27:05)?
Set Time Library variables to RTC using tm.Hour = now.Hour, etc.
In Loop:
Every 5 seconds update hour(), minute() second() with RTC time.
Every loop update tm.hour with hour(), etc.
I know it is still not right since the serial prints show tm.Hour,tm.Minute alternating each loop as shown at the bottom.
#include "RTClib.h"
#include <Wire.h>
#include <TimeLib.h>
RTC_DS1307 RTC;
time_t time_provider()
{
return RTC.now().unixtime();
}
tmElements_t tm; //part of Time Library
void setup() {
//setSyncProvider(time_provider);
Serial.begin(9600);
Wire.begin(); //sets up the I2C
RTC.begin(); //initializes the I2C to the RTC
if (! RTC.begin()) {
Serial.println("Couldn't find RTC");
}
// Set the RTC Time to 5:10:30 Nov 3 2020
RTC.adjust(DateTime(2020,11,3,5,10,30));
//Set Arduino Time Library different than RTC time 9:27:05
setTime(9, 27, 05, 14, 07, 2015);
//Setting Time Library to RTC time
DateTime now = RTC.now();
tm.Hour = now.hour();
tm.Minute = now.minute();
tm.Second = now.second();
}
void loop() {
setSyncProvider(RTC.now);
setSyncInterval(5000);
//Time Library time updates to RTC every 5 seconds
tm.Hour = hour();
tm.Minute = minute();
tm.Second = second();
Serial.print("tm.Hour: ");
Serial.print(tm.Hour);
Serial.println();
Serial.print("tm.Minute: ");
Serial.print(tm.Minute);
Serial.println();
Serial.print("tm.Seconds: ");
Serial.print(tm.Second);
Serial.println();
Serial.println();
delay(1000);
}
Serial Prints:
tm.Hour: 5
tm.Minute: 34
tm.Seconds: 56
tm.Hour: 18
tm.Minute: 0
tm.Seconds: 0
tm.Hour: 5
tm.Minute: 34
tm.Seconds: 56
tm.Hour: 18
tm.Minute: 0
tm.Seconds: 0
tm.Hour: 5
tm.Minute: 34
tm.Seconds: 56
tm.Hour: 18
tm.Minute: 0
tm.Seconds: 0