const int BTN_PIN = 14;
const int LED_PIN = 32;
bool ledState = false;
int lastBtnState = HIGH;
void checkButton() {
int btnState = digitalRead(BTN_PIN); // read the button
if (lastBtnState != btnState) { // if it changed
lastBtnState = btnState; // save the state
if (btnState == LOW) { // if LOW was just pressed
Serial.println("The button was just pressed");
ledState = !ledState; // toggle LED state
digitalWrite(LED_PIN, ledState); // write LED state
} else { // if HIGH was just released
Serial.println("The button was just released");
}
delay(20); // debounce the button
}
}
void setup() {
Serial.begin(115200);
pinMode(BTN_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
Serial.println("\nReady!\n");
}
void loop() {
checkButton();
}