Go Down

Topic: Error on http://arduino.cc/en/Tutorial/Debounce (Read 1 time) previous topic - next topic

shybyte

Oct 14, 2011, 11:39 am Last Edit: Oct 14, 2011, 11:50 am by shybyte Reason: 1
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: [Select]

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.

johnwasser

#1
Oct 14, 2011, 07:06 pm Last Edit: Oct 15, 2011, 10:49 pm by johnwasser Reason: 1
I noticed the same thing and re-wrote the example to actually work as described.
Code: [Select]

// 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;
}
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Msquare

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

johnwasser

#3
Oct 15, 2011, 10:44 pm Last Edit: Oct 15, 2011, 10:48 pm by johnwasser Reason: 1

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


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

Changing:
Code: [Select]

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


to:
Code: [Select]

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
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

shybyte

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

robtillaart

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. 
Rob Tillaart

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

Go Up