Debounce example doesn't work as described.

The Debounce example on this site (forum won't let me post the link) and the one included with the Arduino IDE are supposed to toggle the LED state each time the button is pressed, according to the description on the page and the start of the file. However the code never toggles the ledState variable (it is set but never referenced) and the LED is just set to the button state.

This modified code works for me:

// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin = 2;    // the number of the pushbutton pin
const int ledPin =  13;     // the number of the LED pin

// Variables will change:
int ledState = LOW;         // the current state of the output pin
int lastButtonState = LOW;  // the previous debounced button state
int lastReading= LOW;       // the previous reading from the input pin

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0;  // the last time the input pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);

  // check to see if you just pressed the button 
  // (i.e. the input went from LOW to HIGH),  and you've waited 
  // long enough since the last press to ignore any noise:  

  // If the switch changed, due to noise or pressing:
  if (reading != lastReading) {
    // reset the debouncing timer
    lastDebounceTime = millis();
    // save the reading.  Next time through the loop,
    // it'll be lastReading:
    lastReading = reading;
  } 
  
  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so accept the button changed state:
  
    // toggle the LED if the state of the button changes from LOW to HIGH:
    if (lastButtonState == LOW && reading == HIGH) {
      if (ledState == HIGH) {
        ledState = LOW;
      } else {
        ledState = HIGH;
      }
      digitalWrite(ledPin, ledState);
    }
    lastButtonState = reading;
  }
}
 if (ledState == HIGH) {
        ledState = LOW;
      } else {
        ledState = HIGH;
      }

aka ledState = !ledState; or if you really like:ledState = (HIGH + LOW) - ledState; which allows for the possibility that HIGH != 1 or LOW != 0

I purposefully left the code simplistic since most of the other examples are written this way, presumably because beginners will be reading it.

Personally, I would have used:

ledState = (ledState == LOW) ? HIGH : LOW;

This doesn't require knowing what the values of HIGH and LOW are, and it won't break. It also produces the smallest code (tied with ledState=!ledState).

I wouldn't recommend using ledState = !ledState in this example code, since it is a little obscure for beginners and it only works if LOW=0 and HIGH=1. (Theoretically it's also possible that other languages/compilers might use a bitwise xor and flip between 0 and -1 which would then break if ledState was compared against HIGH -- ie. it's safe to use ! in boolean expressions, but sometimes dodgy with integer expressions)

The "ledState = (HIGH+LOW)-ledState;" is also slightly obscure for beginners and could also break if (HIGH+LOW) > pow(2,sizeof(ledState)) although it's highly unlikely that would happen. FWIW, It's 2 bytes larger than the !ledState and the ?: method.

FWIW, It's 2 bytes larger than the !ledState and the ?: method.

FWIW, at 0017, it is four bytes shorter, and since it doesn't have a conditional branch, will almost certainly execute faster.

But let's not quibble.

But let's not quibble.

Agreed! I bet we could also agree that code can be optimized for speed, size or readability but rarely for all three at once.

In any case, I hope that the Debounce example is updated (with any version of code) so that it performs as described on the website and in the header documentation.

Oops, I stand corrected. For this particular example code, the (HIGH+LOW)-ledState produces the smallest code of all the options (6 bytes smaller using 0018)

My previous size results were on a small test program where for some reason the (HIGH+LOW)-ledState was larger. Obviously, optimizer results may vary.

Again, I'm not concerned what version of the code is used in the Debounce example -- only that it does what it is supposed to do.

code can be optimized for speed, size or readability but rarely for all three at once.

My favourite code maxim:

Cheap, fast, reliable. Pick two.