#include "config.h"
// State variables to track button behavior [cite: 64-66, 77]
bool lastRawState = HIGH;
bool stableState = HIGH;
unsigned long lastChangeTime = 0;
bool ledState = false;
void setup() {
Serial.begin(SERIAL_BAUD); [cite: 58]
pinMode(BUTTON_PIN, INPUT_PULLUP); // Use internal pull-up [cite: 27, 38]
pinMode(LED_PIN, OUTPUT); [cite: 38]
// Initial boot message [cite: 92]
Serial.println(FIRMWARE_NAME);
Serial.print("[BOOT] PIN-");
Serial.println(BUTTON_PIN);
}
void loop() {
bool raw = digitalRead(BUTTON_PIN); [cite: 69]
// If the button state just changed, reset the timer [cite: 72, 119]
if (raw != lastRawState) {
lastChangeTime = millis();
lastRawState = raw;
}
// Only act if the state has been stable for 30ms [cite: 50, 71]
if ((millis() - lastChangeTime) >= DEBOUNCE_MS) {
// Check if the stable state is different from the previous one [cite: 71]
if (raw != stableState) {
bool prevStable = stableState;
stableState = raw;
// Falling Edge Detection: Trigger only when pressed (HIGH to LOW) [cite: 78, 82]
if (prevStable == HIGH && stableState == LOW) {
ledState = !ledState;
digitalWrite(LED_PIN, ledState); [cite: 83]
// Log the event to Serial Monitor [cite: 109]
Serial.print("[PRESS] LED -> ");
Serial.println(ledState ? "ON" : "OFF");
}
}
}
}