/*
Simple LED Toggle
*/
// pin constants
const int BTN_PIN = 8;
const int LED_PIN = 7;
// state variables
bool ledValue = false;
int oldBtnVal = HIGH; // INPUT_PULLUP idles HIGH
void setup() {
Serial.begin(9600);
pinMode(BTN_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
Serial.println("Push the button!\n");
}
void loop() {
int buttonValue = digitalRead(BTN_PIN);
if (buttonValue != oldBtnVal) { // button changed
oldBtnVal = buttonValue; // remember state
if (buttonValue == LOW) { // was just pressed
Serial.println("Button pressed");
ledValue = !ledValue; // invert current ledValue
} else {
Serial.println("Button released"); // was just released
}
delay(20); // short delay to debounce button
digitalWrite(LED_PIN, ledValue);
}
}