understanding debounce tutorial code

I'm having trouble understanding the code from the debounce tutorial, specifically the void loop().

Below is code from the tutorial with my interpretation of what its doing in plain english. I'm really lost on the millis() part. Would be awesome sauce if someone could translate to noobanese for me. :fearful: :smiley:

const int buttonPin = 2;
const int ledPin = 4;

int buttonState;
int lastButtonState = LOW;
int ledState = HIGH;

long lastDebounceTime = 0;
long debounceDelay = 50;

void setup() {
  Serial.begin(9600);
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, ledState);
}

void loop() {
  int reading = digitalRead(buttonPin); // read the button
  if (reading != lastButtonState) {       // check if the button has been pressed, if true then...
    lastDebounceTime = millis();         // change the lastDebounceTime equal to amount of time passed since loop started
  }
  if ((millis() - lastDebounceTime) > debounceDelay) { // check if ...confused... is greater than 50ms, if true then...
    if (reading != buttonState) {       // check if the reading we took earlier is not the buttonState, if true then...
      buttonState = reading;            // change the buttonState to match the reading
      if (buttonState == HIGH) {        // check if the new buttonState is HIGH, if true then...
        ledState = !ledState;           // invert the ledState
      }
    }
  }
  digitalWrite(ledPin, ledState);       // turn on or off the LED
  lastButtonState = reading;            // change lastButtonState to current reading in prep for next loop
}
  if (reading != lastButtonState) {       // check if the button has been pressed, if true then...

Checks if the switch has BECOME pressed or has BECOME released

  if ((millis() - lastDebounceTime) > debounceDelay) { // check if ...confused... is greater than 50ms, if true then...

Compares now with the last time the switch changed state. If the times are too close together, the switch bounced rather than making a useful state change.

The thing that is so confusing about that crappy example is that they use three variables to keep track of the two states of the switch.

PaulS:
The thing that is so confusing about that crappy example is that they use three variables to keep track of the two states of the switch.

So how would you do it?

jtdoria:
So how would you do it?

I woudn't.

There is a very simple debounce process in several things at a time and none at all in planning and implementing a program.

...R

So how would you do it?

There is a current switch state and a previous switch state. There is a current time and there is a time that the last state change happened. That's a total of four values that need to be considered to determine if the state changed, and, if so, whether we care (legitimate press or release vs. contact bounce).

If the state change is legitimate, save the current state as the previous state. If not, don't do that. There is no need to keep track of the current state, the previous state, and the bouncy state.