Pages: [1]   Go Down
Author Topic: Analog input timer  (Read 341 times)
0 Members and 1 Guest are viewing this topic.
NC,US
Offline Offline
Newbie
*
Karma: 0
Posts: 42
@Weedar_64 #Farmlife
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
if (analogRead(0) < 2) {digitalWrite (LEDpinStat, HIGH);}
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8975
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13085
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Code:
unsigned long Mark;

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

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

Pages: [1]   Go Up
Jump to: