mirror of
https://github.com/arduino/Arduino.git
synced 2025-01-18 07:52:14 +01:00
Merge pull request #1192 from miek/debounce-fix
Fix Debounce example to work as described
This commit is contained in:
commit
97e0f34572
@ -19,6 +19,8 @@
|
|||||||
by David A. Mellis
|
by David A. Mellis
|
||||||
modified 30 Aug 2011
|
modified 30 Aug 2011
|
||||||
by Limor Fried
|
by Limor Fried
|
||||||
|
modified 28 Dec 2012
|
||||||
|
by Mike Walters
|
||||||
|
|
||||||
This example code is in the public domain.
|
This example code is in the public domain.
|
||||||
|
|
||||||
@ -43,6 +45,9 @@ long debounceDelay = 50; // the debounce time; increase if the output flicker
|
|||||||
void setup() {
|
void setup() {
|
||||||
pinMode(buttonPin, INPUT);
|
pinMode(buttonPin, INPUT);
|
||||||
pinMode(ledPin, OUTPUT);
|
pinMode(ledPin, OUTPUT);
|
||||||
|
|
||||||
|
// set initial LED state
|
||||||
|
digitalWrite(ledPin, ledState);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
@ -62,11 +67,20 @@ void loop() {
|
|||||||
if ((millis() - lastDebounceTime) > debounceDelay) {
|
if ((millis() - lastDebounceTime) > debounceDelay) {
|
||||||
// whatever the reading is at, it's been there for longer
|
// whatever the reading is at, it's been there for longer
|
||||||
// than the debounce delay, so take it as the actual current state:
|
// than the debounce delay, so take it as the actual current state:
|
||||||
buttonState = reading;
|
|
||||||
}
|
|
||||||
|
|
||||||
// set the LED using the state of the button:
|
// if the button state has changed:
|
||||||
digitalWrite(ledPin, buttonState);
|
if (reading != buttonState) {
|
||||||
|
buttonState = reading;
|
||||||
|
|
||||||
|
// only toggle the LED if the new button state is HIGH
|
||||||
|
if (buttonState == HIGH) {
|
||||||
|
ledState = !ledState;
|
||||||
|
|
||||||
|
// set the LED:
|
||||||
|
digitalWrite(ledPin, ledState);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// save the reading. Next time through the loop,
|
// save the reading. Next time through the loop,
|
||||||
// it'll be the lastButtonState:
|
// it'll be the lastButtonState:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user