Calculate elapsed days and hours

I'm new to the platform and been reading about the time library operations, can't figure out how to calculate the elapsed time in days/hours from two rtc.now() readings.

I store one value in an unsigned long variable and want to calculate the difference with a realtime reading

I see there are macros for doing this, but from the examples and my lack of knowledge in the language I cannot assemble the right routine to calculate it and serial print its value.

If you can point me to an example I'll be grateful.

Pablo

EEPROM.read() only reads ONE byte

use put() and get() to handle something more than 1 byte (and update())

EDIT: why did you modify your post while I'm answering.. examples are provided in the doc and with the class

J-M-L:
EDIT: why did you modify your post while I'm answering.. examples are provided in the doc and with the class

Sorry about that, was so ashamed of my stupid mistake that I repurposed the post intead of writing a new one with the other doubt I'm having (tried to delete it but found no way)

Nothing to be ashamed about; We don’t judge the persons here, just comment the code.

We all started somewhere and wrote stupid things… even today probably it still happens to most of us until we go “gee what was I thinking”…

Thanks!! Maybe you can help me with the edited question

for the record, this was the deleted question:

Hi, I was having problem retrieving a stored value from eeprom for my arduino uno project, it turns out, the value was never stored, here is the minimum code to reproduce my issue, im compiling with 1.89

#include <EEPROM.h>;

int modo;
const int loc=25; 
void setup() {
    Serial.begin(9600);
    modo = EEPROM.read(loc);
    Serial.print("read value: ");
    Serial.println(modo);
    modo=2;
    EEPROM.write(modo,loc); // edit this was my mistake… facepalm
    modo = EEPROM.read(loc);
    Serial.print("Read stored value: ");
    Serial.println(modo);
}
void loop() {
}




I get the same values before and after.

What am I doing wrong?

Thanks!

[edit] I may need a cup of tea.. I just noticed my error [facepalm] EEPROM.write(loc,modo);

the mistake is to believe this will read a full int    modo = EEPROM.read(loc); an int is two bytes on a UNO, and you only read one.

if you follow the links above, you have sample code for writing and reading data over multiple bytes

if you want to calculate elapsed days, hours etc.. you probably need a RTC on your system to get the information in the first place. Save the moment in the form of numbers of seconds since the epoch (unix time) for example, or since another point in time as a uint32_t, most RTC libraries do have a function to give you that unix time (and do math with it with a DateTime and TimeSpan class)

A month ago I knew nothing about Arduino C, in this month I managed to make work a LCD, an encoder with its button, a DHT22 sensor, a DS3231 RTC and this whole weekend I'm struggling trying to make this calculation using the proper libraries and not math.

Readed everything I found about datetime and timespan but I guess my problem is that I don't understand classes and C, my program works because its closer to BASIC than C...

I hacked my way to this point understanding examples, but found none to do something similar to what I want.

At this point I have the RTC readings and retrieved from EEPROM the moment I want to calculate the elapsed time from

I just don't know how to use these classes, maybe you can point me to a proper example.

Thanks!!

PS: My test bed and a whole menu system is working right now, I just need to fill the spaces next to Dias (days) Horas (Hours)

I realized that all the days of month and leap year calculations were already in the rtc.now() value, so its just math what I needed, with the help of a friend who programs C for a living we came to this simple solution:

void lapso(){
  unsigned long lap=ahora-modofecha; //ahora is rtc.now() modofecha is the start of the lapse
  lapsodias=lap/86400; //stores days
  lapsohoras=lap%86400/3600; // operates on the remainder to get the hours

}

can’t figure out how to calculate the elapsed time in days/hours from two rtc.now() readings.
I’m struggling trying to make this calculation using the proper libraries and not math.

What rtc library are you using. If the .now() expression is part of RTCLib.h which has a DS3231 class. There is also a TimeSpan class in that library. There is an other interesting library based on RTCLib.h which has the alarm functions included if you want them. RTClibExtended.h Its available through the library manager.

It’s easiest to work with the unix time stamp data returned by rtc.now.unixtime().
You subtract the earlier value from the later one and feed it to the TimeStamp

#include <Wire.h>
#include "RTClib.h"
//#include <RTClibExtended.h>

void setup() {
 Serial.begin(115200);
 uint32_t unixTime1 = 1234567;
 uint32_t unixTime2 = 2345678;
 TimeSpan ts1 = unixTime2-unixTime1;
 showTimeSpan(ts1);
}

void loop() {
 
}

void showTimeSpan(const TimeSpan& ts) {
    Serial.print(ts.days(), DEC);
    Serial.print(" days ");
    Serial.print(ts.hours(), DEC);
    Serial.print(" hours ");
    Serial.print(ts.minutes(), DEC);
    Serial.print(" minutes ");
    Serial.print(ts.seconds(), DEC);
    Serial.print(" seconds (");
    Serial.print(ts.totalseconds(), DEC);
    Serial.print(" total seconds)");
    Serial.println();
}

There is also a library example in RTCLib.h using the TimeSpan, called “datecalc” which uses other methods of input besides the unix time stamps.

TimeLib.h has everything you need.

I prefer linux time for elapsed time calculations (seconds since Jan. 1, 2000).