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

Thread: cannot set Teensy RTC time

  1. #1
    Junior Member
    Join Date
    Jul 2018
    Posts
    6

    cannot set Teensy RTC time

    Testing the example code TimeTeensy3; the initialization and time display works well. The serial port is returning the system time with a few seconds delay, presumably due to the compiling time. However, when I want to adjust the RTC time, either by using the serial command "t1634734422" or adding this in the loop:
    Code:
     time_t  tt = 1634734422;
     Teensy3Clock.set(tt);
     setTime(tt);
    It still returns the system time and nothing is changed. Could you please point out what is wrong or give me an example code for setting the RTC time? Thanks!

    Complete Source Code (or a link to the code)
    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(9600);
      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()) {
        t = processSyncMessage();
        if (t != 0) {
          Teensy3Clock.set(t);
           // set the RTC
          setTime(t);
        }
      }
     time_t  tt = 1634734422;
     Teensy3Clock.set(tt);
     setTime(tt);
      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();
         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
         }
      }
      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);
    }
    Which PJRC product you're using
    Time/TimeTeensy3.pde
    Part numbers of any other chips or modules + links to their info
    CFS-20635768 crystal, Teensy3.2
    Wiring details - how exactly have you connected the hardware (a photo's worth 1000 words)
    MicroUSB and crystal only
    Software setup, Arduino vs C+makefile, running on Windows, Mac or Linux?Versions?
    Arduino IDE, Linux

  2. #2
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    7,086
    The text at the end of that post looks like a cut and paste spam job?

    A forum search will turn up many links - this one should be useful: TimeTeensy3-example-gt-Time-chosen-is-not-permanent

  3. #3
    Junior Member
    Join Date
    Jul 2018
    Posts
    6
    Quote Originally Posted by defragster View Post
    The text at the end of that post looks like a cut and paste spam job?
    No it is not - I followed the instruction in the Sticky post.

    I came across that post before but it is irrelevant. Yet, thanks anyway.

    I figured it out that the serial command should start with a capital T, i.e. "T1634734422" . I am keeping this post in case someone faces the same issue.

  4. #4
    Senior Member
    Join Date
    Jul 2014
    Posts
    1,939
    Quote Originally Posted by cuchan View Post
    I figured it out that the serial command should start with a capital T, i.e. "T1634734422" . I am keeping this post in case someone faces the same issue.
    or you change the relevant definition to 't'

  5. #5
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    7,086
    Indeed - I assumed it wasn't - and didn't remove - but the presentation order left me wondering.

    The sample includes a note to that effect - but having an example message "T1634734422" would be more direct:
    /* code to process time sync messages from the serial port */
    #define TIME_HEADER "T" // Header tag for serial time sync message
    It still returns the system time and nothing is changed.
    In reading that and other posts there are other reasons for failure { battery or crystal bad or bad connection, invalid time value, RTC hardware exposed to overvoltage??? } - the link to an alternate way to set time - versus the code in OP - would have shown the hardware wasn't the problem and showed an alternate software solution.
    Code:
    setTime(10,10,10,11, 12, 2017);
    Teensy3Clock.set(now());

  6. #6
    Junior Member
    Join Date
    Jul 2018
    Posts
    6
    Thanks! That is better than my version.

    Would like to add the reason why my code didn't work: I was working on the file in the Arduino library directory, which is read-only. The code works once I copy it to another folder.

  7. #7
    FYI, the example code TimeTeensy3.ino and many similar have a small error:
    Code:
    unsigned long processSyncMessage() {
      unsigned long pctime = 0L;
      const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013 
    
      if(Serial.find(TIME_HEADER)) {
         pctime = Serial.parseInt();
         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
         }
      }
      return pctime;
    }
    The 'if( pctime < DEFAULT_TIME) {...'
    will never execute because of the 'return pctime;' statement above it.

  8. #8
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    2,803
    we’re trying to find out if the compiler might optimize it away

Tags for this Thread

Posting Permissions

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