State Change Detection (Edge Detection) for pushbuttons

I did happen to wire it up sloppily in Wokwi earlier today, falling for the floating input pin, and thought it was the debouncing that wasn't working.

If they were aiming for making things simple, the extra divide-by-4 chunk makes things needlessly complicated.

Here's a Wokwi of the Arduino Official code:

imagehttps://wokwi.com/projects/391475222059330561

The essential trick behind edge detection is remembering a value from before and comparing it to the currentValue, and the extra bits in the official sketch might distract from that.

If I was going to use this active-HIGH circuit and leave in some extra bits like debouncing and doing something interesting with the LED, I'd highlight those extra bits as separate features:

/*
  State change detection (edge detection)
  (with debouncing and LED toggling)
  https://wokwi.com/projects/391475222059330561

  Modified per https://forum.arduino.cc/t/state-change-detection-edge-detection-for-pushbuttons/1231632/10?u=davex
  to focus on the change detection, 
  and to use millis() for debouncing the change detection instead of delay();

  Often, you don't need to know the state of a digital input all the time, but
  you just need to know when the input changes from one state to another.
  For example, you want to know when a button goes from OFF to ON. This is called
  state change detection, or edge detection.

  This example shows how to:
  1) detect when a button or button changes from off to on and on to off.
  2) debounce an input with millis()
  3) toggle a LED based on its state

  The circuit:
  - pushbutton attached to pin 2 from +5V
  - 10 kilohm resistor attached to pin 2 from ground
  - LED attached from pin 13 to ground through 220 ohm resistor (or use the
    built-in LED on most Arduino boards)

  created  27 Sep 2005
  modified 30 Aug 2011
  by Tom Igoe
  modified 04 Mar 2023 by DveX for https://forum.arduino.cc/t/state-change-detection-edge-detection-for-pushbuttons/1231632/10
  This example code is in the public domain.
  
  Based on:
  https://docs.arduino.cc/built-in-examples/digital/StateChangeDetection/
*/

// this constant won't change:
const int buttonPin = 2;  // the pin that the pushbutton is attached to
const int ledPin = 13;    // the pin that the LED is attached to

// Variables will change:
int buttonState = 0;        // current state of the button
int lastButtonState = 0;    // previous state of the button
uint32_t lastChangeMillis = 0; // previous time of change for debouncing
uint32_t debounceInterval = 50;

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(115200);
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);
  uint32_t currentMillis = millis();

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState ) { // if the state has changed
    if (currentMillis - lastChangeMillis >= debounceInterval) { // limit rate using time stamps
      lastChangeMillis = currentMillis; // record the time for future debouncing
      // save the current button state as the real last state, for next time through the loop
      lastButtonState = buttonState;
      // act on the detected state change:
      if (buttonState == HIGH) {
        // if the current state is HIGH then the button went from off to on:
        Serial.print("HIGH ");
        toggleLed();
      } else {
        // if the current state is LOW then the button went from on to off:
        Serial.print("LOW ");
      }
    }
  }
}

void toggleLed(void){
  if (digitalRead(ledPin) == LOW) { // toggle the LED as needed
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }
}