source: arduino-1-6-7/trunk/fuentes/arduino-ide-amd64/examples/02.Digital/Debounce/Debounce.ino @ 46

Last change on this file since 46 was 46, checked in by jrpelegrina, 4 years ago

First release to Xenial

File size: 2.6 KB
Line 
1/*
2 Debounce
3
4 Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
5 press), the output pin is toggled from LOW to HIGH or HIGH to LOW.  There's
6 a minimum delay between toggles to debounce the circuit (i.e. to ignore
7 noise).
8
9 The circuit:
10 * LED attached from pin 13 to ground
11 * pushbutton attached from pin 2 to +5V
12 * 10K resistor attached from pin 2 to ground
13
14 * Note: On most Arduino boards, there is already an LED on the board
15 connected to pin 13, so you don't need any extra components for this example.
16
17
18 created 21 November 2006
19 by David A. Mellis
20 modified 30 Aug 2011
21 by Limor Fried
22 modified 28 Dec 2012
23 by Mike Walters
24
25 This example code is in the public domain.
26
27 http://www.arduino.cc/en/Tutorial/Debounce
28 */
29
30// constants won't change. They're used here to
31// set pin numbers:
32const int buttonPin = 2;    // the number of the pushbutton pin
33const int ledPin = 13;      // the number of the LED pin
34
35// Variables will change:
36int ledState = HIGH;         // the current state of the output pin
37int buttonState;             // the current reading from the input pin
38int lastButtonState = LOW;   // the previous reading from the input pin
39
40// the following variables are long's because the time, measured in miliseconds,
41// will quickly become a bigger number than can be stored in an int.
42long lastDebounceTime = 0;  // the last time the output pin was toggled
43long debounceDelay = 50;    // the debounce time; increase if the output flickers
44
45void setup() {
46  pinMode(buttonPin, INPUT);
47  pinMode(ledPin, OUTPUT);
48
49  // set initial LED state
50  digitalWrite(ledPin, ledState);
51}
52
53void loop() {
54  // read the state of the switch into a local variable:
55  int reading = digitalRead(buttonPin);
56
57  // check to see if you just pressed the button
58  // (i.e. the input went from LOW to HIGH),  and you've waited
59  // long enough since the last press to ignore any noise:
60
61  // If the switch changed, due to noise or pressing:
62  if (reading != lastButtonState) {
63    // reset the debouncing timer
64    lastDebounceTime = millis();
65  }
66
67  if ((millis() - lastDebounceTime) > debounceDelay) {
68    // whatever the reading is at, it's been there for longer
69    // than the debounce delay, so take it as the actual current state:
70
71    // if the button state has changed:
72    if (reading != buttonState) {
73      buttonState = reading;
74
75      // only toggle the LED if the new button state is HIGH
76      if (buttonState == HIGH) {
77        ledState = !ledState;
78      }
79    }
80  }
81
82  // set the LED:
83  digitalWrite(ledPin, ledState);
84
85  // save the reading.  Next time through the loop,
86  // it'll be the lastButtonState:
87  lastButtonState = reading;
88}
89
Note: See TracBrowser for help on using the repository browser.