Using Timer Interrupt to Stop Execution - Arduino Mega 2560

Hi all,

I would like to execute a certain set of commands (e.g., turning off a relay) in my Arduino program when a certain time elapses (e.g., 20 minutes) from the moment when the program starts execution as a safety mechanism.

What is the easiest way to do this?

I read about Timers in Arduino Mega but I think 20 minutes is too long to use timers. I saw that there is External Real-Time Clock modules (like DS3231 AT24C32 IIC High Precision Real Time Clock Module).

Any suggestions would be appreciated.

Thanks

20 minutes is not too long, I use things like that all the time. 20*60*1000

Just use millis(). Note that using millis() for timing relies on the fact that your current code is written in a non-blocking way.

You will have to give a more detailed description of your needs to get a more detailed reply. If it's from the moment that the Arduino is reset/powered, you can use below

if( millis() > 20UL * 60 * 1000)
{
  // time limit reached
}

what does the UL do in that example?

Standard arithmetic is 16 bit; the L tells the compiler to treat it as a long and as a result the multiplication will result in a long and not be truncated. The U indicates unsigned.

sterretje: Standard arithmetic is 16 bit; the L tells the compiler to treat it as a long and as a result the multiplication will result in a long and not be truncated. The U indicates unsigned.

Thanks, will try to remember that. makes the whole expression an unsigned long. I get it.

tajeldin: I would like to execute a certain set of commands (e.g., turning off a relay) in my Arduino program when a certain time elapses (e.g., 20 minutes) from the moment when the program starts execution as a safety mechanism.

It's unclear what you mean by "safety mechanism". But, if there's the possibility of bodily injury or property damage should that mechanism fail, then I would not trust its implementation to amateur-written code running on hobbyist-quality hardware.

gfvalvo: It's unclear what you mean by "safety mechanism". But, if there's the possibility of bodily injury or property damage should that mechanism fail, then I would not trust its implementation to amateur-written code running on hobbyist-quality hardware.

I guarantee exactly that happens every day. Scary isn't it.