Pages: [1]   Go Down
Author Topic: Error on http://arduino.cc/en/Tutorial/Debounce  (Read 742 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi!

The program on page  http://arduino.cc/en/Tutorial/Debounce  is not matching with this following description on that page:
Quote
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).
Quote
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:
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.
« Last Edit: October 14, 2011, 04:50:06 am by shybyte » Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8081
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I noticed the same thing and re-wrote the example to actually work as described.
Code:
// 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;
}
« Last Edit: October 15, 2011, 03:49:09 pm by johnwasser » Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Copenhagen, Denmark
Offline Offline
Edison Member
*
Karma: 25
Posts: 1143
Have you testrun your INO file today?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

unsigned long, not long !  (That was a kneejerk reaction, sorry. Well, not really.  smiley-wink )
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8081
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

unsigned long, not long !  (That was a kneejerk reaction, sorry. Well, not really.  smiley-wink )

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

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

to:
Code:
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
« Last Edit: October 15, 2011, 03:48:49 pm by johnwasser » Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12465
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

just tried but not enough privileges.

You can report a bug at - http://code.google.com/p/arduino/issues/list - 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. 
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Pages: [1]   Go Up
Jump to: