File tree 1 file changed +18
-4
lines changed
build/shared/examples/02.Digital/Debounce
1 file changed +18
-4
lines changed Original file line number Diff line number Diff line change 19
19
by David A. Mellis
20
20
modified 30 Aug 2011
21
21
by Limor Fried
22
+ modified 28 Dec 2012
23
+ by Mike Walters
22
24
23
25
This example code is in the public domain.
24
26
@@ -43,6 +45,9 @@ long debounceDelay = 50; // the debounce time; increase if the output flicker
43
45
void setup () {
44
46
pinMode (buttonPin, INPUT);
45
47
pinMode (ledPin, OUTPUT);
48
+
49
+ // set initial LED state
50
+ digitalWrite (ledPin, ledState);
46
51
}
47
52
48
53
void loop () {
@@ -62,12 +67,21 @@ void loop() {
62
67
if ((millis () - lastDebounceTime) > debounceDelay) {
63
68
// whatever the reading is at, it's been there for longer
64
69
// than the debounce delay, so take it as the actual current state:
65
- buttonState = reading;
70
+
71
+ // if the button state has changed:
72
+ if (reading != buttonState) {
73
+ buttonState = reading;
74
+
75
+ // only toggle the LED if the new button state is HIGH
76
+ if (buttonState == HIGH) {
77
+ ledState = !ledState;
78
+
79
+ // set the LED:
80
+ digitalWrite (ledPin, ledState);
81
+ }
82
+ }
66
83
}
67
84
68
- // set the LED using the state of the button:
69
- digitalWrite (ledPin, buttonState);
70
-
71
85
// save the reading. Next time through the loop,
72
86
// it'll be the lastButtonState:
73
87
lastButtonState = reading;
You can’t perform that action at this time.
0 commit comments