I want to run my Teensy 4.1 on UTC or GPST for an astronomy project. In case of problems with the GPS (like testing indoors) I want to fallback to the RTC and have attached a Battery to VBAT/GND as described. With the battery Teensy 4.1 keeps the time with some moderate accuracy also when turned off, which is ok for me.
But: whenever I load a new version of my sketch (arduino-IDE) the time is gone! It is set to some weird time which is similar to my wallclock time even including daylight saving!
This time is not fit for astronomical calculations, and I want to have my GPST back! This is the reason I have installed a battery in the first place.
I get the impression that there is a strange mechanism at work that resets the Teensy RTC time every time I press the button on the Teensy and everytime I upload a sketch.
Even in my case, when I have attached the battery all the time without power failure.
Below is a sketch to demonstrate this behavior: With this sketch I can change the RTC time by pressing 2 buttons.
After powering off and on the RTC has faithfully remembered the changed time, but when uploading the sketch again the time is lost!
Please help me! This is quite annoying during development. What can I do? Is there a way to tell the teensy to leave my RTC clock as it is?
Yours,
Johannes
But: whenever I load a new version of my sketch (arduino-IDE) the time is gone! It is set to some weird time which is similar to my wallclock time even including daylight saving!
This time is not fit for astronomical calculations, and I want to have my GPST back! This is the reason I have installed a battery in the first place.
I get the impression that there is a strange mechanism at work that resets the Teensy RTC time every time I press the button on the Teensy and everytime I upload a sketch.
Even in my case, when I have attached the battery all the time without power failure.
Below is a sketch to demonstrate this behavior: With this sketch I can change the RTC time by pressing 2 buttons.
After powering off and on the RTC has faithfully remembered the changed time, but when uploading the sketch again the time is lost!
Please help me! This is quite annoying during development. What can I do? Is there a way to tell the teensy to leave my RTC clock as it is?
Yours,
Johannes
Code:
#include <LiquidCrystal.h>
const int rs = 27, en = 28, d4 = 29, d5 = 30, d6 = 31, d7 = 32;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void DecomposeDay(int day,int &y,unsigned int &m,unsigned int &d) {
// day.. number of days since 1970.01.01
day += (5*(400*365+97) -(30*365+7) -(31+29));
// number of days since 0000.03.01
y = day / (400*365+97);
day -= (400*365+97) * y;
if (day < 0) {day += (400*365+97);--y;}
y *= 400;
{
const int c = day / (100*365+24);
y += 100 * c;
if (c > 3) {
m = 1;
d = 28;
return;
}
day -= (100*365+24) * c;
}
{
const int h = day / (4*365+1);
day -= (4*365+1) * h;
y += 4 * h;
}
{
const int h = day / 365;
y += h;
if (h > 3) {
m = 1;
d = 28;
return;
}
day -= 365 * h;
}
if (day < 31+30+31+30+31+31)
if (day < 31+30+31)
if (day < 31)
{m = 2; d = day;} // mar
else
if (day < 31+30)
{m = 3; d = day-31;} // apr
else
{m = 4; d = day-(31+30);} // may
else
if (day < 31+30+31+30)
{m = 5; d = day-(31+30+31);} // jun
else
if (day < 31+30+31+30+31)
{m = 6; d = day-(31+30+31+30);} // jul
else
{m = 7; d = day-(31+30+31+30+31);} // aug
else
if (day < 31+30+31+30+31+31+30+31+30)
if (day < 31+30+31+30+31+31+30)
{m = 8; d = day-(31+30+31+30+31+31);} // sep
else
if (day < 31+30+31+30+31+31+30+31)
{m = 9; d = day-(31+30+31+30+31+31+30);} // oct
else
{m = 10; d = day-(31+30+31+30+31+31+30+31);} // nov
else
if (day < 31+30+31+30+31+31+30+31+30+31)
{m = 11; d = day-(31+30+31+30+31+31+30+31+30);} // dec
else {
if (day < 31+30+31+30+31+31+30+31+30+31+31)
{m = 0; d = day-(31+30+31+30+31+31+30+31+30+31);} // jan
else
{m = 1; d = day-(31+30+31+30+31+31+30+31+30+31+31);} // feb
++y;
}
}
void Time2String(time_t unix_time, char buff[]) {
const int day = (unix_time / 86400) - ((unix_time < 0) ? 1 : 0);
int y;
unsigned int m,d;
DecomposeDay(day,y,m,d);
unsigned int s = unix_time - day*86400;
unsigned int t = s / 60; const unsigned int second = s - t*60;
s = t / 60; const unsigned int minute = t - s*60;
sprintf(buff,"%+05d%02u%02u_%02u%02u%02u",
y,m+1,d+1,s,minute,second);
}
const int pin_plus = 14;
const int pin_minus = 15;
void setup() {
lcd.begin(16, 2);
pinMode(pin_plus,INPUT_PULLUP);
pinMode(pin_minus,INPUT_PULLUP);
}
unsigned int pressed_plus = 0;
unsigned int pressed_minus = 0;
void loop() {
time_t t = Teensy3Clock.get();
unsigned int m = millis();
int offset = 0;
if (digitalRead(pin_plus) == LOW) {
if (pressed_plus == 0) {
pressed_plus = m;
offset = 1;
} else {
if (m > pressed_plus+1000) {
// long press
unsigned int h = (m-pressed_plus)/1000;
offset = h*h;
}
}
} else {
pressed_plus = 0;
}
if (digitalRead(pin_minus) == LOW) {
if (pressed_minus == 0) {
pressed_minus = m;
offset = -1;
} else {
if (m > pressed_minus+1000) {
// long press
unsigned int h = (m-pressed_minus)/1000;
offset = -h*h;
}
}
} else {
pressed_minus = 0;
}
if (offset) {
t += offset;
Teensy3Clock.set(t);
}
char buff[17];
Time2String(t,buff);
lcd.setCursor(0, 0);
lcd.print(buff);
delay(20);
}