Arduino programming help: Do two things at once!

So I'm making a new project but I ran up into a little problem with the code.
So this is what I want:

Arduino check to see if A0 is HIGH,
If so, turn on LED for 5 minutes.
If, during any period within the 5 minutes, if pin A0 goes HIGH again, then start the 5 minutes all over again. 
So basically every time pin A0 is HIGH no matter when always start a new 5 min timer.

This is what I had. . .

void loop()
 pirstate = digitalRead(pir);
 if (pirstate == HIGH)
    digitalWrite(LED, HIGH);
    digitalWrite(relay, HIGH);
    digitalWrite(LED, LOW);
    digitalWrite(relay, LOW);

The only problem with the above code is that it doesn't do anything while waiting the 'pulse' time. I need it to be able to within 'pulse' time check again to see if the pin is HIGH, and if it is, start all over again with 'pulse' seconds.

Thanks guys!

Your help is invaluable!


read this, will help you

sspence3 years ago
lewisb423 years ago
You have to realize that delay() pauses EVERYTHING. My approach would look something like this:

unsigned long lastHigh = 0;

if(millis() - lastHigh > 300000) //  Note: 300000ms == 5 min
turn LED off

if(pin is HIGH)
turn LED on
lastHigh = millis() // reset the 5 minute countdown

Make sure your timer variable is of type "unsigned long".  If you use int you're going to get weird behavior.
caitlinsdad3 years ago
So where in the code are you checking for the elapsed time? It's looping and looping....