/* * LatchButton * This implements latching behavior on a pushbutton. * The state of the input changes only when the pushbutton * has been pressed and then released. While simple in concept, * implementing a latch becomes complicated due to switch bounce. * This sketch modifies the provided sketch on the Arduino webpage found at * http://www.arduino.cc/en/Tutorial/Debounce and modifies it for easier use * in other code. */ int buttonPin = 4; int currState; int prevState = LOW; long time = 0; int latchState = LOW; void setup() { setupLatch(); } void loop() // run over and over again { checkLatch(); } void setupLatch(){ Serial.begin(9600); pinMode(4, INPUT); } void checkLatch(){ currState = digitalRead(buttonPin); if(currState ==HIGH &&prevState==LOW &&abs(millis()-time)>50){ if(latchState == HIGH) latchState =LOW; else latchState = HIGH; time = millis(); Serial.println(latchState); } prevState = currState; }