The software debounce example in the playground is a great tutorial if you want to learn abount software debouncing.
If you need your CPU to other things however it is pretty useless.
Below is a small skecth that will debounce an ISR serviced button (pin 2) with minimal overhead.
/*
* ISR Debounce
*/
// use 2ms debounce time
#define DEBOUNCE_TICKS (word)microsecondsToClockCycles(2000)
extern volatile unsigned long timer0_overflow_count;
word keytick; // record time of keypress
void setup() {
attachInterrupt(0,KeyPress,FALLING);
}
void KeyPress() {
keytick=(word)timer0_overflow_count;
}
// returns true if key pressed
boolean KeyCheck() {
if (keytick!=0) {
if (((word)timer0_overflow_count-keytick)>DEBOUNCE_TICKS) {
keytick=0;
return true;
}
}
return false;
}
void loop()
{
if (KeyCheck()) {
// do something useful
}
}