Tutorial.Debounce History

Hide minor edits - Show changes to markup

June 04, 2010, at 11:57 PM by Equipo Traduccion -
Changed lines 1-8 from:

Examples > Digital I/O

Debounce

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). It also debounces the input, without which pressing the button once would appear to the code as multiple presses. Makes use of the millis() function to keep track of the time when the button is pressed.

Circuit

to:

Ejemplos > E/S (I/O) Digital

Anti-Rebote

Este ejemplo muestra el uso de un pulsador como un interruptor: cada vez que pulsas el botón, el LED (o lo que sea) es encendido (si está apagado) o apagado (si está encendido). Esto requiere anti rebotes en la entrada, sin el una sola pulsación puede aparecer para el código como muchas pulsaciones. Haz uso de la función millis() para no perder de vista el momento de pulsar el botón.

Circuito

Changed lines 11-17 from:

image developed using Fritzing. For more circuit examples, see the Fritzing project page

Schematic:

click the image to enlarge

to:

imagen desarrollada usando Fritzing. para más circuitos de ejemplo, visita lapágina del proyecto Fritzing

Esquema:

haz click en la imagen para ampliarla

Changed lines 22-23 from:

The code below is based on Limor Fried's version of debounce, but the logic is inverted from her example. In her example, the switch returns LOW when closed, and HIGH when open. Here, the switch returns HIGH when pressed and LOW when not pressed.

to:

El código está basado en la versión de anti rebotes de Limor Fried, pero la lógica está invertida en el ejemplo. En su ejemplo, el switch retorna LOW cuando está cerrado, y HIGH cuando está abierto. Aquí, el switch retorna HIGH cuando está pulsado y LOW cuando no está pulsado.

February 24, 2010, at 04:21 AM by Tom Igoe -
Added line 23:
Changed lines 26-50 from:
 /* 
  Debounce
  
  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.  There's
  a minimum delay between toggles to debounce the circuit (i.e. to ignore
  noise).  
  
  The circuit:
  * LED attached from pin 13 to ground
  * pushbutton attached from pin 2 to +5V
  * 10K resistor attached from pin 2 to ground
  
  * Note: On most Arduino boards, there is already an LED on the board
  connected to pin 13, so you don't need any extra components for this example.
  
  
  created 21 November 2006
  by David A. Mellis
  modified 3 Jul 2009
  by Limor Fried
  
  
  http://www.arduino.cc/en/Tutorial/Debounce
  */
to:
Deleted lines 27-74:
 // 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.
 long lastDebounceTime = 0;  // the last time the output 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 != 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;
 }
August 27, 2009, at 08:46 PM by Tom Igoe -
Added lines 11-13:

image developed using Fritzing. For more circuit examples, see the Fritzing project page

July 05, 2009, at 07:15 PM by Tom Igoe -
Changed lines 20-21 from:

/*

  Debounce
to:
Changed lines 22-43 from:
  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.  There's
  a minimum delay between toggles to debounce the circuit (i.e. to ignore
  noise).  

  The circuit:
  * LED attached from pin 13 to ground
  * pushbutton attached from pin 2 to +5V
  * 10K resistor attached from pin 2 to ground

  * Note: On most Arduino boards, there is already an LED on the board
  connected to pin 13, so you don't need any extra components for this example.

  created 21 November 2006
  by David A. Mellis
  modified 3 Jul 2009
  by Limor Fried

  http://www.arduino.cc/en/Tutorial/Debounce
  */
to:
 /* 
  Debounce
  
  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.  There's
  a minimum delay between toggles to debounce the circuit (i.e. to ignore
  noise).  
  
  The circuit:
  * LED attached from pin 13 to ground
  * pushbutton attached from pin 2 to +5V
  * 10K resistor attached from pin 2 to ground
  
  * Note: On most Arduino boards, there is already an LED on the board
  connected to pin 13, so you don't need any extra components for this example.
  
  
  created 21 November 2006
  by David A. Mellis
  modified 3 Jul 2009
  by Limor Fried
  
  
  http://www.arduino.cc/en/Tutorial/Debounce
  */
Changed lines 48-51 from:
 // 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
to:
 // 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
Changed lines 53-56 from:
 // 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
