From af850b2d9185ef105feee342d09917b017cfe865 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sun, 29 Dec 2013 22:34:12 -0500 Subject: [PATCH] Adding debounce button thingy and dice + button version --- arduino/DebounceButton/DebounceButton.ino | 28 ++++++++++++++++++++ arduino/DebounceButton/Makefile | 1 + arduino/DiceWithButton/DiceWithButton.ino | 32 +++++++++++++++++++++++ arduino/DiceWithButton/Makefile | 1 + 4 files changed, 62 insertions(+) create mode 100644 arduino/DebounceButton/DebounceButton.ino create mode 120000 arduino/DebounceButton/Makefile create mode 100644 arduino/DiceWithButton/DiceWithButton.ino create mode 120000 arduino/DiceWithButton/Makefile diff --git a/arduino/DebounceButton/DebounceButton.ino b/arduino/DebounceButton/DebounceButton.ino new file mode 100644 index 0000000..a5b1522 --- /dev/null +++ b/arduino/DebounceButton/DebounceButton.ino @@ -0,0 +1,28 @@ +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); + delay(50); + } + + oldButtonState = CURRENT_BUTTON_STATE; +} diff --git a/arduino/DebounceButton/Makefile b/arduino/DebounceButton/Makefile new file mode 120000 index 0000000..51c7f30 --- /dev/null +++ b/arduino/DebounceButton/Makefile @@ -0,0 +1 @@ +../arduino.mk \ No newline at end of file diff --git a/arduino/DiceWithButton/DiceWithButton.ino b/arduino/DiceWithButton/DiceWithButton.ino new file mode 100644 index 0000000..e3b6ff2 --- /dev/null +++ b/arduino/DiceWithButton/DiceWithButton.ino @@ -0,0 +1,32 @@ +const unsigned int LED_BIT0 = 10; +const unsigned int LED_BIT1 = 11; +const unsigned int LED_BIT2 = 12; +const unsigned int BUTTON_PIN = 7; + +int currentValue = 0; +int oldValue = 0; + +void outputResult(const long result) { + digitalWrite(LED_BIT0, result & B001); + digitalWrite(LED_BIT1, result & B010); + digitalWrite(LED_BIT2, result & B100); +} + +void setup() { + pinMode(LED_BIT0, OUTPUT); + pinMode(LED_BIT1, OUTPUT); + pinMode(LED_BIT2, OUTPUT); + pinMode(BUTTON_PIN, INPUT); + randomSeed(analogRead(A0)); +} + +void loop() { + currentValue = digitalRead(BUTTON_PIN); + + if (currentValue != oldValue && currentValue == HIGH) { + outputResult(random(1, 7)); + delay(50); + } + + oldValue = currentValue; +} diff --git a/arduino/DiceWithButton/Makefile b/arduino/DiceWithButton/Makefile new file mode 120000 index 0000000..51c7f30 --- /dev/null +++ b/arduino/DiceWithButton/Makefile @@ -0,0 +1 @@ +../arduino.mk \ No newline at end of file