Adding slightly more reliable switch solution
This commit is contained in:
parent
757deda43a
commit
244a3ee1a9
1
arduino/MoreReliableSwitch/Makefile
Symbolic link
1
arduino/MoreReliableSwitch/Makefile
Symbolic link
@ -0,0 +1 @@
|
||||
../arduino.mk
|
27
arduino/MoreReliableSwitch/MoreReliableSwitch.ino
Normal file
27
arduino/MoreReliableSwitch/MoreReliableSwitch.ino
Normal file
@ -0,0 +1,27 @@
|
||||
const unsigned int BUTTON_PIN = 7;
|
||||
const unsigned int LED_PIN = 13;
|
||||
|
||||
int oldButtonState = LOW;
|
||||
int ledState = LOW;
|
||||
|
||||
void setup() {
|
||||
pinMode(LED_PIN, OUTPUT);
|
||||
pinMode(BUTTON_PIN, INPUT);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
const int CURRENT_BUTTON_STATE = digitalRead(BUTTON_PIN);
|
||||
|
||||
if (CURRENT_BUTTON_STATE != oldButtonState &&
|
||||
CURRENT_BUTTON_STATE == HIGH) {
|
||||
if (ledState == LOW) {
|
||||
ledState = HIGH;
|
||||
} else {
|
||||
ledState = LOW;
|
||||
}
|
||||
|
||||
digitalWrite(LED_PIN, ledState);
|
||||
}
|
||||
|
||||
oldButtonState = CURRENT_BUTTON_STATE;
|
||||
}
|
Loading…
Reference in New Issue
Block a user