Hi.Forum.
Using RTC Lib and Adafruit PCF8523 shield.
I need to know the time difference between two button presses also after days.
So first press I invoke in global variables a function that stores the timestamp.
void CatchTime() {
DateTime Start = rtc.now();
anno = Start.year();
mese = Start.month();
giorno = Start.day();
ora = Start.hour();
minuto = Start.minute();
secondo = Start.second();
Then at second button press I invoke the function passed:
void ElapsedTime() {
DateTime passed = (rtc.now() - TimeSpan(giorno, ora, minuto, secondo));
But guess what?
Pushing two buttons at the same day the
Serial.print(passed.day(), DEC); command instead of giving me 0Days gives me 31Days..
How could it be possible?
Best regards and thanks
Any help appreciated
As accurate as possible in the second domain.
I mean considering the accuracy of the RTC just the calculation of two timestamps.
First stamp: Y1M1D1-H1M1S1 store it in a variable
Second stamp: actual YMD-HMS store it
Subtract and calculate how many months days, etc have passed.
why are you asking that?
Thank You
As UKHeliBob says, use unixtime to record the events
RTClib.h makes it simple in that there is a function now.unixtime() which returns the seconds since 1970 from the year/day/hour/minute/second values stored in the rtc registers and returned to the Arduino with the call to rtc.now().
The reason for the days difference reported as 31 is that you are misusing the TimeSpan object
TimeSpan(giorno, ora, minuto, secondo) is just a number of seconds.
Review of the RTClib library example datecalc.ino shows how to get a timespan from two datetime objects and how to print it.
However, you will find if more simple to just work directly with now.unixtime().
You can extract hours,minutes and seconds from a difference between two time values in seconds like something like this this
delta_time = endTime - startTime; //delta_time from now.unixtime() is in seconds
deltaHours = (deltaTime/ 3600); //extract hours with integer math
deltaMinutes = (delta_time % 3600) / 60; //extract minutes with modulus and integer math
deltaSeconds = deltaTime % 60; //extract seconds with modulus