Tutorial.Debounce History

Hide minor edits - Show changes to output

April 02, 2013, at 09:18 PM by Scott Fitzgerald -
Changed lines 5-6 from:
This example demonstrates the use '''debouncing''' the input, which means checking twice in a short period of time to make sure it's definitely pressed. Without debouncing, pressing the button once can appear to the code as multiple presses. Makes use of the millis() function to keep track of the time when the button is pressed.
to:
This example demonstrates how to '''debounce''' an input, which means checking twice in a short period of time to make sure it's definitely pressed. Without debouncing, pressing the button once can appear to the code as multiple presses. Makes use of the millis() function to keep track of the time when the button is pressed.
April 02, 2013, at 09:18 PM by Scott Fitzgerald -
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, which means checking twice in a short period of time to make sure it's definitely pressed. Without debouncing, pressing the button once can appear to the code as multiple presses. Makes use of the millis() function to keep track of the time when the button is pressed.
to:
This example demonstrates the use '''debouncing''' the input, which means checking twice in a short period of time to make sure it's definitely pressed. Without debouncing, pressing the button once can appear to the code as multiple presses. Makes use of the millis() function to keep track of the time when the button is pressed.
May 02, 2012, at 03:35 AM by Scott Fitzgerald -
Changed line 34 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/2.Digital/Debounce/Debounce.ino lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/02.Digital/Debounce/Debounce.ino lang=arduino tabwidth=4:)
November 16, 2011, at 04:05 AM by Scott Fitzgerald -
Changed line 34 from:
(:source http://arduino.cc/en/pub/code/new-extension/build/shared/examples/2.Digital/Debounce/Debounce.ino lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/2.Digital/Debounce/Debounce.ino lang=arduino tabwidth=4:)
September 30, 2011, at 03:03 AM by Scott Fitzgerald -
Changed line 34 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/2.Digital/Debounce/Debounce.pde lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/new-extension/build/shared/examples/2.Digital/Debounce/Debounce.ino lang=arduino tabwidth=4:)
September 23, 2010, at 10:29 PM by Christian Cerrito -
Changed lines 3-4 from:
!!Debounce
to:
!!!Debounce
September 23, 2010, at 09:09 PM by Christian Cerrito -
Changed lines 26-28 from:
%width=400px%[[Attach:button_schem.png | Attach:button_schem.png]]

to:
%height=400px%[[ Attach:button_sch.png | Attach:button_sch.png]]

September 17, 2010, at 05:41 PM by Tom Igoe -
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. Makes use of the millis() function to keep track of the time when the button is pressed.
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, which means checking twice in a short period of time to make sure it's definitely pressed. Without debouncing, pressing the button once can appear to the code as multiple presses. Makes use of the millis() function to keep track of the time when the button is pressed.
September 16, 2010, at 10:04 PM by Tom Igoe -
Changed line 34 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/2.Digital/Debounce/Debounce.pde lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/2.Digital/Debounce/Debounce.pde lang=arduino tabwidth=4:)
August 24, 2010, at 10:52 PM by Christian Cerrito -
Added lines 7-14:
!!!Hardware Required

* Arduino Board
* momentary button or switch
* 10K ohm resistor
* breadboard
* hook-up wire
Changed line 43 from:
* [[(Reference/)if]]
to:
* [[(Reference/)if]]()
August 24, 2010, at 10:28 PM by Christian Cerrito -
Deleted line 40:
* [[Debounce]] - read a pushbutton filtering noise
August 24, 2010, at 10:22 PM by Christian Cerrito -
Changed lines 30-31 from:
""See Also""
to:
!!!See Also
August 24, 2010, at 10:21 PM by Christian Cerrito -
Added lines 30-31:
""See Also""
Changed lines 36-38 from:
* [[Reference/)millis]]()

to:
* [[Reference/millis]]()

August 24, 2010, at 10:20 PM by Christian Cerrito -
Changed lines 28-40 from:
(:divend:)
to:
(:divend:)

* [[(Reference/)pinMode]]()
* [[(Reference/)digitalWrite]]()
* [[(Reference/)digitalRead]]()
* [[(Reference/)if]]
* [[Reference/)millis]]()


