Multiple Hits with Debounced Switch on Interrupt

Hello, I have a hardware debounced switch with a Schmitt trigger. I run the following code:

int buttonInt0 = 0; //digital pin2
int mode;

void setup() {
//attach interrupt
Serial.begin(9600);
attachInterrupt(buttonInt0, button_down, FALLING);
}
void button_down()
{
mode = 1;
}
void loop() {
if (mode == 1)
{
Serial.println("UP");
mode = 0;
}
}

When I open the serial monitor and depress the switch "UP" is printed 6 or so times. I would expect it should only be printed once. Why is this? I checked my switch output on the scope and it looks clean.

A couple of questions. The values that you assign to mode are 0 and 1. Those values both fit in a byte.

You are referencing mode in both loop() and the ISR, and yet mode is not declared volatile. Why not?

In short, I don't know any better. :slight_smile:

What you have to understand is that the resolution of digital electronics is a few nanoseconds, and things like contact-bounce in switches are on a timescale of milliseconds. Thus every bounce of the metal contacts in the switch will cause a new switching transition on the pin that's generating interrupts.

The solution to this contact bounce is not a schmidt-trigger (thats only needed for slowly varying waveforms), but is either

  1. software debouncing - ignore all transitions that occur within a certain time of the first transition. Timescale of 10ms would normally surfice.

  2. pass the output from the switch through a low-pass-filter to smear-out the bounces, then pass that signal through a schmidt-trigger to turn it back to a digital signal.

Perhaps I will be more specific. Here is the circuit I am using: How to debounce a switch
It is a combination schmitt trigger with a rc circuit.