Bugreport: Timelib

Frank B

Senior Member
output from a modified demo (code below)

Code:
10:04:58 1 1 2020
10:04:59 1 1 2020
10:05:00 1 1 2020
[COLOR=#ff0000]20:21:30 28 1 2020
20:21:31 28 1 2020
20:21:32 28 1 2020
20:21:33 28 1 2020[/COLOR]

This is reproducable on T3.2 and T3.6
code:
Code:
/*
 * TimeRTC.pde
 * example code illustrating Time library with Real Time Clock.
 * 
 */

#include <TimeLib.h>
#include <Wire.h>


[COLOR=#ff0000]time_t getTeensy3Time()
{
  return Teensy3Clock.get();
}[/COLOR]

void setup()  {

[COLOR=#ff0000]    setSyncProvider(getTeensy3Time);
  setTime(10, 0, 1, 1, 1, 2020); //set initial time[/COLOR]
  
  Serial.begin(9600);
  while (!Serial) ; // wait until Arduino Serial Monitor opens
  //setSyncProvider(RTC.get);   // the function to get the time from the RTC
  if(timeStatus()!= timeSet) 
     Serial.println("Unable to sync with the RTC");
  else
     Serial.println("RTC has set the system time");      
}

void loop()
{
  if (timeStatus() == timeSet) {
    digitalClockDisplay();
  } else {
    Serial.println("The time has not been set.  Please run the Time");
    Serial.println("TimeRTCSet example, or DS1307RTC SetTime example.");
    Serial.println();
    delay(4000);
  }
  delay(1000);
}

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year()); 
  Serial.println(); 
}

void printDigits(int digits){
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}
red lines are modified from the example.
It happens after 5 minutes.
 
Last edited:
..it happens after the syncinterval :
line 40:
static uint32_t syncInterval = 300; // time sync will be attempted after this many seconds


..which is 5 minutes exactly
smile.png
 
ok, turns out this it not easy solvable.
setTime(10, 0, 1, 1, 1, 2020); //set initial time
does not set the rtc.time, because it does not know how. the automatic sync again does not know this and reads the rtc time. again. therefore the time will be wrong.

Best is:
- not to use the timelib
- or not to use setTime()
- or use
Teensy3Clock.set()

I guess this is solved as any change to timelib would be incompatible to arduino.
 
Last edited:
it does ignore setTime then. It should calculate with offsets - but it doesn't, obvoiusly.

Edit: any doing so would lead to other problems regarding usability (user: "hey, i've set the time and switched off - after restart the time is wrong")
So best is "don't fix".
Or set the rtc time somehow. That needs a new callback. This is incompatible. (The lib is quite old... maybe newer versions are better?)
 
Last edited:
see Post #5

setTime doesn't make any sense. if does not know how to set rtc it at least should use an offset. as it is , it is senseless because at first sync the user-set time is gone.

good night :)
 
So would it run for a few billion seconds (rather than 5 minutes) just by setting
static uint32_t syncInterval = 0x2FFFFFFF; ?

a clock that craps out after 5 minutes seems of minimal use. It seems like the time on my t3.2, using the demo, is set at the time of compilation. It runs for 5 minutes then recycles to the time of compilation. If I unplug the t3.2 and plug it in again, it remembers the time of compilation.
 
FWIW I tried a t4 and that got past the 5 minute mark, although I compiled right at 15:00 and it started with time 14:00. I recompiled at 15:08 and it said 14:08, so it wasn't some fortuitous glitch just as the hour clicked over. I'll leave it run for a while--see how it does for at least a few hours.
 
FWIW I tried a t4 and that got past the 5 minute mark, although I compiled right at 15:00 and it started with time 14:00. I recompiled at 15:08 and it said 14:08, so it wasn't some fortuitous glitch just as the hour clicked over. I'll leave it run for a while--see how it does for at least a few hours.

What version of TeensyDuino is in use? The T-4.0 time set was altered to be set from PC time during upload - seems the compile time reset was removed?

