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