How to write a software interrupt?

I'm writing a simple a button to release a lock. If button is on, door will unlock, but if door is not opening in 5 seconds, the lock will be locked again. If door is open within 5 second, a LED will turn on immediately . My problem is I cannot turn on the LED immediately, I always need to wait 5 sec delay. Could anybody help? Thanks a lot.

if ( button_status == 1 )
  {
    Unlock ();
    if ( door_status == 1 )
    {
     LED_ON ();
     delay(1000);
     Lock ();
    }
    else
    {
     delay(5000);
     Lock();
    }
    if ( door_status == 1 )
    {
     LED_ON ();
     delay(1000);
     Lock ();
    }
    else
    {
    Lock();
    }
  }

Moderator edit: CODE TAGS

Get rid of the call to delay. Look at the blink without delay example for clues.

check the blink without delay example in the tutorial section, comparing the internal clock - millis() - against a certain periiod is a way to program without delay's and it will make your code much more reactive.

Thanks AWOL and Robtillaart, I'll check it.

Hi, all, I replace code to the following. But LED still cannot on immediately when door open, need to wait “interval” time. Could anybody help? Thanks a lot.

if ( button_status == 1 )
  {
    Unlock ();
    unsigned long currentTime = millis() ;
    previousTime = currentTime;
    while ( currentTime - previousTime < interval && door_status == 0 ) {
    currentTime = millis() ;  
    Serial.print("Time: ");
    Serial.println(currentTime - previousTime);
    }
   if ( door_status == 1 ) {
   LED_ON ();
   delay(1000);
   Lock ();
   }
   else {
    Lock();
   }
}

You did not understand the blink without delay example, then. All you have done is re-implement using a while loop.

Try again.

PaulS: Try again.

I've tried all night... Could you give more hints? As my understanding for the LED without delay, the code looks like: check time interval, if > 1000ms then change LED status.

Start with writing down the steps that YOU would follow to make whatever you want the Arduino to make happen. All you have is a watch and a pad of paper, in addition to the inputs and outputs.

Notice that the request arrived to unlock the door. Unlock the door and note the time. Go back to reading the paper, or watching TV, or whatever. Periodically, see if the door has been opened. If so, forget about it. Scratch out the time that the door was unlocked. Periodically, see if the door has been unlocked for 5 seconds, but not yet opened. If so, relock it, and scratch out the time.

If that is a fairly accurate list, then noticing things is what the Arduino is good at. Note the time is:

unsigned long when = millis();

Periodically is every pass through loop.

Given this much, you should be able to figure out how code the rest.

A much easier alternative to the messy blink without delay stuff is to use a simple timer.

See the mstimer2 library
http://arduino.cc/playground/Main/MsTimer2

You simply setup the timer to call your timer routine in setup.
Then start it when you are ready (like when the button is pressed instead of in setup() ),
then stop it when you don’t want it to call your timer routine anymore,
which might be in your timer routine if you only want it to be called once.

— bill

Thanks for all help… :slight_smile:
I just wondering why:

while ( currentTime - previousTime < interval && door_status == 0 )

is no use.
It looks like in the while loop, door_status cannot be monitored

Because nothing is changing the door status perhaps?
What code are you running now?