I’m have a RTC and I want to make a countdown timer. The time remaining will be displayed on an LCD matrix in hh:mm:ss format. I’m having trouble figuring out the number of seconds from now to the countdown time. I apparently lacking in some understanding of how makeTime(), and epochtime() work.
My RTC is set with the right time, I ran one of the example sketches that outputs the time and it was right. My intent is to set my countdown time using makeTime(), then subtract the now.unixtime(). I thought this would give me the number of seconds until I reached the countdown, but I’m getting numbers that are way too big.
Initially I was setting tm.Year = 2013, then I read it should be 2013 - 1900, but that didn’t fix it. I’m also not sure if tm.Month is 11 for December or 12. Neither way got things working.
Below is my test sketch
#include <Wire.h>
#include "RTClib.h"
#include "Time.h"
RTC_DS1307 RTC;
uint32_t refreshTimer;
tmElements_t tm;
void setup() {
Serial.begin(9600);
Wire.begin();
RTC.begin();
refreshTimer = millis();
}
void loop() {
time_t nextMooverTime;
uint32_t countdownSec;
if ((long) (millis() - refreshTimer) > 0 )
{
DateTime now = RTC.now();
tm.Second = 0;
tm.Hour = 10;
tm.Minute = 30;
tm.Day = 7;
tm.Month = 12;
tm.Year = 2013 - 1900;
nextMooverTime = makeTime(tm);
countdownSec = nextMooverTime - now.unixtime();
Serial.print(nextMooverTime);
Serial.print("\t");
Serial.print(countdownSec);
Serial.print("\t");
Serial.print(now.unixtime());
Serial.println();
// at 8:55 AM EST on 12/6/13, I get this
// 3595401000 2209080988 1386320012
// if I change month to 11, I get this
// 3592809000 2206488929 1386320071
// change year to 2013 instead of 2013 - 1900
// 2708568104 1322247995 1386320109
refreshTimer = millis() + 1000; // update display every 1/2 second
}
}