2
2
Debounce
3
3
4
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).
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).
8
7
9
8
The circuit:
10
9
- LED attached from pin 13 to ground
11
10
- pushbutton attached from pin 2 to +5V
12
11
- 10 kilohm resistor attached from pin 2 to ground
13
12
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.
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.
16
15
17
16
created 21 Nov 2006
18
17
by David A. Mellis
28
27
http://www.arduino.cc/en/Tutorial/Debounce
29
28
*/
30
29
31
- // constants won't change. They're used here to
32
- // set pin numbers:
30
+ // constants won't change. They're used here to set pin numbers:
33
31
const int buttonPin = 2 ; // the number of the pushbutton pin
34
32
const int ledPin = 13 ; // the number of the LED pin
35
33
@@ -38,8 +36,8 @@ int ledState = HIGH; // the current state of the output pin
38
36
int buttonState; // the current reading from the input pin
39
37
int lastButtonState = LOW; // the previous reading from the input pin
40
38
41
- // the following variables are unsigned longs because the time, measured in milliseconds,
42
- // will quickly become a bigger number than can be stored in an int.
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.
43
41
unsigned long lastDebounceTime = 0 ; // the last time the output pin was toggled
44
42
unsigned long debounceDelay = 50 ; // the debounce time; increase if the output flickers
45
43
@@ -56,8 +54,8 @@ void loop() {
56
54
int reading = digitalRead (buttonPin);
57
55
58
56
// check to see if you just pressed the button
59
- // (i.e. the input went from LOW to HIGH), and you've waited
60
- // long enough since the last press to ignore any noise:
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:
61
59
62
60
// If the switch changed, due to noise or pressing:
63
61
if (reading != lastButtonState) {
@@ -66,8 +64,8 @@ void loop() {
66
64
}
67
65
68
66
if ((millis () - lastDebounceTime) > debounceDelay) {
69
- // whatever the reading is at, it's been there for longer
70
- // than the debounce delay, so take it as the actual current state:
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:
71
69
72
70
// if the button state has changed:
73
71
if (reading != buttonState) {
@@ -83,7 +81,6 @@ void loop() {
83
81
// set the LED:
84
82
digitalWrite (ledPin, ledState);
85
83
86
- // save the reading. Next time through the loop,
87
- // it'll be the lastButtonState:
84
+ // save the reading. Next time through the loop, it'll be the lastButtonState:
88
85
lastButtonState = reading;
89
86
}
0 commit comments