Debouncing various sensors

I've hit upon a very nice debounce technique, which is done in software. Suppose you have a button, to be read on the pin named "button Pin". Define an unsigned long variable called "wait", and initially set wait = 0 in the setup() routine.

Now, use the following code when reading the button.

state = digitalRead(buttonPin);
if ((state == LOW) && (millis() > wait))
{
//Do whatever the button initiates.
wait = millis() + 500;
}

So, the result of this will be that the button may mechanically bounce, but the action associated
with the button will not occur again within 1/2 second. That is adequate time for most debouncing.

I have used this technique for buttons and interrupts, and it works very smoothly. Of course you can
do debouncing down in the microsecond range by using micros() as the clock.

Hope this is useful.

John Doner