1306Views10Replies

Author Options:

Arduino programming help: Do two things at once! Answered

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. . .

voidloop()
{
  
 pirstate = digitalRead(pir);
  
 if (pirstate == HIGH)
  {
    digitalWrite(LED, HIGH);
    digitalWrite(relay, HIGH);
    delay(pulse);
  }
 
 else
  {
    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!


10 Replies

user
lewisb42 (author)2013-05-08

You should probably re-post your code so I can see how you implemented it.

Select as Best AnswerUndo Best Answer

user
HavocRC (author)lewisb422013-05-08

Hey lewis I the code is working great! ( I had made a mistake in my timing) So, that really helps! The only problem is that I don't know what is happening. Could you explain please?

Thank you!


Select as Best AnswerUndo Best Answer

user
lewisb42 (author)HavocRC2013-05-08

The key here is realizing the millis() function basically tells you "what time is it right now?" Technically it is the number of milliseconds (1/1000 of a second) that have elapsed since your turned the Arduino on. It's a number that just keeps going up and up (except it rolls over to zero and starts again somewhere around day 50).

So what you're doing is keeping track of the most-recent time A0 went high and continually checking to see if 5 minutes have elapsed since then. If A0 goes high before those 5 minutes are up, start the clock over. If the clock runs out, turn the LED off.

Select as Best AnswerUndo Best Answer

user
HavocRC (author)lewisb422013-05-08

Well I kinda guessed that, but what about the code part? What's what? Which tracks the 5 min etc.?

Select as Best AnswerUndo Best Answer

user
lewisb42 (author)HavocRC2013-05-08

My bad. Here's my original pseudocode, with more comments:

// this is the variable that "remembers" the last A0 high.
unsigned long lastHigh = 0;

loop()
{

// this if-statement checks if the current time (millis()) is at least 5 minutes (300000 milliseconds) past when lastHigh was set
if(millis() - lastHigh > 300000)
{
turn LED off
}

// check if A0 is high. if it is, lastHigh gets updates to the current time, even if the 5 minutes aren't yet up
// note that turning on an already-on LED is ok -- it basically means nothing changes
if(pin is HIGH)
{
turn LED on
lastHigh = millis() // lastHigh is reset to the time the LED was turned on
}
}

Select as Best AnswerUndo Best Answer

user
HavocRC (author)lewisb422013-05-08

A bit hazy, but I get it.

Thanks!

Select as Best AnswerUndo Best Answer

user
lewisb42 (author)2013-05-08

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

unsigned long lastHigh = 0;

loop()
{
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.

Select as Best AnswerUndo Best Answer

user
caitlinsdad (author)2013-05-07

So where in the code are you checking for the elapsed time? It's looping and looping....

Select as Best AnswerUndo Best Answer