HARDWARE SOLUTION:
There are countless circuits for debouncing switches - with different methods more or less effective under differing circumstances. The following circuit is one possible solution for switch debouncing and has been chosen for its effectiveness in switching low level logic signals used by microcontrollers (such as the Arduino.) The circuit is very simple and uses a very small number of components (one 2N3904 transistor, one 10uF electrolytic capacitor, two 1K OHM resistors and one N/O momentary pushbutton switch.) In this example, the output of the debounced switch is connected to DIGITAL PIN 8 on the Arduino with and LED connected to PIN 13 (can be used for testing the switch debounce circuit.)
The following is an example of how to approach switch debounce with a software solution. You'll notice that, though this is a rather simple function, the code can quickly become rather complicated. For this reason, it can often be simpler to use a hardware debounce scheme to achieve a clarity in the program and be able to dedicate memory and processing power to the functional parts of the program.
Code: Select all
int buttonPIN = 9; //input pin for button
int ledPIN = 13; //LED output pin
int buttonState = 1; //rest state of switch is pulled high
int ledState = 0; //initial LED state is off
//need to use long variables to store large values from millis()
unsigned long timeRead = 0;
unsigned long debounceTime = 300; //adjust this variable for suitable debounce
void setup()
{
pinMode (buttonPIN, INPUT); //set pin as an input
pinMode (ledPIN, OUTPUT); //set pin as an output
}
///////////////////////////////////////////////////////////////////////
int ledToggle() //this function will toggle the state of the LED on and off
{
if (ledState = 0) //if the LED is off
{
digitalWrite (ledState, HIGH); //turn the LED on
ledState = 1; //store the new state of the output pin (ON)
}
else //if the LED is on
{
digitalWrite (ledState, LOW); //tuen the LED off
ledState = 0; //store the new state of the ouput pin (OFF))
}
}
///////////////////////////////////////////////////////////////////////
int debounceWait() //this function will wait for the debounce time to pass
{
do
{
//do nothing while this condition is true
}while (millis() - timeRead < debounceTime); //has debounce time been reached?
}
///////////////////////////////////////////////////////////////////////
void loop()
{
//read current state of button
buttonState = digitalRead(buttonPIN);
//do this if the button has been pressed
if (buttonState = 0)
{
//store the time when button was pressed
timeRead = millis();
//perform the function resulting from button press
ledToggle();
//wait for the button to come to rest in closed position
debounceWait();
do
{
buttonState = digitalRead (buttonPIN); //read the state of the button
}while (buttonState = 1); //wait for button to be released
// once the button has been released, read the time
timeRead = millis();
//wait for the button to come to rest in open position
debounceWait();
}
}