Timer?

Project: when a predetermined time has been reached, a LED comes on. When a single-pole toggle switch has been operated various times (closed), once the time has reached 15 seconds a LED is activated. The switch can be closed 15 seconds or multiple times but once 15 seconds occurs, a LED comes on and then can be reset to zero again.
Is this possible and if so, where can I find the sketch?

Thank you,
John

You'll find it on your computer, after writing it and saving it on your HDD :slight_smile:

Yes, it's possible. Basic Arduino (C) programming is what you need. For time keeping look at the function "millis" in the Arduino reference. The example sketch "blink without delay" will teach you how to use it to perform an action once every X seconds or do something after X seconds.

Hello Sacha,

Thank you for the reply and read the analogy of cooking a pizza in a microwave oven for 10 minutes while doing something else.
My analogy is this: I place the pizza in the oven setting the timer for 10 minutes. Two minutes later my phone rings and I open the microwave door stopping the timer at 8 minutes. Five minutes later I close the microwave door and the microwave cooks the pizza for the remaining 8 minutes. About two minutes later I open the microwave door again stopping the microwave oven with 6 minutes remaining. Three minutes later I close the door pressing "start" again. The microwave timer cooks the pizza during the remaining six minutes and the oven shuts off.
Is using "blinking without delay" the program I need?

Blinking without delay is a good starting point. When you start with an interval of 10,000 ms and the starting time, check when that interval has passed, relative to the starting time. When the process is temporarily stopped, remember the remaining interval. When restarted, remember the new starting time and wait for the remaining interval to expire, relative to the new starting time.

Here is a beginning for your sketch:

unsigned long lastMillis = millis();
unsigned long seconds = 0;
boolean isOn = false;

#define BTN_PIN  10

void setup()
{
   // setup button;
   pinMode(BTN_PIN, INPUT);
}

void loop()
{
  // read button;
  isOn = digitalRead(BTN_PIN);

  if (millis() - lastMillis >= 1000)
  {
    if (isOn)
       seconds++;

    lastMillis += 1000; 
  }

  if (seconds >= 600)
  {
    // sound alarm;
  }

}

I find there are two approaches.

set timer for 10 minutes...
if something happens, stop that, start another timer...

I am making a home alarm to sense intrusion.
look for someone, set off the alarm for 20 minutes, then chirp each minture thereafter

I also have a key to activae/deactivate.

this uses a timer of how long the key is pressed.

for this I look to see if the key is pressed, and the first time it is pressed, create a global variable called duration

if(keyPressed=HIGH){
if(keyPresed=lastKeyPressed){
lastKeyPressed-keyPressed:
duration = millis():
}
}

now I have duration as that start time, I use that for all the next comparisons.
if 3 seconds , then do this
after 5 seconds, do that
after 11 seconds, do the other thing.

I found that it was easier to have one point in time and use that for the work done to 'see' what the key is doing.

Thank you Flor & Dave for the responses. Dave when I copied & pasted your sketch and clicked "compile" I got a error.