* [[DigitalReadSerial]] - read a switch, print the state out to the Serial Monitor
* [[Blink]] - turn an LED on and off
* [[Debounce]] - read a pushbutton filtering noise
* [[Button State Change]] - counting the number of button pushes
August 24, 2010, at 08:27 PM by Christian Cerrito -
August 24, 2010, at 08:08 AM by Christian Cerrito -
Changed line 26 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/Digital/Debounce/Debounce.pde lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/2.Digital/Debounce/Debounce.pde lang=arduino tabwidth=4:)
February 24, 2010, at 04:21 AM by Tom Igoe -
Added line 23:
Changed lines 26-50 from:
%color=#7e7e7e%/*
%color=#7e7e7e% Debounce
%color=#7e7e7e%
%color=#7e7e7e% Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
%color=#7e7e7e% press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There's
%color=#7e7e7e% a minimum delay between toggles to debounce the circuit (i.e. to ignore
%color=#7e7e7e% noise).
%color=#7e7e7e%
%color=#7e7e7e% The circuit:
%color=#7e7e7e% * LED attached from pin 13 to ground
%color=#7e7e7e% * pushbutton attached from pin 2 to +5V
%color=#7e7e7e% * 10K resistor attached from pin 2 to ground
%color=#7e7e7e%
%color=#7e7e7e% * Note: On most Arduino boards, there is already an LED on the board
%color=#7e7e7e% connected to pin 13, so you don't need any extra components for this example.
%color=#7e7e7e%
%color=#7e7e7e%
%color=#7e7e7e% created 21 November 2006
%color=#7e7e7e% by David A. Mellis
%color=#7e7e7e% modified 3 Jul 2009
%color=#7e7e7e% by Limor Fried
%color=#7e7e7e%
%color=#7e7e7e%
%color=#7e7e7e% http://www.arduino.cc/en/Tutorial/Debounce
%color=#7e7e7e% */
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/Digital/Debounce/Debounce.pde lang=arduino tabwidth=4:)
Deleted lines 27-74:
%color=#7e7e7e%// constants won't change. They're used here to
%color=#7e7e7e%// set pin numbers:
const %color=#cc6600%int%% buttonPin = 2; %color=#7e7e7e%// the number of the pushbutton pin
const %color=#cc6600%int%% ledPin = 13; %color=#7e7e7e%// the number of the LED pin

%color=#7e7e7e%// Variables will change:
%color=#cc6600%int%% ledState = %color=#006699%HIGH%%; %color=#7e7e7e%// the current state of the output pin
%color=#cc6600%int%% buttonState; %color=#7e7e7e%// the current reading from the input pin
%color=#cc6600%int%% lastButtonState = %color=#006699%LOW%%; %color=#7e7e7e%// the previous reading from the input pin

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

%color=#cc6600%void%% %color=#cc6600%'''setup'''%%() {
%color=#cc6600%pinMode%%(buttonPin, %color=#006699%INPUT%%);
%color=#cc6600%pinMode%%(ledPin, %color=#006699%OUTPUT%%);
}

%color=#cc6600%void%% %color=#cc6600%'''loop'''%%() {
%color=#7e7e7e%// read the state of the switch into a local variable:
%color=#cc6600%int%% reading = %color=#cc6600%digitalRead%%(buttonPin);

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

%color=#7e7e7e%// If the switch changed, due to noise or pressing:
%color=#cc6600%if%% (reading != lastButtonState) {
%color=#7e7e7e%// reset the debouncing timer
lastDebounceTime = %color=#cc6600%millis%%();
}

%color=#cc6600%if%% ((%color=#cc6600%millis%%() - lastDebounceTime) > debounceDelay) {
%color=#7e7e7e%// whatever the reading is at, it's been there for longer
%color=#7e7e7e%// than the debounce delay, so take it as the actual current state:
buttonState = reading;
}

%color=#7e7e7e%// set the LED using the state of the button:
%color=#cc6600%digitalWrite%%(ledPin, buttonState);

