The problem is mechanical switches bounce when pushed, and things happen fast enough in a microcontroller that you can get more than one reading while it's bouncing. You could try reading it, waiting some period of time, then checking to see if the value changed. Do that until you get the same value.

I'm not a good one to explain this, I don't understand the details well enough. There are some interesting threads about this on the avrfreaks web site, and this has probably more information than you want: http://www.ganssle.com/debouncing.pdf

I would expect my code to deal with debounce as interrupt event is triggered by buttonDown action, however it has to wait for button to be released to carry on executing interrupt routine, and then at the end it does nothing for ~2s (delay). So if first interrupt is triggered by first falling edge detected on input pin, how come the second event occurs after 2 second delay?

Logged

"Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian W

When the switch bounces up, this loop will end and the following code will execute. Then the ISR will be called again when the switch bounces down. I would think the 2ms delay at the end of the isr would deal with that, but I've heard of switches that take longer to stop bouncing.

I don't know that this is the case, but there are a lot of people who put a lot of effort into debouncing because it's tricky.

When the switch bounces up, this loop will end and the following code will execute. Then the ISR will be called again when the switch bounces down. I would think the 2ms delay at the end of the isr would deal with that, but I've heard of switches that take longer to stop bouncing.

I understand what You say, however, the delay is NOT 2ms, it is 2s(2000ms). You can set my hair on fire and call me a princess, but bouncing does not last for 2 seconds. Also, after first interrupt trigger, that interrupt becomes disabled as long as it's function is running.

I'll try to use RC de-bouncer to see if it really is related to bouncing button.

Logged

"Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian W