From 244a3ee1a9d987e52ad12b3cc86ea9e96b617c4b Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sun, 29 Dec 2013 22:22:50 -0500 Subject: [PATCH] Adding slightly more reliable switch solution --- arduino/MoreReliableSwitch/Makefile | 1 + .../MoreReliableSwitch/MoreReliableSwitch.ino | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 120000 arduino/MoreReliableSwitch/Makefile create mode 100644 arduino/MoreReliableSwitch/MoreReliableSwitch.ino diff --git a/arduino/MoreReliableSwitch/Makefile b/arduino/MoreReliableSwitch/Makefile new file mode 120000 index 0000000..51c7f30 --- /dev/null +++ b/arduino/MoreReliableSwitch/Makefile @@ -0,0 +1 @@ +../arduino.mk \ No newline at end of file diff --git a/arduino/MoreReliableSwitch/MoreReliableSwitch.ino b/arduino/MoreReliableSwitch/MoreReliableSwitch.ino new file mode 100644 index 0000000..2b67b72 --- /dev/null +++ b/arduino/MoreReliableSwitch/MoreReliableSwitch.ino @@ -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; +}