A pushbutton is a Mechanical Switch. Switches play an important and extensive role in practically every computer, microprocessor and microcontroller application. Mechanical switches are inexpensive, simple and reliable. However, switches can be very noisy electrically. When a switch, such as a pushbutton is pressed, the connection actually makes and breaks several, perhaps even hundreds, of times before the final switch state settles. The problem is known as switch bounce.
The consequences of uncorrected switch bounce can range from being just annoying to catastrophic.
A software technique called "Debouncing" is one way to solve this problem. Here's how it works:
Usually, the switch is connected to a pin on the Microcontroller so that it maintains either a HIGH or LOW condition when the switch is open (not pressed). If the switch closes (is pressed) then that condition will change. The Microcontroller detects that change. Instead of reacting right away, the Microcontroller will wait a period of time, say 5 mS, to give the switch a chance to stop bouncing and then checks that pin again. If the pin still reads that the switch is closed, then the Microcontroller will do something about it.
In this tutorial, a pushbutton press (BTN1) will trigger LD1 to light. Otherwise, LD1 will be OFF.
This tutorial introduces the if/else statement. This type of statement gives your sketch the ability to make decisions based on conditions occurring outside of the Microcontroller or as a result of some sort of internal condition such as the result of some mathematical operation.
We recommend you read the following before proceeding:
The hardware used in this tutorial will be the chipKIT uC32 along with a chipKIT Basic I/O Shield both manufactured by Digilent Inc. The reference guide for each board, schematics and other resources are available on their individual homepages:
Create a new sketch in MPIDE.
Save the sketch with a meaningful name.
Add the following code to your new sketch:
int ledPin = 26; // LED connected to digital pin 26
int pbPin = 4; //pushbutton BTN1 connected to digital pin 4
int pbState = LOW; //this variable will hold the value or state of BTN1
// i.e. if not pressed, pbState = LOW, if pressed, pbState = HIGH
void setup()
{
pinMode(ledPin, OUTPUT); // sets the digital pin 26 as output
pinMode(pbPin, INPUT); //sets digital pin 4 as input
digitalWrite(ledPin, LOW); //make sure LD1 is OFF
}
void loop()
{
pbState = digitalRead(pbPin); //read the value, HIGH or LOW, on the pushbutton pin
if (pbState == HIGH) //is pbState HIGH indicating BTN1 is pressed??
//if yes, do the code inside the curly braces. This is the debounce section
{
delay(5); //wait 5 mS for any pushbutton bounce to disappear
pbState = digitalRead(pbPin); //read the value on the pushbutton pin again
if(pbState == HIGH) //is it still HIGH (pressed)
{
digitalWrite(ledPin, HIGH); //Light LD1>
}
}
//otherwise
else
{
digitalWrite(ledPin, LOW); //keep LD1 OFF
}
}
In the MPIDE, verify that the chipKIT uC32 and the associated communication port are selected.
Upload the code to the chipKIT Board.
After the sketch is sent to the chipKIT uC32 board's Microcontroller, LED 1 (LD1) on the chipKIT Basic I/O Shield should light when pushbutton BTN1 is pressed.