RTC hour displays wrong time.

Status
Not open for further replies.

Dellyjoe

Active member
Good Afternoon everyone I have been researching this issue for a little while now about three days and I can't figure out why but the RTC on the Teensy 3.6 displays everything correctly other then the hour time.


I'm new to programming and I'm having a lot of fun. I normally like to figure things out for myself but i'm having a hard time with this issue

Current time is 13:37 and the RTC on the teensy 3.6 shows this time below.

17:37:42: 24/3/2019

Also here is my github for the project if this helps https://github.com/Dellyjoe/RTC_Light_Timer


I'm really just using the example code so i'm not sure what is really wrong.

Code:
#include <Arduino.h>
#include <rtc.h>

//******************************************Setup*****************************//
void intrtc()
{
  // set the Time library to use Teensy 3.0's RTC to keep time
  setSyncProvider(getTeensy3Time);

  Serial.begin(115200); // Setting up the Serial Terminal
  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");
  }
}
//******************************************Setup*****************************//
//******************************************Main******************************//
void displaytime()
{
  intrtc();
  if (Serial.available())
  {
    time_t t = processSyncMessage();
    if (t != 0)
    {
      Teensy3Clock.set(t); // set the RTC
      setTime(t);
    }
  }
  digitalClockDisplay();
  //delay(1000);
}
//******************************************Main******************************//
//*****************************************Functions**************************//
void digitalClockDisplay()
{
  // digital clock display of the time
  Serial.print(hour());
  Serial.print(":");
  Serial.print(minute());
  Serial.print(":");
  Serial.print(second());
  Serial.print(": ");
  Serial.print(day());
  Serial.print("/");
  Serial.print(month());
  Serial.print("/");
  Serial.print(year());
  Serial.println();
}

time_t getTeensy3Time()
{
  return Teensy3Clock.get();
}
unsigned long processSyncMessage()
{
  unsigned long pctime = 0L;
  const unsigned long DEFAULT_TIME = 1553130740; // March 20, 2019 9:12:20 PM

  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;
} //*****************************************Functions**************************//
 
hmm ok

I'm UTC -5 hours which is 18000 seconds.

If I use adjustTime(number); which would look like adjustTime(-18000); where in the code would I put this?
 
Hey everyone, I'm still having a hard time getting the time adjust function to work. Can you not use //adjustTime(offset * SECS_PER_HOUR); when using time_t getTeensy3Time()?

All I'm trying to do is set the time to EST. Right now it has the right mins and sec but the hours are off. The date works great.


Code:
#include <TimeLib.h>   // Teensy 3.0's RTC
#include <DS1307RTC.h> // a basic DS1307 library that returns time as a time_t
#include <Wire.h>

time_t getTeensy3Time();
unsigned long processSyncMessage();
/*  code to process time sync messages from the serial port   */

// single character message tags
#define TIME_HEADER "T"   // Header tag for serial time sync message
#define FORMAT_HEADER 'F' // Header tag indicating a date format message
#define FORMAT_SHORT 's'  // short month and day strings
#define FORMAT_LONG 'l'   // (lower case l) long month and day strings

#define TIME_REQUEST 7 // ASCII bell character requests a time sync message

//const int offset = -5;  // Eastern Standard Time (USA)
const int offset = -4; // Eastern Daylight Time (USA)

void intrtc();
void digitalClockDisplay();
void displaytime();
void printDigits(int digits);
Code:
#include <rtc.h>

//*****************************************Functions**************************//
void intrtc()
{
  // set the Time library to use Teensy 3.0's RTC to keep time
  setSyncProvider(getTeensy3Time);

  Serial.begin(115200); // Setting up the Serial Terminal
}

void displaytime()
{
  intrtc();
  if (Serial.available())
  {
    time_t t = processSyncMessage();
    if (t != 0)
    {
      Teensy3Clock.set(t); // set the RTC
      setTime(t);
      //adjustTime(offset * SECS_PER_HOUR);
    }
  }
  digitalClockDisplay();
  //delay(1000);
}
//*****************************************Functions**************************//
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);
}


time_t getTeensy3Time()
{
  return Teensy3Clock.get();
}
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;
} //*****************************************Functions**************************//
 
