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

Thread: Teensy 4.0 RTC

  1. #1
    Junior Member
    Join Date
    May 2020
    Posts
    3

    Teensy 4.0 RTC

    Iím using the Teensy 4.0 RTC, and I have some strange issue, with keeping the time, the problem is as follows: Iím setting the time using the serial interface (view code), and the Teensy is displaying it correctly, up until now everything is okÖ. But as Iím disconnecting the power from the Teensy and reconnecting it, it is losing one hour, the minutes and the seconds are ok, it is only the hour segment that is missing the time, and it is always losing one hour. Does anyone have any idea?

    Click image for larger version. 

Name:	Teensy 4.0 rtc.jpg 
Views:	5 
Size:	81.6 KB 
ID:	20376

    Arduino code:

    #include <TimeLib.h> //Teensy time lib

    #define led 14
    #define display Serial1 // nextion display

    int long blink_time; // blinking led
    int long time_display_cycle;

    void setup() {
    Serial.begin(115200); Serial.setTimeout(50);
    display.begin(115200);
    setSyncProvider(getTeensy3Time);
    pinMode(led, OUTPUT);
    Serial.println("- = Program Starts = - ");
    }

    void loop() {
    led_blink(100); // blinking the LED connected on pin 14
    display_clock();
    set_clock();
    }

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

    void led_blink(int t) {
    if (millis() - blink_time >= t) {
    digitalWrite(led, !digitalRead(led));
    blink_time = millis();
    }
    }

    void set_clock() { //setting the time using the serial interface, typ 1 for entering time setting mode
    if (Serial.available() > 0) {
    int data_type;
    data_type = Serial.parseInt();
    if (data_type == 1) {// 1-->clock setting
    Serial.println(Serial.parseInt());
    Serial.println(" -= Time Setting =- ");
    //=============
    Serial.print("Enter Hour :.....");
    while (Serial.available() == 0) {
    }
    int Hour = int (Serial.parseInt());
    Serial.println(Hour);
    //=============
    Serial.print("Enter Minutes:.....");
    while (Serial.available() == 0) {
    }
    int Minute = int (Serial.parseInt());
    Serial.println(Minute);
    //=============
    Serial.print("Day Of Month :.....");
    while (Serial.available() == 0) {
    }
    int Day_month = int (Serial.parseInt());
    Serial.println(Day_month);
    //=============
    Serial.print("Month :.....");
    while (Serial.available() == 0) {
    }
    int Month = int (Serial.parseInt());
    Serial.println(Month);
    //=============
    Serial.print("Year :.....");
    while (Serial.available() == 0) {
    }
    int Year = int (Serial.parseInt());
    Serial.println(Year);
    //=============
    // hours, minutes, seconds, days, months, years
    setTime(Hour, Minute, 0, Day_month, Month, Year);
    }
    else {
    Serial.println("");
    Serial.println("- = Unknown data type = -");
    }
    Serial.println("================================== ==");
    time_display_cycle = 0;
    display_clock();
    }
    }

    void display_clock() {
    if (millis() - time_display_cycle >= 1000) { // display the time once a second
    String _minute;
    String _second;
    String Day;

    if (second() < 10) {
    _second = "0" + String(second());
    }
    else {
    _second = String(second());
    }

    if (minute() < 10) {
    _minute = "0" + String(minute());
    }
    else {
    _minute = String(minute());
    }

    switch (weekday()) {
    case 1:
    Day = "Sun";
    break;
    case 2:
    Day = "Mon";
    break;
    case 3:
    Day = "Tue";
    break;
    case 4:
    Day = "Wed";
    break;
    case 5:
    Day = "Thu";
    break;
    case 6:
    Day = "Fri";
    break;
    case 7:
    Day = "Sat";
    break;
    }

    String txt = String(day()) + "/" + String(month()) + "/" + String(year()) + " " +
    String(hour()) + ":" + _minute + " " + Day;
    Serial.println(txt);

    String txt_hmi = String(hour()) + ":" + _minute + ":" + _second;
    display.print("t0.txt="); // update nextion display
    display.write(0x22);
    display.print(txt_hmi);
    display.write(0x22);
    display.write(0xff);
    display.write(0xff);
    display.write(0xff);

    time_display_cycle = millis();
    }
    }

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    22,110
    Which version of Teensy Loader are you using. In the small Teensy window, click Help > About (or Teensy > About if using Macintosh) to see the version.

    A bug with daylight savings time was fixed in version 1.52. If you have an older version, update to 1.52.

  3. #3
    Junior Member
    Join Date
    May 2020
    Posts
    3
    That was super quick, I updated to the 1.52, and it runs perfect, thanks.

Posting Permissions

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