-2

Trying to understand the two libraries and setSyncProvider(RTC.now). Using Arduino Uno with Adafruit ChronoDot. Aduino IDE 1.8.13.

The code below is simply to understand what is happening.
I am getting surprising results which means I do not understand what the libraries are doing.

What I think I am telling it to do:

  1. Set time RTC to 05:10:30
  2. Set Time Library to 09:27:05
  3. In Loop, printing Library time (hour, minute, seconds)
  4. In Loop, printing RTC time (now.hour, now.minute, now.second)
  5. Every 5 seconds sync the Time Library to the RTC.
    This is not what it is doing.
    The output from serial prints is at the bottom.

Question 1: Why does Time Library time hour() minute() second() change from 9:27:9 to 12:43:44 after first sync and then from 12:43:48 to 19:1:20 after second sync, then reseting to 19:1:20 after each subsequent sync?

Question 2: After the Sync, why is Time Library hour(), minute() and second() not set to RTC values of now.hour(), now.minute() and now.second()?

#include "RTClib.h" 
#include <Wire.h> 
#include <TimeLib.h> 
 
RTC_DS1307 RTC; 
unsigned long LastSyncTime = 0; //will control when it syncs just to understand 
 
time_t time_provider() 
{ 
 return RTC.now().unixtime(); 
} 
 
void setup() { 
Serial.begin(9600); 
Wire.begin(); //sets up the I2C 
RTC.begin(); //initializes the I2C to the 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 so see how sync works 
setTime(9, 27, 05, 4, 07, 2015); 
} 
void loop() { 
//Sync time every 5 seconds 
if ((millis() - LastSyncTime) > 5000) 
 { 
 Serial.println("+++++ Sync ++++++++"); 
 setSyncProvider(RTC.now); //sets Time Library to RTC time: hour() set to RTC now.hour (?) 
 LastSyncTime = millis(); 
 
 } 
DateTime now = RTC.now(); // gets RTC time into now Object 
 
//Print Time Lib Times 
Serial.print("hour: "); 
Serial.print(hour()); 
Serial.println(); 
Serial.print("minute: "); 
Serial.print(minute()); 
Serial.println(); 
Serial.print("seconds: "); 
Serial.print(second()); 
Serial.println(); 
Serial.println(); 
 
 
//Print RTC time 
Serial.print("now.hour: "); 
Serial.print(now.hour()); 
Serial.println(); 
Serial.print("now.minute: "); 
Serial.print(now.minute()); 
Serial.println(); 
Serial.print("now.second: "); 
Serial.print(now.second()); 
Serial.println(); 
Serial.println("______________________"); 
 
delay(1000); 
} 

OUTPUT

hour: 9
minute: 27
seconds: 8

now.hour: 5
now.minute: 10
now.second: 33

hour: 9
minute: 27
seconds: 9

now.hour: 5
now.minute: 10
now.second: 34

+++++ Sync ++++++++
hour: 12
minute: 43
seconds: 44

now.hour: 5
now.minute: 10
now.second: 35

hour: 12
minute: 43
seconds: 45

now.hour: 5
now.minute: 10
now.second: 36

hour: 12
minute: 43
seconds: 46

now.hour: 5
now.minute: 10
now.second: 37

hour: 12
minute: 43
seconds: 47

now.hour: 5
now.minute: 10
now.second: 38

hour: 12
minute: 43
seconds: 48

now.hour: 5
now.minute: 10
now.second: 39

+++++ Sync ++++++++
hour: 19
minute: 1
seconds: 20

now.hour: 5
now.minute: 10
now.second: 40

hour: 19
minute: 1
seconds: 21

now.hour: 5
now.minute: 10
now.second: 41

hour: 19
minute: 1
seconds: 22

now.hour: 5
now.minute: 10
now.second: 42

hour: 19
minute: 1
seconds: 23

now.hour: 5
now.minute: 10
now.second: 43

hour: 19
minute: 1
seconds: 24

now.hour: 5
now.minute: 10
now.second: 44

+++++ Sync ++++++++
hour: 19
minute: 1
seconds: 20

now.hour: 5
now.minute: 10
now.second: 45

hour: 19
minute: 1
seconds: 21

now.hour: 5
now.minute: 10
now.second: 46

hour: 19
minute: 1
seconds: 22

now.hour: 5
now.minute: 10
now.second: 47

hour: 19 minute: 1 seconds: 23

now.hour: 5
now.minute: 10
now.second: 48

hour: 19
minute: 1
seconds: 24

now.hour: 5
now.minute: 10
now.second: 49

+++++ Sync ++++++++
hour: 19
minute: 1
seconds: 20

now.hour: 5
now.minute: 10
now.second: 50

hour: 19
minute: 1
seconds: 21

now.hour: 5
now.minute: 10
now.second: 51

hour: 19
minute: 1
seconds: 22

now.hour: 5
now.minute: 10
now.second: 52

hour: 19
minute: 1
seconds: 23

now.hour: 5
now.minute: 10
now.second: 53

asked Nov 6, 2020 at 21:43
2
  • 1
    −1, as you visibly did not pay attention to an answer to your previous question. I already explained why setSyncProvider(RTC.now) is incorrect and can only result in the Time library giving you garbage. Commented Nov 6, 2020 at 22:06
  • @EdgarBonet - Sorry I did use your previous information and it did work. Copy paste error here from older code. Thanks for your help and patience. Commented Nov 6, 2020 at 23:55

1 Answer 1

1

Why does Time Library time hour() minute() second() change from 9:27:9 to 12:43:44 after first sync and then from 12:43:48 to 19:1:20 after second sync, then reseting to 19:1:20 after each subsequent sync?

Because:

  1. You keep calling setSyncProvider(). The whole point of the sync system is that you should only call that once to set the sync provider and then it syncs itself automatically.
  2. You are setting the sync provider to a function that returns an object. The sync provider needs a function that returns an unsigned long (time_t).

Your code should be:

setSyncProvider(time_provider);

And it should only be called once from inside setup.

Question 2: After the Sync, why is Time Library hour(), minute() and second() not set to RTC values of now.hour(), now.minute() and now.second()?

Because of all the things I stated above and were also told in your last question.

answered Nov 6, 2020 at 22:02

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.