Go Down

Topic: Math calculations involving time - Code for count down timer (Read 190 times) previous topic - next topic


I'm working on my home automation code and wonder what the best way to do calculations involving date/time.

One of my HA is to press a button which turns on my garden path lights for 5 minutes and then turns them off. 

At first I thought of taking the current time adding 5 minutes, then testing to see when 5 minutes have past and turn them off.  But then I realized if I turn the lights on after 55 minutes past the hour I would have to do hour math.  And if it' just before midnight day hour math.

Easiest solution would be if Arduino have a command to convert year, month, day, hour minutes to an integer.  Then I could just do integer math.

Anyone know if such a command exists?  And what it is?  Or is there a better way?



Most people just use millis()

Some psuedocode below (this wont actually compile..)

Code: [Select]
unsigned long int button_on_time

void loop {
  if (button_pressed) {



  if (millis() > button_on_time+5*60*1000) {


Someone else suggested   unsigned long variables with the calculation of     currentTime - startTime >= timeOnInterval

This would work for longer periods of time and eliminate the problems of minutes wrapping if the button is pressed at the end of the hour.

Go Up