to:
 // 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
Changed lines 58-61 from:
 // 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 output pin was toggled
 long debounceDelay = 50;    // the debounce time; increase if the output flickers
to:
 // 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 output pin was toggled
 long debounceDelay = 50;    // the debounce time; increase if the output flickers
Changed lines 63-65 from:
 void setup() {
   pinMode(buttonPin, INPUT);
   pinMode(ledPin, OUTPUT);
to:
 void setup() {
   pinMode(buttonPin, INPUT);
   pinMode(ledPin, OUTPUT);
Changed lines 68-70 from:
 void loop() {
   // read the state of the switch into a local variable:
   int reading = digitalRead(buttonPin);
to:
 void loop() {
   // read the state of the switch into a local variable:
   int reading = digitalRead(buttonPin);
Changed lines 72-74 from:
   // 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:  
to:
   // 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:  
Changed lines 76-79 from:
   // If the switch changed, due to noise or pressing:
   if (reading != lastButtonState) {
     // reset the debouncing timer
     lastDebounceTime = millis();
to:
   // If the switch changed, due to noise or pressing:
   if (reading != lastButtonState) {
     // reset the debouncing timer
     lastDebounceTime = millis();
Changed lines 82-84 from:
   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:
to:
   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:
Changed lines 88-89 from:
   // set the LED using the state of the button:
   digitalWrite(ledPin, buttonState);
to:
   // set the LED using the state of the button:
   digitalWrite(ledPin, buttonState);
Changed lines 91-92 from:
   // save the reading.  Next time through the loop,
   // it'll be the lastButtonState:
to:
   // save the reading.  Next time through the loop,
   // it'll be the lastButtonState:
Added lines 95-96:
July 05, 2009, at 07:14 PM by Tom Igoe -
Deleted lines 19-20:

[@

Changed lines 21-44 from:
 Debounce
to:
  Debounce

  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.  There's
  a minimum delay between toggles to debounce the circuit (i.e. to ignore
  noise).  

  The circuit:
  * LED attached from pin 13 to ground
  * pushbutton attached from pin 2 to +5V
  * 10K resistor attached from pin 2 to ground

  * Note: On most Arduino boards, there is already an LED on the board
  connected to pin 13, so you don't need any extra components for this example.

  created 21 November 2006
  by David A. Mellis
  modified 3 Jul 2009
  by Limor Fried

  http://www.arduino.cc/en/Tutorial/Debounce
  */
Changed lines 46-49 from:
 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.  There's
 a minimum delay between toggles to debounce the circuit (i.e. to ignore
 noise).  
to:
 // 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
Changed lines 51-54 from:
 The circuit:
 * LED attached from pin 13 to ground
 * pushbutton attached from pin 2 to +5V
 * 10K resistor attached from pin 2 to ground
to:
 // 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
Changed lines 56-57 from:
 * Note: On most Arduino boards, there is already an LED on the board
 connected to pin 13, so you don't need any extra components for this example.
to:
 // 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 output pin was toggled
 long debounceDelay = 50;    // the debounce time; increase if the output flickers
Added lines 61-64:
 void setup() {
   pinMode(buttonPin, INPUT);
   pinMode(ledPin, OUTPUT);
 }
Changed lines 66-69 from:
 created 21 November 2006
 by David A. Mellis
 modified 3 Jul 2009
 by Limor Fried
to:
 void loop() {
   // read the state of the switch into a local variable:
   int reading = digitalRead(buttonPin);
Added lines 70-72:
   // 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:  
Changed lines 74-124 from:
 http://www.arduino.cc/en/Tutorial/Debounce
 */

// 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. long lastDebounceTime = 0; // the last time the output 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 != 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 buttonState.  Next time through the loop,
  // it'll be the lastButtonState:
  lastButtonState = reading;

} @]

to:
   // 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;
 }
July 04, 2009, at 12:37 AM by Tom Igoe -
July 04, 2009, at 12:25 AM by Tom Igoe -
Changed lines 19-20 from:
to:

The code below is based on Limor Fried's version of debounce, but the logic is inverted from her example. In her example, the switch returns LOW when closed, and HIGH when open. Here, the switch returns HIGH when pressed and LOW when not pressed.

Changed lines 41-42 from:
 modified 17 Jun 2009
 by Tom Igoe
to:
 modified 3 Jul 2009
 by Limor Fried
Changed line 58 from:

// the follow variables are longs because the time, measured in milliseconds,

to:

// the following variables are long's because the time, measured in miliseconds,

Changed lines 61-62 from:

long debounceDelay = 200; // the debounce time, increase if the output flickers

to:

long debounceDelay = 50; // the debounce time; increase if the output flickers

Changed lines 69-70 from:
  buttonState = digitalRead(buttonPin);
to:
  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);
Changed lines 75-86 from:
  if ((buttonState == HIGH) && 
    (lastButtonState == LOW) && 
    (millis() - lastDebounceTime) > debounceDelay) {
    // toggle the output
    if (ledState == HIGH) {
      ledState = LOW;
    } else {
      ledState = HIGH;
    }
    // ... and store the time of the last button press
    // in a variable:
    lastDebounceTime = millis();    
to:
  // 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;
Changed lines 87-90 from:
  // set the LED using the ledState variable:
  digitalWrite(ledPin, ledState);
to:
  // set the LED using the state of the button:
  digitalWrite(ledPin, buttonState);
Changed line 93 from:
  lastButtonState = buttonState;
to:
  lastButtonState = reading;
Deleted line 94:
June 25, 2009, at 10:02 PM by Tom Igoe -
June 25, 2009, at 02:17 PM by Tom Igoe -
Changed lines 7-12 from:

Circuit

A push-button on pin 7 and an LED on pin 13.

to:

Circuit

Schematic:

click the image to enlarge

Changed lines 31-32 from:
 * pushbutton attached from pin 7 to +5V
 * 10K resistor attached from pin 7 to ground
to:
 * pushbutton attached from pin 2 to +5V
 * 10K resistor attached from pin 2 to ground
Changed line 49 from:

const int buttonPin = 7; // the number of the pushbutton pin

to:

const int buttonPin = 2; // the number of the pushbutton pin

June 17, 2009, at 11:21 PM by Tom Igoe -
Changed lines 16-23 from:

int inPin = 7; // the number of the input pin int outPin = 13; // the number of the output pin

int state = HIGH; // the current state of the output pin int reading; // the current reading from the input pin int previous = LOW; // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,

to:

/*

 Debounce

 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.  There's
 a minimum delay between toggles to debounce the circuit (i.e. to ignore
 noise).  

 The circuit:
 * LED attached from pin 13 to ground
 * pushbutton attached from pin 7 to +5V
 * 10K resistor attached from pin 7 to ground

 * Note: On most Arduino boards, there is already an LED on the board
 connected to pin 13, so you don't need any extra components for this example.

 created 21 November 2006
 by David A. Mellis
 modified 17 Jun 2009
 by Tom Igoe

 http://www.arduino.cc/en/Tutorial/Debounce
 */

// constants won't change. They're used here to // set pin numbers: const int buttonPin = 7; // 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 follow variables are longs because the time, measured in milliseconds,

Changed lines 54-60 from:

long time = 0; // the last time the output pin was toggled long debounce = 200; // the debounce time, increase if the output flickers

void setup() {

  pinMode(inPin, INPUT);
  pinMode(outPin, OUTPUT);
to:

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

void setup() {

  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
Changed lines 62-76 from:

void loop() {

  reading = digitalRead(inPin);

  // if we just pressed the button (i.e. the input went from LOW to HIGH),
  // and we've waited long enough since the last press to ignore any noise...  
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    // ... invert the output
    if (state == HIGH)
      state = LOW;
    else
      state = HIGH;

    // ... and remember when the last button press was
    time = millis();    
to:

void loop() {

  buttonState = 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 ((buttonState == HIGH) && 
    (lastButtonState == LOW) && 
    (millis() - lastDebounceTime) > debounceDelay) {
    // toggle the output
    if (ledState == HIGH) {
      ledState = LOW;
    } else {
      ledState = HIGH;
    }
    // ... and store the time of the last button press
    // in a variable:
    lastDebounceTime = millis();    
Changed lines 82-84 from:
  digitalWrite(outPin, state);

  previous = reading;
to:
  // set the LED using the ledState variable:
  digitalWrite(ledPin, ledState);

  // save the buttonState.  Next time through the loop,
  // it'll be the lastButtonState:
  lastButtonState = buttonState;
Added line 89:
June 16, 2007, at 05:17 PM by David A. Mellis -
Changed lines 5-6 from:

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). It also debounces the input, without which pressing the button once would appear to the code as multiple presses.

to:

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). It also debounces the input, without which pressing the button once would appear to the code as multiple presses. Makes use of the millis() function to keep track of the time when the button is pressed.

June 16, 2007, at 05:14 PM by David A. Mellis -
Changed lines 9-10 from:

A push-button on pin 2 and an LED on pin 13.

to:

A push-button on pin 7 and an LED on pin 13.

Changed line 16 from:

int inPin = 2; // the number of the input pin

to:

int inPin = 7; // the number of the input pin

June 16, 2007, at 05:13 PM by David A. Mellis -
Changed lines 9-10 from:

A push-button on pin 7 and an LED on pin 13.

to:

A push-button on pin 2 and an LED on pin 13.

Changed lines 16-28 from:

/*

 * Debounce
 * by David A. Mellis
 * 
 * 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.  There's
 * a minimum delay between toggles to debounce the circuit (i.e. to ignore
 * noise).  
 *
 * http://www.arduino.cc/en/Tutorial/Debounce
 */

int inPin = 7; // the number of the input pin

to:

int inPin = 2; // the number of the input pin

Changed lines 37-40 from:
  // if the input just went from LOW and HIGH and we've waited long enough
  // to ignore any noise on the circuit, toggle the output pin and remember
  // the time
to:
  // if we just pressed the button (i.e. the input went from LOW to HIGH),
  // and we've waited long enough since the last press to ignore any noise...  
Added line 41:
    // ... invert the output
Changed lines 46-48 from:
    digitalWrite(outPin, state);
    time = millis();
to:
    // ... and remember when the last button press was
    time = millis();    
Added lines 51-52:
  digitalWrite(outPin, state);
March 25, 2007, at 09:47 AM by David A. Mellis -
Changed lines 9-10 from:

A push-button on pin 2 and an LED on pin 13.

to:

A push-button on pin 7 and an LED on pin 13.

Changed line 28 from:

int inPin = 2; // the number of the input pin

to:

int inPin = 7; // the number of the input pin

March 25, 2007, at 09:46 AM by David A. Mellis -
Changed lines 16-18 from:

/* switch

to:

/*

 * Debounce
 * by David A. Mellis
Changed lines 25-26 from:
 * David A. Mellis
 * 21 November 2006
to:
 * http://www.arduino.cc/en/Tutorial/Debounce
Deleted lines 41-43:
  if (DEBUG)
    Serial.begin(19200);
Changed lines 59-60 from:
    time = millis();    
to:
    digitalWrite(outPin, state);
    time = millis();
Changed lines 62-64 from:
  digitalWrite(outPin, state);
to:
March 25, 2007, at 09:45 AM by David A. Mellis -
Changed lines 3-4 from:

Switch

to:

Debounce

March 25, 2007, at 09:45 AM by David A. Mellis - Renaming Switch example
Added lines 1-68:

Examples > Digital I/O

Switch

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). It also debounces the input, without which pressing the button once would appear to the code as multiple presses.

Circuit

A push-button on pin 2 and an LED on pin 13.

Code

/* switch
 * 
 * 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.  There's
 * a minimum delay between toggles to debounce the circuit (i.e. to ignore
 * noise).  
 *
 * David A. Mellis
 * 21 November 2006
 */

int inPin = 2;         // the number of the input pin
int outPin = 13;       // the number of the output pin

int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin

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

void setup()
{
  if (DEBUG)
    Serial.begin(19200);

  pinMode(inPin, INPUT);
  pinMode(outPin, OUTPUT);
}

void loop()
{
  reading = digitalRead(inPin);

  // if the input just went from LOW and HIGH and we've waited long enough
  // to ignore any noise on the circuit, toggle the output pin and remember
  // the time
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH)
      state = LOW;
    else
      state = HIGH;

    time = millis();    
  }

  digitalWrite(outPin, state);

  previous = reading;
}

Share