Go Down

Topic: Analog input timer (Read 357 times) previous topic - next topic

trvslamm

small issue I know one of you can helo me out with very easily. If I want the code below to only work if the analogRead has been less than two for 30 seconds what do i do?
Code: [Select]
if (analogRead(0) < 2) {digitalWrite (LEDpinStat, HIGH);}

johnwasser

Code: [Select]

static unsigned long timerStart = 0;

if (analogRead(0) >= 2)
    timerStart = millis();

if (millis() - timerStart > 30000)
    digitalWrite (LEDpinStat, HIGH);   // A0 has been <2 for 30 seconds or more
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Coding Badly


What you are trying to do is essentially the same as debouncing a push-button.  This should get you started...

Code: [Select]
unsigned long Mark;

void loop( void )
{
 if (analogRead(0) >= 2)
 {
   Mark = millis();
 }

 if ( millis() - Mark >= 30000UL )
 {
   // Do your thing.
 }
}

Go Up