Was playing with Teensy3Clock.set() yesterday on T_4 and it doesn't seem to effectively push time back to the underlying RTC clock - there was a thread linked in this post :: Teensyduino-1-52-Beta-2 - also the write to SRTC edit code is shown there.

I see @manitou has posted follow up on the linked thread: TimeTeensy3-example-gt-Time-chosen-is-not-permanent - it uses time.h not timelib.h
 
OK, the t4 (unmodified, as it came from pjrc) ran the TimeTeensy3 for >13 hours w/o problem other than being 1 hour off at the outset.
Code:
/*
 * TimeRTC.pde
 * example code illustrating Time library with Real Time Clock.
 * 
 */

#include <TimeLib.h>

void setup()  {
  // set the Time library to use Teensy 3.0's RTC to keep time
  setSyncProvider(getTeensy3Time);

  Serial.begin(115200);
  while (!Serial);  // Wait for Arduino Serial Monitor to open
  delay(100);
  if (timeStatus()!= timeSet) {
    Serial.println("Unable to sync with the RTC");
  } else {
    Serial.println("RTC has set the system time");
  }
}

void loop() {
  if (Serial.available()) {
    time_t t = processSyncMessage();
    if (t != 0) {
      Teensy3Clock.set(t); // set the RTC
      setTime(t);
    }
  }
  digitalClockDisplay();  
  delay(1000);
}

void digitalClockDisplay() {
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year()); 
  Serial.println(); 
}

time_t getTeensy3Time()
{
  return Teensy3Clock.get();
}

/*  code to process time sync messages from the serial port   */
#define TIME_HEADER  "T"   // Header tag for serial time sync message

unsigned long processSyncMessage() {
  unsigned long pctime = 0L;
  const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013 

  if(Serial.find(TIME_HEADER)) {
     pctime = Serial.parseInt();
     Serial.println("OK, got time from PC");
     return pctime;
     if( pctime < DEFAULT_TIME) { // check the value is a valid time (greater than Jan 1 2013)
       pctime = 0L; // return 0 to indicate that the time is not valid
       Serial.println("reset time from PC");
     }
  }
  return pctime;
}

void printDigits(int digits){
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

In a bit, I will try the same code on the t3.6 again, just as a sanity check. I can fix one hour off in my program code and the t4 is cheaper than the t3.6
 
Recompiled once more on the t4 at ~6:24AM
Code:
RTC has set the system time
5:23:51 13 4 2020
5:23:52 13 4 2020
5:23:53 13 4 2020
5:23:54 13 4 2020

Plugged in the t3.6 I'd programmed yesterday:
Code:
RTC has set the system time
11:54:14 12 4 2020
11:54:15 12 4 2020
11:54:16 12 4 2020
11:54:17 12 4 2020
11:54:18 12 4 2020

recompiled the same program on the t3.6:
Code:
RTC has set the system time
6:28:09 13 4 2020
6:28:10 13 4 2020
6:28:11 13 4 2020
6:28:12 13 4 2020
6:28:13 13 4 2020
6:28:14 13 4 2020
 
but after 7 minutes the t3.6 is still keeping time. Not what I'd expected, glad I ran the sanity check.

One more thing, I plug the t4 back in:
Code:
RTC has set the system time
0:00:01 1 1 2019
0:00:02 1 1 2019
0:00:03 1 1 2019
0:00:04 1 1 2019
0:00:05 1 1 2019
0:00:06 1 1 2019
 
but after 7 minutes the t3.6 is still keeping time. Not what I'd expected, glad I ran the sanity check.

One more thing, I plug the t4 back in:
Code:
RTC has set the system time
0:00:01 1 1 2019
...

Which if any have an RTC battery?

The T_4.0 startup code sets RTC at '0:00:01 1 1 2019' when no valid time is present, and during upload the computer time is set - badly off an hour on Windows for now.

The T_3.6 when it detects no valid time will use the compile time - that the IDE embeds during the build, that explains the p#14 RTC=="11:54:14 12 4 2020" on power up.
 
Back
Top