Go Down

Topic: how can I stop ( readMechine)counter after 10sec or may earlier say 5 - Thx (Read 605 times) previous topic - next topic

hemant

how I stop ( readMechine)counter after 10sec or may earlier say 5 sec. if no input .
Thanks

Code: [Select]
int machinePin = 3;    // input pin
int reading;      // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin
int readMechine=0; //number of times microsw was pressed
int ledPin=13;     // visual light
int audioPin = 12; // buzzer

long time = 0;    // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers
void setup()
{
  pinMode(machinePin, INPUT);
  pinMode(ledPin,OUTPUT);
  pinMode(audioPin, OUTPUT);
}
void loop()
{
 
  reading = digitalRead(machinePin);

 
  if (reading == HIGH && previous == LOW && millis() - time > debounce) 
{
    //increment count
    readMechine++;
   // ... and remember when the last button press was
    time = millis();   
  }
  previous = reading;
   if (readMechine==3)
{   
   
   digitalWrite(ledPin,HIGH);
}
   if (readMechine==5)

   digitalWrite(audioPin,HIGH);
   
    readMechine=0; //reset count
   
}
}




CrossRoads

Do a similar thing as you have here

if (reading == HIGH && previous == LOW && millis() - time > debounce)

as an outer loop around the whole thing.
Store the time when you want to start the inactivity time, set a flag in the inner part that stays clear (0) if nothing happened, or is set (1) if something did), then check if flag is clear and time elapsed.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Go Up