box-o-sand/oldstuff/arduino/msaqsg/ch03/MoreReliableSwitch/MoreReliableSwitch.ino
2015-06-22 13:15:42 -05:00

28 lines
576 B
C++

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;
}