How to add 150 minutes to actual time [SOLVED]

I need to disable a product for 2,5 hours after a certain moment and want to show the end time on the display.

I use the RTClib.h libraray and can read the actual time (now.hour() and now.minute()).
How can I add in a simple way 150 minutes to this time (only for presentation purposes).

My RTC has to run undisturbed further.
I can make the interruption routine with compairing mills().

Get the current time as Epoch Time, then add 9000 seconds to it. Then, display it as human-friendly day:hour:minute:second.

Store the current hours and minutes.
Keep checking hours.now and hours.minutes until 2 hours and 30 minutes have passed.
Just like watching a clock on the wall.

Untested, before morning coffee...

futureHour = hour()+2;
futureMinute = minute() + 30;
if minute > 30
{
++futureHour
}

Thanx gentlemen for your suggestions.
I will use the solution of gfvalvo.

ArduinoStarter1:
Thanx gentlemen for your suggestions.
I will use the solution of gfvalvo.

It's the most bulletproof approach.

RTClib.h offers two helper classes on top of reading time: DateTime to stores date and time information and TimeSpan to represent duration / changes in time with seconds accuracy

whilst not rocket science to do, it can help make your code easy to read as you stay within high level constructs of the library.

build a date time when the code needs to stop that is 2h30 ahead

DateTime stopTime = rtc.now();
DateTime StopTimePlusTwoHoursAndThirtyMinutes = stopTime + TimeSpan(0, 2, 30, 0);

as you loop, you can check when rtc.now() becomes greater than StopTimePlusTwoHoursAndThirtyMinutes you'll be done

DateTime now = rtc.now();
if (now > StopTimePlusTwoHoursAndThirtyMinutes) {
  // time's up
  ...
} else {
  // be patient
  ...
}

Because object now is different from method now(), I prefer to name the object as nowTime.

DateTime nowTime = rtc.now();

fair point, I prefer maintenant :slight_smile:

The compiler does not get confused though (and the example codes always use now, hence I used it here)

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.