timer question

Here's my situation. I want to start a timer using an input, after roughly 5 min I want the timer to time out and set an output high. Is this possible and if so how would I go about doing it? thx

Here’s my situation. I want to start a timer using an input

The Arduino has a number of timers. You don’t want to use one for something as trivial as this, though.

Suppose someone called you, and said be out front in 5 minutes, and I’ll pick you up. You wouldn’t start a timer, would you? Of course not. You’d look at your watch, and note what time it was. Periodically, you’d check again, to see of enough time had elapsed. If it had, you’d go outside. If not, you’d keep doing whatever it was you were doing.

The Arduino has a watch - millis(). It has a memory - global or static variables. It has a “periodically” - every pass through loop.

unsigned long phoneRang;
unsigned long waitTime = 5000UL * 60UL;

void loop()
{
   if(digitalRead(somePin) == HIGH)
      phoneRang = millis();

   if(millis() - phoneRang > waitTime)
     goOutside();
}

The only difficulty with your requirement is this:

after roughly 5 min

The Arduino can do things at exactly the right time (+/- a few nanoseconds). It has no concept of “roughly”.

The Arduino can do things at exactly the right time (+/- a few nanoseconds). It has no concept of "roughly".

Nope, the Uno has an accuracy of about 0.3%, since it uses a ceramic resonator as its clock source... Earlier Arduinos used a quartz crystal with, say, about 0.003% accuracy, which is about 10 milliseconds in 5 minutes, 7 orders of magnitude more rough than "(+/- a few nanoseconds)".

The Uno would be expected to be accurate to about 1 second in 5 minutes, 9 orders of magnitude more rough than "(+/- a few nanoseconds)".

The Arduino can do things at exactly the right time (+/- a few nanoseconds). It has no concept of "roughly".

but you can code it :slight_smile:

unsigned long roughly(uint32_t x)
{ 
  #define ROUGHLYFACTOR 10
  uint32_t y = x/ROUGHLYFACTOR;
  return x + random(y) - random(y); 
}

I think you need to recognize that you not waiting already too, & not reset your start time:

time_running = 0;
void loop()
{
if( (digitalRead(somePin) == HIGH) && (time_running == 0) )
{phoneRang = millis();
time_running = 1; // wating for time to go by
digitalWrite(someOutputpin, HIGH);}

if(millis() - phoneRang > waitTime)
{ goOutside();
digitalWrite (someOutputpin, LOW);
time_running = 0;} // done wating, allow next time period to start with somePin going High
}

I actually want to reset the time, every time the pin goes high. I'm trying to close a garage door based on no movement within a certain amount of time. So I'm using a motion detector and every time it picks up some motion I want the time to start over. If that makes any sense.

Taking out the time_running stuff should let that happen then, then the 30 seconds will reset every time.

ok, so everything is working fine now. But I do have a question. What does the UL after 5000 and 60 mean?

UL unsigned long (32 bit number, value from 0 to 2^32-1 (FFFFFFFF)

ah, ok thx