/*
Simple "state change detection" button
*/
const int BTN_PIN = 2;
int oldBtnState = 1; // pin idles HIGH with pullup
void checkButton() {
int btnState = digitalRead(BTN_PIN); // read the pin
if (btnState != oldBtnState) { // if it changed
oldBtnState = btnState; // remember the state
if (btnState == LOW) { // was just pressed
Serial.println("Button Pressed");
} else { // was just released
Serial.println("Button Released");
}
delay(20); // short delay to debounce button
}
}
void setup() {
Serial.begin(115200);
pinMode(BTN_PIN, INPUT_PULLUP);
}
void loop() {
checkButton();
}