How to record time using Arduino

Hi guys,

I am trapped in a Arduino project, plz help me out.

I have a carbon monoxide sensor with Aruidno. Basically, I want to shutdown my generator when CO reaches a certain level plus continue for certain minutes. For example, the CO reaches xxx ppm and the generator waits for 5 mins to shut down.

I couldn't find a function in which Arduino can record time. In other words, I don't know how to write this 5 mins.

store "millis()" in a long variable (timerstart) when timing start. (and set a booloea flag that the event occured)

Then test: if (flag is set AND millis()>timerstart+manymilliseconds) stop_the_motor;

As a matter of interest though, would you continue to monitor the CO in the 5 minutes and cancel the shutdown if it goes back to a safe value?

That is a good idea.
..but: if CO is in critical consentraton.. don't

manor_royal:
As a matter of interest though, would you continue to monitor the CO in the 5 minutes and cancel the shutdown if it goes back to a safe value?

That's a good idea. My logic is : if CO is above 200 ppm, the timers starts to count and generator keeps running for 40 mins and shut down. If the CO level goes below 200 PPM, the timer stops count,but as soon as CO reaches 200 PPM again, the timer starts to count again. The idea is, as long as the timers reaches 40 mins, generator shuts down.

knut_ny:
That is a good idea.
..but: if CO is in critical consentraton.. don't

Yeah. I need a function to count 40 mins and then generator shuts down.

boolean prev_critical = false ;
unsigned long timestamp = 0L ;

void loop ()
{
  boolean critical = CO_critical () ;   // check for critical
  if (critical && ! prev_critical)
    timestamp = millis () ;     // when goes critical, record timestamp
  prev_critical = critical ;
  if (critical && millis() - timestamp >= 40*60*1000L)  // if still critical and 40mins have gone by, act
    shutdown () ;

  ... other stuff ..
}