%color=#7e7e7e%// save the reading. Next time through the loop,
%color=#7e7e7e%// it'll be the lastButtonState:
lastButtonState = reading;
}
August 27, 2009, at 08:46 PM by Tom Igoe -
Added lines 11-13:
[-image developed using [[http://www.fritzing.org |Fritzing]]. For more circuit examples, see the [[http://fritzing.org/projects/|Fritzing project page]] -]

July 05, 2009, at 07:15 PM by Tom Igoe -
Changed lines 20-21 from:
/*
Debounce
to:
(:div class=code :)
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:
%color=#7e7e7e%/*
%color=#7e7e7e% Debounce
%color=#7e7e7e%
%color=#7e7e7e% Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
%color=#7e7e7e% press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There's
%color=#7e7e7e% a minimum delay between toggles to debounce the circuit (i.e. to ignore
%color=#7e7e7e% noise).
%color=#7e7e7e%
%color=#7e7e7e% The circuit:
%color=#7e7e7e% * LED attached from pin 13 to ground
%color=#7e7e7e% * pushbutton attached from pin 2 to +5V
%color=#7e7e7e% * 10K resistor attached from pin 2 to ground
%color=#7e7e7e%
%color=#7e7e7e% * Note: On most Arduino boards, there is already an LED on the board
%color=#7e7e7e% connected to pin 13, so you don't need any extra components for this example.
%color=#7e7e7e%
%color=#7e7e7e%
%color=#7e7e7e% created 21 November 2006
%color=#7e7e7e% by David A. Mellis
%color=#7e7e7e% modified 3 Jul 2009
%color=#7e7e7e% by Limor Fried
%color=#7e7e7e%
%color=#7e7e7e%
%color=#7e7e7e% http://www.arduino.cc/en/Tutorial/Debounce
%color=#7e7e7e% */
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:
%color=#7e7e7e%// constants won't change. They're used here to
%color=#7e7e7e%// set pin numbers:
const %color=#cc6600%int%% buttonPin = 2; %color=#7e7e7e%// the number of the pushbutton pin
const %color=#cc6600%int%% ledPin = 13; %color=#7e7e7e%// 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:
%color=#7e7e7e%// Variables will change:
%color=#cc6600%int%% ledState = %color=#006699%HIGH%%; %color=#7e7e7e%// the current state of the output pin
%color=#cc6600%int%% buttonState; %color=#7e7e7e%// the current reading from the input pin
%color=#cc6600%int%% lastButtonState = %color=#006699%LOW%%; %color=#7e7e7e%// 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:
%color=#7e7e7e%// the following variables are long's because the time, measured in miliseconds,
%color=#7e7e7e%// will quickly become a bigger number than can be stored in an int.
%color=#cc6600%long%% lastDebounceTime = 0; %color=#7e7e7e%// the last time the output pin was toggled
%color=#cc6600%long%% debounceDelay = 50; %color=#7e7e7e%// the debounce time; increase if the output flickers
Changed lines 63-65 from:
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
to:
%color=#cc6600%void%% %color=#cc6600%'''setup'''%%() {
%color=#cc6600%pinMode%%(buttonPin, %color=#006699%INPUT%%);
%color=#cc6600%pinMode%%(ledPin, %color=#006699%OUTPUT%%);
Changed lines 68-70 from:
void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
to:
%color=#cc6600%void%% %color=#cc6600%'''loop'''%%() {
%color=#7e7e7e%// read the state of the switch into a local variable:
%color=#cc6600%int%% reading = %color=#cc6600%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:
%color=#7e7e7e%// check to see if you just pressed the button
%color=#7e7e7e%// (i.e. the input went from LOW to HIGH), and you've waited
%color=#7e7e7e%// 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:
%color=#7e7e7e%// If the switch changed, due to noise or pressing:
%color=#cc6600%if%% (reading != lastButtonState) {
%color=#7e7e7e%// reset the debouncing timer
lastDebounceTime = %color=#cc6600%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:
%color=#cc6600%if%% ((%color=#cc6600%millis%%() - lastDebounceTime) > debounceDelay) {
%color=#7e7e7e%// whatever the reading is at, it's been there for longer
%color=#7e7e7e%// 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:
%color=#7e7e7e%// set the LED using the state of the button:
%color=#cc6600%digitalWrite%%(ledPin, buttonState);
Changed lines 91-92 from:
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
to:
%color=#7e7e7e%// save the reading. Next time through the loop,
%color=#7e7e7e%// it'll be the lastButtonState:
Added lines 95-96:

(:divend:)
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 [[http://www.ladyada.net/learn/sensors/tilt.html|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.

Attach:PushButton.jpg
to:
!!!Circuit

Attach:button.png

!!!Schematic

[-click the image to enlarge-]

%width=400px%[[Attach:button_schem.png | Attach:button_schem.png]]

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.

Attach:PushButton.jpg

!!! 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