Newbee question about Set time example TimeTeensy3

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.
I have never tried this version.

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

The other alternative is to recompile the program and download to teensy each time.
I tried to recompile and download and worked only once!
 
Tried with Teensy 3.2 (no Xtal) AND Teensy 3.6 (Xtal factory installed ):

1: download TimeTeensy3.ino on the serial monitor you get the time displayed. If you remove the power and set the power again the time restart at the same time after download.

2. added: setTime(0,0,0,1,10,21); into the setup:

Code:
void setup()  {
  // set the Time library to use Teensy 3.0's RTC to keep time
  delay(1000);
  
  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");
  }
  setTime(0,0,0,1,10,21);
}

Just after download you get 0:00:00 1 10 2021
it correct!

Let's running for a while...
Capture d’écran 2021-05-01 à 17.57.05.jpg

After 5 minutes you get the old time again!!!!!


Exactly the same behavior with Teensy 3.2 and 3.6

Why is it so difficult to set the time????
 
I think the easiest way to get you to understand what is happening is to ask you
what
Code:
  setSyncProvider(getTeensy3Time);
does?
 
2. added: setTime(0,0,0,1,10,21); into the setup:
I think you are confused how this Tme Library works. See Time Library: https://www.pjrc.com/teensy/td_libs_Time.html
It's just basically software clock implementation with easy to use commands, Internally, Time depends upon Arduino's millis() function to keep track to elasped time. However, Time can synchronize to several types of hardware which provide time and date information.

DS1307 Real Time Clock Chip
GPS Module
Ethernet-based Network Time Protocol
Teensy 3.x Real Time Clock

Code:
int Hr;
int Min;
int Sec;
int Dy;
int Mnth;
int Yr;

    Serial.println("Seting Arduino Time library now");
    Dy      = 1;
    Mnth    = 5;
    Yr      = 2021;
    Hr      = 21;
    Min     = 20;
    Sec     = 0;
    [COLOR="#FF0000"]setTime( Hr, Min, Sec, Dy, Mnth, Yr); // Set Arduino Time library.[/COLOR]

    Serial.println("Seting the Teensy RTC Clock from Arduino Time library");
    // Now lets set the RTC in Teensy before exiting.
    // Now we can use the system time to update the Teensy's RTC bits
    // This sets the Teensy RTC Clock from Arduino Time library(system time) - epoch stylee, just like it wants :)
    [COLOR="#FF0000"]Teensy3Clock.set(now());[/COLOR]
    Serial.println();
And here is the whole CODE you can play with this:
Code:
#include <TimeLib.h>  // https://www.pjrc.com/teensy/td_libs_Time.html

elapsedMillis sincePrint;
int ledState = LOW;             // ledState used to set the LED
// int Led = LED_BUILTIN; //13;    // Teensy 3.x

int Hr;
int Min;
int Sec;
int Dy;
int Mnth;
int Yr;

// **********************************************************************************
// *                   setup ()
// **********************************************************************************
void setup() {
  // put your setup code here, to run once:
  pinMode(LED_BUILTIN, OUTPUT);

  setSyncInterval(59); // 1 minute / Configure how often the getTimeFunction is called in seconds.

  Serial.begin(115200); // Init the serial port only if needed
  // NOTE: On Teensy Serial.begin(baud) is completely ignored.
  // Unlike a standard Arduino (FTDI IC), the Teensy 3.x Serial object always communicates at 12 Mbit/sec USB speed.
  // Unlike a standard Arduino (FTDI IC), the Teensy 4.x Serial object always communicates at 480 Mbit/sec USB 2.0 Hi-Speed.
  while (!Serial && millis() < 2000) {}  // wait for Serial Monitor with timeout 2 sec.

  if (timeStatus() != timeSet) {
    Serial.println("Unable to sync with the RTC");

    Serial.println("Seting Arduino Time library now");
    Dy      = 1;
    Mnth    = 5;
    Yr      = 2021;
    Hr      = 21;
    Min     = 20;
    Sec     = 0;
    [COLOR="#FF0000"]setTime( Hr, Min, Sec, Dy, Mnth, Yr);[/COLOR] // Set Arduino Time library.

    Serial.println("Seting the Teensy RTC Clock from Arduino Time library");
    // Now lets set the RTC in Teensy before exiting.
    // Now we can use the system time to update the Teensy's RTC bits
    // This sets the Teensy RTC Clock from Arduino Time library(system time) - epoch stylee, just like it wants :)
    [COLOR="#FF0000"]Teensy3Clock.set(now());[/COLOR]
    Serial.println();
  } else {
    Serial.println("RTC has set the system time");
    Serial.println();
  }
}

// **********************************************************************************
// *                   loop ()
// **********************************************************************************
void loop() {
  // put your main code here, to run repeatedly:
  DisplayTime();
}

// **********************************************************************************
// *                   DisplayTime ()
// **********************************************************************************
void DisplayTime() {
  if (sincePrint >= 1000) {      // "sincePrint" auto-increases
    sincePrint = 0;
    //digitalToggle(13); // Teensyduino 1.53 AND UP
    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
      Serial.print("Teensy Unix time (also known as Epoch time)  UTC: ");
      Serial.println(Teensy3Clock.get());
      digitalClockDisplay(); // Arduino Time library
      Serial.println();
    } else {
      ledState = LOW;
    }
    // set the LED with the ledState of the variable:
    digitalWriteFast(LED_BUILTIN, ledState);
  }
}

// **********************************************************************************
// *                   Display Digital Clock
// **********************************************************************************
void digitalClockDisplay() {
  // digital clock display of the time
  Serial.print("12h: ");
  Serial.print(hourFormat12());    // the hour now in 12 hour format
  printDigits(minute());
  printDigits(second());
  if (isAM() == true) {
    Serial.print(".AM");
  }
  else {
    Serial.print(".PM");
  }
  Serial.println();

  if (hour() < 10) {
    Serial.print('0');
  }
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());

  //isAM();            // Returns true if time now is AM
  //isPM();            // Returns true if time now is PM
  if (isAM() == true) {
    Serial.print(".AM");
  }
  else {
    Serial.print(".PM");
  }

  Serial.print(" "); // space
  switch (weekday()) // Day of the week, Sunday is day 1
  {
    case 1:
      Serial.print("Sunday");
      break;
    case 2:
      Serial.print("Monday");
      break;
    case 3:
      Serial.print("Tuesday");
      break;
    case 4:
      Serial.print("Wednesday");
      break;
    case 5:
      Serial.print("Thursday");
      break;
    case 6:
      Serial.print("Friday");
      break;
    case 7:
      Serial.print("Saturday");
      break;
  } //end of switch case
  Serial.print(" "); // space
  Serial.print(day());
  Serial.print(" "); // space
  Serial.print(month());
  Serial.print(" "); // space
  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);
}
 
Back
Top