Teency RTC manually set time issue

Status
Not open for further replies.

philip.porhammer

Well-known member
I need to manually set the RTC on the Teency 3.6 as it is going to my used by non-tekies.

the .h file had this:
//void setTime(int hr,int min,int sec,int day, int month, int yr);
so I did this in the setup:
setTime(3,22,46,1,2,23);
hourFormat12();
it does not retain the manually set time.

the code:
/*
* TimeRTC.pde
* example code illustrating Time library with Real Time Clock.
*
*/



//void setTime(int hr,int min,int sec,int day, int month, int yr);

#include <TimeLib.h>
//int led = 13;
void setup() {
// pinMode(led, OUTPUT);

// 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) {digitalWrite(led, HIGH);//fail
//delay(500);
// digitalWrite(led, LOW);
//}
// else {digitalWrite(led, HIGH);//pass

// delay(500);
// digitalWrite(led, LOW);
// delay(500);
// digitalWrite(led, HIGH);}

//********************************I wouls think that I only need the next line to manualy set the time:
// h m s
setTime(3,22,46,1,2,23);
hourFormat12(); // the hour now in 12 hour format
}

void loop() {
// if (Serial.available()) {
// time_t t = processSyncMessage();
// if (t != 0) {
// Teensy3Clock.set(t); // set the RTC
// setTime(t);
// }
// }
digitalClockDisplay();
delay(100);
}

void digitalClockDisplay() {
// digital clock display of the time
Serial.print(" h: "); Serial.print(hour());
Serial.print(" M: "); Serial.print(minute());
Serial.print(" S: "); Serial.print(second());
// 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);
}

*/
 
C is not my native language, I am more of a LabView guy.

I am pulling this stuff from the TimLib.h and guessing as to how to work this to no avail.
void setTime(int hr,int min,int sec,int day, int month, int yr);
setTime(3,22,46,1,2,23);
void setTime(int hr,int min,int sec,int day, int month, int yr);
'int min' redeclared as different kind of symbol

I also don't underdant what's going on here:
if (Serial.available()) {
time_t t = processSyncMessage();
if (t != 0) {
Teensy3Clock.set(t); // set the RTC
setTime(t);

like this line: time_t t = processSyncMessage(); time_t is red but the following t is not and t in part of this line: Teensy3Clock.set(t); // set the RTC.


so this is a SWAG of how to make a one shot program that I will blend into other programs once I get the manual time setting program to work.

I will have time setting buttons on the back of the clocks.



int THour=3 ;
int TMinute=10;
int TSecond=22;

// from the timeLib.h
//void setTime(int hr,int min,int sec,int day, int month, int yr);
#include <TimeLib.h>
void setup() {
setSyncProvider(getTeensy3Time);
hourFormat12(); // this is in the TimeLib but not red, why?
}
void loop() {
// Teensy3Clock.set(); // no matching function for call to 'teensy3_clock_class::set(int, int, int, int, int, int)'
// no matching function for call to 'teensy3_clock_class::set()'

setTime(THour,TMinute,TSecond,1,2,23);
delay(100);
}

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

Real Code a.jpg
 

Attachments

  • Real Code b.jpg
    Real Code b.jpg
    107.2 KB · Views: 49
Here is a simple piece of code to set the RTC to 05:30:00 on 05 Mar 2021. Provided the RTC battery is connected, that time will be maintained even while the teensy is not powered.

Code:
	tmElements_t tmElements;

	// write new time values to rt clock
	tmElements.Second = 0;
	tmElements.Minute = 30
	tmElements.Hour = 5;
// not needed		tmElements.Wday;	// day of week, sunday is day 1
	tmElements.Day = 5
	tmElements.Month = 3
	tmElements.Year = CalendarYrToTm(2021);		// offset from 1970; 

	Teensy3Clock.set(makeTime(tmElements));

To retrieve the time from the RTC to use it in your code, simply call
Code:
	// read it back and set the software clock
	const time_t rtcTime = Teensy3Clock.get();
	setTime(rtcTime);
 
Also note that the Arduino timelib has no concept of timezone so you need to make allowances in your code. As a rule I ALWAYS set the RTC to UTC time and apply the appropriate adjustment for timezone when getting / setting the RTC
 
this clock is going to my parents (in there 80's ) so i wasn't thinking of Time zones. here is the full code. Thanks for your help, no way I could figure that out, want even close to what I was seeing in the documents.





int SetMinBtn=1;
int SetHourBtn=0;

int Second=0;
int IncMinutes=0;// set minutes button
int IncHours=1; // set hours button
int HourVal=0; ///hour();
int MinVal =0; //minute();
int SecVal =0; //second();
int i;
int j;
// from the timeLib.h

#include <TimeLib.h>
#include <SPI.h>


void setup() {

pinMode(SetMinBtn, INPUT_PULLUP);//
pinMode(SetHourBtn, INPUT_PULLUP);//
setSyncProvider(getTeensy3Time);

Serial.begin(9600);
}


void loop() {

i = digitalRead(SetMinBtn);
j = digitalRead(SetHourBtn);


if(i==0||j==0)
{
MinVal =minute();
HourVal=hour();
if(i==0) {MinVal++; if(MinVal>59) {MinVal=0;} }
if( j==0) {HourVal++; if(HourVal>12){HourVal=1;} }

tmElements_t tmElements;
tmElements.Second=0;
tmElements.Minute=MinVal;
tmElements.Hour=HourVal;

tmElements.Day=0;
tmElements.Month=0;
tmElements.Year=CalendarYrToTm(2021);

Teensy3Clock.set(makeTime(tmElements));
const time_t rtcTime = Teensy3Clock.get();
setTime(rtcTime);
}


Serial.print("hours:");Serial.print(hour());
Serial.print(" minutes:");Serial.print(minute());
Serial.print(" Seconds:");Serial.println(second());

delay(500);
}

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;
}
*/
 
Status
Not open for further replies.
Back
Top