Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 16 of 16

Thread: Bugreport: Timelib

Hybrid View

  1. #1
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    6,886

    Bugreport: Timelib

    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
    20:21:30 28 1 2020
    20:21:31 28 1 2020
    20:21:32 28 1 2020
    20:21:33 28 1 2020
    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>
    
    
    time_t getTeensy3Time()
    {
      return Teensy3Clock.get();
    }
    
    void setup()  {
    
        setSyncProvider(getTeensy3Time);
      setTime(10, 0, 1, 1, 1, 2020); //set initial time
      
      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 by Frank B; 01-28-2020 at 07:42 PM.

  2. #2
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    6,886
    ..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

  3. #3
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    6,886
    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 by Frank B; 01-28-2020 at 08:50 PM.

  4. #4
    Senior Member oddson's Avatar
    Join Date
    Feb 2013
    Location
    Isle in the Salish Sea
    Posts
    1,342
    When it hits the sync interval it syncs!?
    That's a bug??

  5. #5
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    6,886
    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 by Frank B; 01-28-2020 at 09:04 PM.

  6. #6
    Senior Member oddson's Avatar
    Join Date
    Feb 2013
    Location
    Isle in the Salish Sea
    Posts
    1,342
    So the bug is the set command is not setting Teensy3Clock?

  7. #7
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    6,886
    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

  8. #8
    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.

  9. #9
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    11,525
    Quote Originally Posted by jrraines View Post
    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

  10. #10
    Sometimes I feel like I'm totally incompetent. I keep finding:
    https://github.com/PaulStoffregen/Ti...aster/Time.cpp etc.

    but I don't see any reference to Teensy3Clock.set() in the code or documentation. I can't be looking at the library I'm running.

  11. #11
    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

  12. #12
    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

  13. #13
    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.

  14. #14
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    6,886
    use Teensy3Clock.set()

  15. #15
    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

  16. #16
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    11,525
    Quote Originally Posted by jrraines View Post
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •