|
| 1 | +/* |
| 2 | + Debounce |
| 3 | + |
| 4 | + Each time the input pin goes from LOW to HIGH (e.g. because of a push-button |
| 5 | + press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There's |
| 6 | + a minimum delay between toggles to debounce the circuit (i.e. to ignore |
| 7 | + noise). |
| 8 | + |
| 9 | + The circuit: |
| 10 | + * LED attached from pin 13 to ground |
| 11 | + * pushbutton attached from pin 2 to +5V |
| 12 | + * 10K resistor attached from pin 2 to ground |
| 13 | + |
| 14 | + * Note: On most Arduino boards, there is already an LED on the board |
| 15 | + connected to pin 13, so you don't need any extra components for this example. |
| 16 | + |
| 17 | + |
| 18 | + created 21 November 2006 |
| 19 | + by David A. Mellis |
| 20 | + modified 30 Aug 2011 |
| 21 | + by Limor Fried |
| 22 | + |
| 23 | +This example code is in the public domain. |
| 24 | + |
| 25 | + http://www.arduino.cc/en/Tutorial/Debounce |
| 26 | + */ |
| 27 | + |
| 28 | +// constants won't change. They're used here to |
| 29 | +// set pin numbers: |
| 30 | +const int buttonPin = 2; // the number of the pushbutton pin |
| 31 | +const int ledPin = 13; // the number of the LED pin |
| 32 | + |
| 33 | +// Variables will change: |
| 34 | +int ledState = HIGH; // the current state of the output pin |
| 35 | +int buttonState; // the current reading from the input pin |
| 36 | +int lastButtonState = LOW; // the previous reading from the input pin |
| 37 | + |
| 38 | +// the following variables are long's because the time, measured in miliseconds, |
| 39 | +// will quickly become a bigger number than can be stored in an int. |
| 40 | +long lastDebounceTime = 0; // the last time the output pin was toggled |
| 41 | +long debounceDelay = 50; // the debounce time; increase if the output flickers |
| 42 | + |
| 43 | +void setup() { |
| 44 | + pinMode(buttonPin, INPUT); |
| 45 | + pinMode(ledPin, OUTPUT); |
| 46 | +} |
| 47 | + |
| 48 | +void loop() { |
| 49 | + // read the state of the switch into a local variable: |
| 50 | + int reading = digitalRead(buttonPin); |
| 51 | + |
| 52 | + // check to see if you just pressed the button |
| 53 | + // (i.e. the input went from LOW to HIGH), and you've waited |
| 54 | + // long enough since the last press to ignore any noise: |
| 55 | + |
| 56 | + // If the switch changed, due to noise or pressing: |
| 57 | + if (reading != lastButtonState) { |
| 58 | + // reset the debouncing timer |
| 59 | + lastDebounceTime = millis(); |
| 60 | + } |
| 61 | + |
| 62 | + if ((millis() - lastDebounceTime) > debounceDelay) { |
| 63 | + // whatever the reading is at, it's been there for longer |
| 64 | + // than the debounce delay, so take it as the actual current state: |
| 65 | + buttonState = reading; |
| 66 | + } |
| 67 | + |
| 68 | + // set the LED using the state of the button: |
| 69 | + digitalWrite(ledPin, buttonState); |
| 70 | + |
| 71 | + // save the reading. Next time through the loop, |
| 72 | + // it'll be the lastButtonState: |
| 73 | + lastButtonState = reading; |
| 74 | +} |
| 75 | + |
0 commit comments