Error on http://arduino.cc/en/Tutorial/Debounce

Hi!

The program on page http://arduino.cc/en/Tutorial/Debounce is not matching with this following description on that page:

This example demonstrates the use of a pushbutton as a switch: each time you press the button, the LED (or whatever) is turned on (if it's off) or off (if on).

Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
press), the output pin is toggled from LOW to HIGH or HIGH to LOW.

Maybe I'm just misreading this? From this description I would expect that the LED stays turned on when I release the push button.

But this interpretation is not matching this code:

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 != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  } 
  
  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:
    buttonState = reading;
  }
  
  // set the LED using the state of the button:
  digitalWrite(ledPin, buttonState);

  // save the reading.  Next time through the loop,
  // it'll be the lastButtonState:
  lastButtonState = reading;
}

This code comes also with the Arduino IDE Download.

I noticed the same thing and re-wrote the example to actually work as described.

// 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 = HIGH;         // the current state of the output pin

int buttonState;             // the current reading from the input pin
int lastButtonState = 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.
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
const unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
  pinMode(buttonPin, INPUT);
  digitalWrite(buttonPin, HIGH); // turn on the internal pull-up
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  int currentButtonState = digitalRead(buttonPin);
  
  if (currentButtonState != lastButtonState) {
    lastDebounceTime = millis();
  } 

  if ((millis() - lastDebounceTime) > debounceDelay && 
    currentButtonState != buttonState) {
    // We have a new and different button state   
    buttonState = currentButtonState;
    if (buttonState == LOW) // Pressed
    {
      // The new state is "Pressed"
      ledState = !ledState;
      digitalWrite(ledPin, ledState);
    }
  }
  
  lastButtonState = currentButtonState;
}

[b]unsigned[/b] long, not long ! (That was a kneejerk reaction, sorry. Well, not really. :wink: )

Msquare:
[b]unsigned[/b] long, not long ! (That was a kneejerk reaction, sorry. Well, not really. :wink: )

OOPS! You're right. Making the corrections now.

Changing:

long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers

to:

unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
const unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers

What is the best way to forward such corrections to the actual website?

just tried but not enough privileges.

You can report a bug at - Google Code Archive - Long-term storage for Google Code Project Hosting. - and refer to this thread. You can login with a gmail account. My experience is that the bugslist is monitored but I can imagine that now the energy and focus is on the 1.0 version.