I'm writing a code for the Arduino at the moment to control the electromagnetic locks on the doors in my office, i have set the Arduino up on a circut that will receive infomation from an input (in this case a keypad) that will tell it to open the doors (by setting the output to the lock high). The code i have so far i think is pretty sweet, but my boss wants a time delay on the locks so that when the output goes high it continues to stay that way for approx. 5 seconds so that the lock is open for that time. i've been messing about for ages trying to work out how to do this. this is my current code without the time delay:

my boss just got back to me.... he said he didn't want to use delay as the timer . thanks so much for taking the time to write that it worked the way we wanted it to as well, just not for multiple locks he said. Please anymore suggestions?

Is your boss asking you to do this as a learn-to-program exercise or does he really just want the answer?

I think it would be more rewarding for you to figure out the solution for yourself, so I'll just try to point you in the right direction:

1) your loop() function is a loop. Your program consists of loop() executed over and over again.

2) you can have loop carry out different functions based on how much time has elapsed by using if statements. Your loop() just consists of a series of these time-based if statements so that the right events happen at the right time. This in effect simulates a multi-threaded application in which you can deal with multiple events in parallel. Using delay() blocks processor execution and forces serial event-handling. Using

if (current time >= time for event 1){ do_event_1();}

Allows processor execution to continue while it's not time for event 1, so if it becomes time for other events, it's able to run them.

3) millis() returns the number of milliseconds that have elapsed since the last reset or power-up. You can use this as your timer source.

4) What you're really looking for here is a finite-state machine. You should use global variables to record what state you are currently in so that you know which step to next take in your main loop. You might need multiple state variables, especially if you have multiple events going on in parallel.

knowing bill it probably is a learning excersise (though he doesn't know much about the Arduino himself from what i gather), but i dont know anything much about code (i know the basics) and he has thrown me in at the deep end. but i have come up with this awesome (i reckon) peice of code thanks to you:

but now he wants me to seperate some devices (the analog inputs are actually monitoring 3 sets of push buttons and fingerprint/keypad things that are on the same circut because they connect to the same door) that are on the same circut in that peice of code, i'll post it once its done.thanks guys you've helped a bunch.