All I'm trying to do is set the time to EST. Right now it has the right mins and sec but the hours are off. The date works great.
Why not simply setting RTC time in EST? Or do you have reasons to run RTC in UTC? Also what means "hours are off"? can you not give an example?
 
Are you actually sending a message to that program running on your Teensy, to properly set the date/time? Or just going with the "default" setting Teensy has?

If you're going with the default, maybe check your PC's date & time setting? Also make sure you have a coin cell connected to VBAT, if you remove power from your Teensy. The default is based on the time your code was compiled by Arduino, so if your PC's time is incorrect, then you'll get similar error with the default on Teensy. If Teensy loses power, and the coin cell is dead or not connected, when you power up again the time will revert to that default based on when Arduino compiled your code.

If you are indeed sending that time set message, maybe there's some issue with the way you're creating the message to send? We're pretty good at helping find and solve such problems, but only when we can clearly see what you're really doing (actual code, screenshots, specific steps used, etc). In this case, I can't even tell if you're transmitting the time set message that example expects, or just going with the default time based on when Arduino compiled your program.
 
Good Evening My PaulStoffregen:

One I will say I'm really new to programming, so these questions you are asking I might not be able to ask

Q1/ Are you actually sending a message to that program running on your Teensy, to properly set the date/time? Or just going with the "default" setting Teensy has?
A1/ Yes I'm using the miniUSB to set the time which I believe is getting it from my computer time but it is off by 4 hours every time

Q2/If you're going with the default, maybe check your PC's date & time setting?
A2/ Not sure if i'm using default but i'm using my PC clock time,

If Teensy loses power, and the coin cell is dead or not connected, when you power up again the time will revert to that default based on when Arduino compiled your code.
A3/ Yes I understand, right now I'm trying to just get the right time displayed on the Terminal

Here is the github to the whole project if you would like to look at it, if not let me know I will display all my .cpp/.h/main
https://github.com/Dellyjoe/RTC_Light_Timer

Thank you for your time in advanced !

Joe
 
Why not simply setting RTC time in EST? Or do you have reasons to run RTC in UTC? Also what means "hours are off"? can you not give an example?

Good Evening WMXZ:

Not sure If I can set time to EST, b/c the code pulls from my computer time which is 20:48:13 3/4/2019 but the RTC in the terminal displays --> 0:48:13: 3/4/2019
 
Perhaps this example code will steer you in the right direction?
Code:
/*
   TimeRTC.pde
   example code illustrating Time library with Real Time Clock.

*/

// deprecated API or MS WIN issue?
// #include <Time.h> // include Arduino Time library

#include <TimeLib.h> // include Arduino Time library

// **********************************************************************************
// *
// *                            Power Up Init.
// *                            put your setup code here, to run once:
// *
// **********************************************************************************
void setup()  {
  setSyncProvider(getTeensy3Time); // Set the Arduino Time library to Sync from Teensy 3.x's RTC
  // The default interval for re-syncing the time is 5 minutes but can be changed by calling the setSyncInterval(interval)
  setSyncInterval(60); // 1 minute / Configure how often the getTimeFunction is called in seconds.
  setTime(getTeensy3Time()); // Set Arduino Time library with Teensy 3.x RTC time.

  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");
  }
  oneTime_set_option(); //One Time Set Option
}

// **********************************************************************************
// *
// *                            Main Loop
// *                            put your main code here, to run repeatedly:
// *
// **********************************************************************************
void loop() {
  USBtime_set_option(); //Check for a time message from your computer, USB time set option using your computer's time.

  digitalClockDisplay(); //Display Clock

  // put your main code here, to run repeatedly:
  delay(1000); //

}

// **********************************************************************************
// *
// *                            One Time Set Option
// *
// **********************************************************************************
//  my variabls for time
int myHr, myMin, mySec, myDay, myMonth, myYr;
boolean oneTime = false; // <---- set me true

void oneTime_set_option() {

  if (oneTime == true) {
    oneTime = false;
    Serial.println("Setting new time");
    myHr = 14; // 2 pm
    myMin = 1;
    mySec = 1;
    myDay = 3;
    myMonth = 4;
    myYr = 2019;

    // This sets the system time: Arduino Time library (NOT the Teensy RTC Clock)
    // set your seperated date/time variables out as normal and update system time FIRST
    //setTime(hour(), minute(), second(), day(), month(), year())
    setTime(myHr, myMin, mySec, myDay, myMonth, myYr);

    // 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 :)
    Teensy3Clock.set(now());
  }
}

