Go Down

Topic: Analog input timer (Read 376 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
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy