The preceding lab discussed the electrical connection to an input pin, but there is still a mechanical problem that needs to be dealt with. When two contacts of the switch hit together, they tend to bounce off of each other a few times before settling down. Most mechanical switches have this bounce problem. You have probably never noticed this bouncing because it only lasts about 1/100 of a second. The problem, however, is that the MicroStamp11 is fast enough to see each of these bounces as a separate hit. So if you are trying to create a system that counts the number of times a button is pressed, you might count individual presses as multiple hits.
The solution to this problem is called debouncing.
Debouncing can be done in many ways. One may, for
instance, use special input circuitry on the switch. It
is, however, easier for us to do the debouncing in
software. In particular, we simply deactivate the switch
for a specified length of time after the first contact is
made. This approach to software debouncing is what you can
use in the lab. In particular, we've provided a couple of
special functions in the lab's kernel.c
source file
that you can use to debounce a switch and to count the
number of times a button was pushed. The function that
you'll use to test the state of an input pin is
button()
. This function, essentially, does nothing
more than wait for the input pin to go high and then it
"waits" for 10 milli-seconds before continuing. The
implicit assumption, of course, is that any switch bouncing
will be over once this 10 milli-second interval is over.