const int BUTTON_PIN = 2;
// ---- Job A: keep-alive ----
const unsigned long ALIVE_INTERVAL_MS = 2000;
unsigned long lastAlive = 0;
// ---- Job B: debounced press detection ----
const unsigned long DEBOUNCE_MS = 50;
int lastReading = HIGH;
int stableState = HIGH;
unsigned long lastChangeTime = 0;
void setup() {
Serial.begin(115200);
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop() {
// ---- Job A: print "System Alive" every 2 seconds ----
if (millis() - lastAlive >= ALIVE_INTERVAL_MS) {
lastAlive = millis();
Serial.println("System Alive");
}
// ---- Job B: print "Button pressed" once per debounced press ----
int reading = digitalRead(BUTTON_PIN);
if (reading != lastReading) {
lastChangeTime = millis();
}
if (millis() - lastChangeTime >= DEBOUNCE_MS) {
if (reading != stableState) {
stableState = reading;
if (stableState == LOW) { // the press edge (active-low)
Serial.println("Button pressed");
}
}
}
lastReading = reading;
}