// **********************************************************************************
// *
// *                   Display Digital Clock
// *
// *
// **********************************************************************************
void digitalClockDisplay() {
  // digital clock display of the time
  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(), Serial.println();

  // Unix epoch
  Serial.print(" Unix ~ Arduino Time library    "); //
  Serial.println(now());
  Serial.print(" Unix ~ Teensy 3.x internal RTC "); //
  Serial.println(Teensy3Clock.get());

  Serial.println();
}

// **********************************************************************************
// *
// *  utility function for digital clock display: prints preceding colon and leading 0
// *
// **********************************************************************************
void printDigits(int digits) {
  Serial.print(":");
  if (digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

// **********************************************************************************
// *
// *  Read Teensy 3.x time from Teensy 3.x internal RTC
// *
// **********************************************************************************
time_t getTeensy3Time()
{
  return Teensy3Clock.get();
}

// **********************************************************************************
// *              USB time set option using your computer's time.
// *              TimeSerial listens for a message from your computer.
// *              This message is sent by a Processing-based program,
// *              found in Time's examples/Processing folder.
// **********************************************************************************
void USBtime_set_option() {
  if (Serial.available()) {
    time_t t = processSyncMessage();
    if (t != 0) {
      Teensy3Clock.set(t); // set the internal RTC in teensy
      setTime(t); // set the Arduino Time library
    }
  }
}

// **********************************************************************************
// *
// *  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;
}




// Set time using your computer's time and Processing software:
// The Processing software is free and open source, and runs on the Mac, Windows, and GNU/Linux platforms.
// Download Processing from https://processing.org/

/*
   SyncArduinoClock.

   portIndex must be set to the port connected to the Arduino

   The current time is sent in response to request message from Arduino
   or by clicking the display window

   The time message is 11 ASCII text characters; a header (the letter 'T')
   followed by the ten digit system time (unix time)
*/

/*

  import processing.serial.*;
  import java.util.Date;
  import java.util.Calendar;
  import java.util.GregorianCalendar;

  public static final short portIndex = 0;  // select the com port, 0 is the first port
  public static final String TIME_HEADER = "T"; //header for arduino serial time message
  public static final char TIME_REQUEST = 7;  // ASCII bell character
  public static final char LF = 10;     // ASCII linefeed
  public static final char CR = 13;     // ASCII linefeed
  Serial myPort;     // Create object from Serial class

  void setup() {
  size(200, 200);
  println(Serial.list());
  println(" Connecting to -> " + Serial.list()[portIndex]);
  myPort = new Serial(this,Serial.list()[portIndex], 9600);
  println(getTimeNow());
  }

  void draw()
  {
  textSize(20);
  textAlign(CENTER);
  fill(0);
  text("Click to send\nTime Sync", 0, 75, 200, 175);
  if ( myPort.available() > 0) {  // If data is available,
    char val = char(myPort.read());         // read it and store it in val
    if(val == TIME_REQUEST){
       long t = getTimeNow();
       sendTimeMessage(TIME_HEADER, t);
    }
    else
    {
       if(val == LF)
           ; //igonore
       else if(val == CR)
         println();
       else
         print(val); // echo everying but time request
    }
  }
  }

  void mousePressed() {
  sendTimeMessage( TIME_HEADER, getTimeNow());
  }


  void sendTimeMessage(String header, long time) {
  String timeStr = String.valueOf(time);
  myPort.write(header);  // send header and time to arduino
  myPort.write(timeStr);
  myPort.write('\n');
  }

  long getTimeNow(){
  // java time is in ms, we want secs
  Date d = new Date();
  Calendar cal = new GregorianCalendar();
  long current = d.getTime()/1000;
  long timezone = cal.get(cal.ZONE_OFFSET)/1000;
  long daylight = cal.get(cal.DST_OFFSET)/1000;
  return current + timezone + daylight;
  }

*/
 
Status
Not open for further replies.
Back
Top