I have been the proud owner of a arduino uno for 3 days now and my first program is almost running now. But i seem to have a problem that i don't understand.
My challenge is te sound a alarm when a door is open for longer than 5 seconds, so i wrote the following program.
The problem is you're telling it that when the button first goes HIGH, sit and do nothing for 5 seconds. Then turn on "relais" and immediately check again. If it's still high, sit there for another 5 seconds and check again. Beyond that, if the sensor is read as HIGH for even a split second, then the alarm will sound.
The correct way to do this is to use millis() to record the time you last saw the input as HIGH. Then in every loop, you should check if it's been over some interval and sound the alarm. Take a look at the Blink Without Delay example to see how millis() and interval checking is done.
A similar code has been tested in Processing. If the reed pin is HIGH for 5 seconds or more, the relais pin turns HIGH. If the reed pin is LOW, the relais pin will be LOW. The temp variable [flag] is the prevent prev from resetting while the reed pin is high. Does this produce the desired effect?