Debounce
Parts
-Arduino microcontroller and carrier board
-LiPo battery
-Switch button
-Resistor
-3 Jumper wires
Prepare the breadboard
Program the Microcontroller
For more information: Arduino Debounce Tutorial
/**
* @file: Debounce
* @date: 4/5/2011
*
* @DESCRIPTION
* 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
*
**/
#define
buttonPin 2
int buttonState = 0; // the current reading from the input pin
int lastButtonState =
LOW; // the previous reading from the input pin
long lastDebounceTime =
0; // the last time the output pin was toggled
const long debounceDelay =
50; // the debounce time; increase if the output flickers
//--- Function: Setup ()
void
setup()
{
pinMode(buttonPin,
INPUT);
}
//--- Function: Setup ()
void
loop()
{
int reading =
digitalRead(buttonPin);
// read the state of the switch
// If the switch changed, due to noise or pressing:
if
(reading != lastButtonState)
{
lastDebounceTime =
millis(); // reset the debouncing timer
}
if
((millis()
- lastDebounceTime)
> debounceDelay)
{
buttonState = reading; // actual current state
}
// save the reading. Next time through the loop,
it'll be the lastButtonState:
lastButtonState = reading;
}
Code from Lecture
int reading = 0;
int buttonState = 0;
int lastButtonState = LOW;
int lastReading = LOW;
long lastDebounceTime = 0;
#define
buttonPin 3
void setup()
{
pinMode(buttonPin, INPUT);
Serial.begin(9600); delay(25);
}
void loop()
{
reading = digitalRead(buttonPin);
if (reading != lastReading)
{
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > 50)
{
buttonState = reading;
if (buttonState&(!lastButtonState))
{
Serial.println("button press");
}
}
lastButtonState = buttonState;
lastReading = reading;
}