Forum Rule: Always post complete source code & details to reproduce any issue!
Page 1 of 2 1 2 LastLast
Results 1 to 25 of 29

Thread: Newbee question about Set time example TimeTeensy3

  1. #1

    Newbee question about Set time example TimeTeensy3

    Hello,

    I tried the TimeTeensy3.ino
    Yesterday I ran the program and it set the time.

    Today I would like to update the time with winter time. And time is not changing. (PC time is at the right local time, but Teensy display local time +1h )
    How to update with the correct time ?


    Second question, Teensy displayed time is PC Time - 2 minutes. Why ?

    Thanks!

  2. #2
    I don't think the Teensy clock is aware of day light savings time, hence you will need to add that based on known time changes.

    Odd on #2, my sync is right on

  3. #3
    Quote Originally Posted by KrisKasprzak View Post
    I don't think the Teensy clock is aware of day light savings time, hence you will need to add that based on known time changes.

    Odd on #2, my sync is right on
    you have right, but it was not my question. I misspoked!

    The first time you run TimeTeensy3.ino, the Teensy was set at the correct date/time (PC time -2 minutes; why???).

    BUT now change the time on your computer and run/upload again TimeTeensy3.ino. The teensy time is not set at the new PC time ! How to force it to copy the new time each time you upload the program into the Teensy.

    Thanks !

  4. #4
    nobody has any idea how to setup time into the Teensy 3.2 or 3.6 twice according to the PC time ??

    Thanks

  5. #5
    It's time to set the sommer time. But unfortunately the Teensy is not set at the new PC time... Why ???
    Thanks

  6. #6
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    14,440
    RTC on Teensy 3.x is "Compile Time" - filled in as HEX is created and unchanging - reload the same HEX 22 hours later and it can go back to that time.Off by 2 minutes suggests a long build time or delay in uploading the sketch.

    Teensy 4.x has the loader set the PC time to RTC when the upload happens.

    To adjust time from the current Teensy time takes manipulation of the clock memory - there are examples. And the sketch time is unique to the RTC time and has to be pushed or pulled to synchronize them.

  7. #7
    Quote Originally Posted by defragster View Post
    RTC on Teensy 3.x is "Compile Time" - filled in as HEX is created and unchanging

    To adjust time from the current Teensy time takes manipulation of the clock memory - there are examples.
    Thanks defragster,

    I canot understand why when you are compiling again the files the "compile time" is not updated (teensy bug ?). But it like this

    However I haven't found examples how the adjust the time by modifying the clock memory.

    Thanks.

  8. #8
    No idea how to "reset" Teensy RTC, to set a new time ?
    Thanks.

  9. #9
    Senior Member
    Join Date
    Apr 2014
    Location
    Cheltenham, UK
    Posts
    301
    Look in the teensy libraries for TimeTeensy3.ino!!

  10. #10
    Quote Originally Posted by BriComp View Post
    Look in the teensy libraries for TimeTeensy3.ino!!
    I tried!

    1. Run TimeTeensy3.ino. Teensy display right time and date! For ex. 21:02:34 14 4 2021 OK !

    2. I put setTime(0); into setup loop.

    Reload TimeTeensy3.ino. The serial monitor display 0:00:00 1 1 1970 OK!

    3. Removed setTime(0);
    Change time on my PC (for testing) For ex. 22:10:34 14 4 2021

    Reload TimeTeensy3.ino. and get time from point 1 (21:02...! Not ok! Time should be the PC time (22:10), not the old one...
    Last edited by 2n3055; 04-14-2021 at 06:28 PM.

  11. #11
    Senior Member
    Join Date
    Apr 2014
    Location
    Cheltenham, UK
    Posts
    301
    I think you are confused as to how the Teensy and RTC work.
    Look at this stream https://forum.pjrc.com/threads/63928...ght=teensy+rtc
    If you are using a Teensy 3.2 you have soldered a crystal to the board haven't you?
    Have you attached a battery to the teensy?

  12. #12
    Senior Member
    Join Date
    Oct 2019
    Location
    Calgary
    Posts
    121
    Out of curiosity I soldered a crystal and end header to my T3.2, hooked up a 3V coin cell and loaded the stripped down and modified version of the TimeTeensy3 sketch shown below. I removed all the time sync stuff (which is not needed to test the RTC and only serves to confuse things). I also set it to set the time back 1 hour 10 seconds after starting up.

    With the serial monitor open, after the initial load, the loader set the RTC to the compile time, which was displayed. After 10 seconds, the sketch set the time back an hour, and the changed time was also shown.

    Loading the sketch again, the time did not change back to the current time. It started up an hour back. After 10 seconds, the sketch set it back another hour, making it 2 hours behind.

    I removed the USB connection and the 3V coin cell, reattached the coin cell and plugged the USB back in. The sketch started up with the original compile time (now several minutes in the past). After 10 seconds, the sketch put it back another hour.

    Loading the sketch again, this time the loader set the RTC to the current time.

    Just to summarise what I observed:

    Once the loader had set the RTC, and with a 3V cell on Vbat, a subsequent load did not reload the RTC time until both USB power and Vbat were removed and reapplied.

    It appears with the T3.2 that if you want the loader to reset the RTC to the current time, you must remove USB power and Vbat, reconnect them, and then reload your sketch. I'd want to double check to be sure but I don't think that's how the T4.x does it; I'm pretty sure that the loader resets the RTC every time you load a sketch there, regardless of whether the RTC was previously set. Interesting!

    Click image for larger version. 

Name:	crystal.jpg 
Views:	16 
Size:	171.5 KB 
ID:	24450Click image for larger version. 

Name:	circuit.jpg 
Views:	19 
Size:	197.6 KB 
ID:	24451
    Code:
    #include <TimeLib.h>
    
    unsigned long changeInterval = Teensy3Clock.get() + 10; // 10 seconds
    bool changeTime = true;
    
    void setup()  {
      Serial.begin(115200);
      while (!Serial);  // Wait for Arduino Serial Monitor to open
      delay(100);
    }
    
    void loop() {
      digitalClockDisplay();  
      delay(1000);
      if( changeTime && Teensy3Clock.get() >= changeInterval ) {
        Serial.println("Setting time back 1 hour");
        Teensy3Clock.set(changeInterval - 3600); // set back 1 hour
        changeTime = false;
      }
    }
    
    void digitalClockDisplay() {
      // digital clock display of the time
      time_t now = Teensy3Clock.get(); // get the RTC
      Serial.print(hour(now));
      printDigits(minute(now));
      printDigits(second(now));
      Serial.print(" ");
      Serial.print(day(now));
      Serial.print(" ");
      Serial.print(month(now));
      Serial.print(" ");
      Serial.print(year(now)); 
      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);
    }

  13. #13
    Quote Originally Posted by BriComp View Post
    I think you are confused as to how the Teensy and RTC work.
    Probably because no crystal and no battery was installed!!!

  14. #14
    Silverlock , Thanks a lot for your tests !

    I tried your code but the time displayed doesn't change; probably because I don't use crystal nor battery !!!

    Using TimeTeensy3.ino (DC power from USB cable) I get:

    At the first download: "RTC has set the system time" 12:00:05 15.4.2021
    and seconds are increasing correctly!

    Remove the USB cable for a while (2-3 minutes).
    Plug the USB cable again and you get:
    "RTC has set the system time" 12:00:05 15.4.2021

    In fact, exactly the time of the first download, not actual time !!!

    As I said before I'm probably confused with the RTC (who need the Xtal 32kHz).

    Without the xtal, where comes the time ? And how to force to set a new time ?????

    PS same behavior with Teensy 3.6 (without battery)

  15. #15
    Maybe the solution could be to reset all the Teensy ; but how to do that ?

  16. #16
    No more idea to help me ?

    Thanks a lot

  17. #17
    Senior Member
    Join Date
    Apr 2014
    Location
    Cheltenham, UK
    Posts
    301
    This is my understanding of using time and RTC on Teensy.
    On the Teensy 3.1/3.2 if you want to use the RTC, then you must solder a crystal to the board.

    On any of the equipped RTC Teensies if you want to maintain the date/time when the Teensy is powered down then you must solder a battery to the Teensy.

    Assuming you are using the RTC then the following applies:-

    Basically there are two timekeeping systems on the teensy.
    The first which I will call teensyTime is running in the background whether you have an RTC attached or not.
    The other is RTCtime which is maintained by the RTC hardware.

    When the Teensy is first powered up, teensyTime is loaded with the date/time that the loaded program was compiled (or was it the time that the program was uploaded to the Teensy- @Paul clarification for me is required here).

    At this time there is NO LINK between teensyTime and RTCtime. Obviously if it was some time since the Teensy was originally programmed, the date/time will NOT be correct.

    In order to get the time from the RTC use the command getTeensy3Clock.get().
    The best way to link the two times is to use the command setSyncProvider(getTeensy3Time);
    getTeensy3Time uses getTeensy3Clock.get.

    All this is shown in the example TimeTeensy3.ino which is contained in the Teensy Time library (and below).
    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();
         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);
    }

  18. #18
    Quote Originally Posted by BriComp View Post
    This is my understanding of using time and RTC on Teensy.
    Assuming you are using the RTC then the following applies:-
    Thanks and according to your explanation I'm using only the "teensytime". No stall is installed.

    Quote Originally Posted by BriComp View Post
    All this is shown in the example TimeTeensy3.ino which is contained in the Teensy Time library (and below).
    I'm already using TimeTeensy3.ino.

    Now the question is how to setup the time of the "teensytime" when the sketch is downloaded ?

  19. #19
    Senior Member
    Join Date
    Apr 2014
    Location
    Cheltenham, UK
    Posts
    301
    Methods to set/adjust the time are:-
    Code:
    int hr, min, sec, dy, mnth, tr
    
        setTime(t);   //where t is in ms
    
    //or
    
        dy      = 25;
        mnth    = 4;
        yr      = 2021;
        hr      = 17;
        min     = 48;
        sec     = 0;
        setTime( hr, min, sec, dy, mnth, yr);
    
    // to add 1 hour to time use
    
        adjustTime( 3600 ); // 1 hour expressed in seconds

  20. #20
    I modified the example TimeTeensy3.ino by adding:

    Code:
     Serial.println("sync ok");
    Code:
    void loop() {
      if (Serial.available()) {
        time_t t = processSyncMessage();
        if (t != 0) {
          Serial.println("sync ok");
          Teensy3Clock.set(t); // set the RTC
          setTime(t);
        }
      }
      digitalClockDisplay();  
      delay(1000);
    }
    I always see: "RTC has set the system time" but never see "sync ok". The program goes never into this test ! Why ????

  21. #21
    Senior Member
    Join Date
    Apr 2014
    Location
    Cheltenham, UK
    Posts
    301
    Did you send any serial data to set the time to Teensy?
    If not
    Code:
    if (Serial.available()) {
    will not be true and you will not go any further.
    If you did send code and it could be decoded successfully then you would see "sync ok".
    Since you did not see "sync ok" you either did not send anything to the teensy or it was not decoded successfully.

  22. #22
    Quote Originally Posted by BriComp View Post
    Did you send any serial data to set the time to Teensy?
    If not
    Code:
    if (Serial.available()) {
    will not be true and you will not go any further.
    I'm sending nothing!!! But if you connect a new teensy download the example TimeTeensy3.ino, the time displayed on the terminal is correct.
    But it works only once.

    Does the IDE Arduino sending on the serial port time and date only once ???

    I'm fully lost with this features!!!!

  23. #23
    Senior Member
    Join Date
    Apr 2014
    Location
    Cheltenham, UK
    Posts
    301
    Each time a program is compiled and loaded to the teensy the date and time from the PC is embedded into the teensy SetUp/StartUp code.
    This code is run each time you apply power to your teensy. So, unless you have an RTC running, you will ALLWAYS get the SAME date/time.

    The program TimeTeensy3.ino is built to take an input from the USB Serial Port to set the computer date/time.

    I believe the input to be sent is in the form Tyr,mo,dy,hr,min,sec where T is a header to say that time set code follows. You WILL SEE this if you examine the program.

  24. #24
    Quote Originally Posted by BriComp View Post
    Each time a program is compiled and loaded to the teensy the date and time from the PC is embedded into the teensy SetUp/StartUp code.
    This code is run each time you apply power to your teensy. So, unless you have an RTC running, you will ALLWAYS get the SAME date/time.

    The program TimeTeensy3.ino is built to take an input from the USB Serial Port to set the computer date/time.
    .
    Thanks but how to set a new time at the next download ? Can we stop the RTC to reload the new time ???
    Thanks

  25. #25
    Senior Member
    Join Date
    Apr 2014
    Location
    Cheltenham, UK
    Posts
    301
    RTCtime will be correct if you have a crystal and battery installed. If Crystal and battery installed all you need to do is update teensyTime from RTCtime as in TimeTeensy3.ino.

    IF NO RTC is active then you must MANUALLY correct teensyTime each time you restart Teensy.

    The other alternative is to recompile the program and download to teensy each time.

Posting Permissions

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