Stop multiple interrupts firing

Hi,

Doing some work and need a bit better response time for button switch detection, so I have turned to using Interrupts.

Now I have got a few examples from here but all these examples detect multiples times for a single keypress.

I read that I could use the ISR to set a variable and monitor that variable in the main loop to cut out the multiple detections. But this still detects multiple times for me.

Here is the code for ISR setting a variable. On the button I have a 10k resistor grounding it

const int buttonPin = 2;    // the number of the pushbutton pin
const int ledPin =  13;    // the number of the LED pin

// variables that will change:
volatile int buttonState = 0;                // variable for reading the pushbutton status
volatile boolean button_pressed = false;  // variable that the ISR will change that loop() can monitor

void setup() {
  Serial.begin(57600);
  Serial.println("Starting...");
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  attachInterrupt(0, pin_ISR, CHANGE);
}

void loop() {
  if (button_pressed) {
    button_pressed = false;
    Serial.println("Key Press");
  }
}
void pin_ISR() {
  buttonState = digitalRead(buttonPin);
  if (buttonState !=0) {
    button_pressed = true;
  }
  digitalWrite(ledPin, buttonState);
}

When it is ruinning, pressing the button will produce either 1 detection (displayed from the serial print in the main loop) or with will produce several 2 to x detections depending on the interrupt type (RISING, FALLING, CHANGE)

What is the best way to just detect the keypress once.

I am wanting to detect a single keypress and then run some time critical code to measure but with the multiple detections it is running the time critical code many times.

Thanks

It is very unusual to need interrupts to detect button-presses. Humans are verrryyy sloowwww. Polling is usually perfectly sufficient.

One way to deal with switch-bounce is to ignore further actions for a short period - perhaps 50 millisecs. Maybe something like this

if (millis() - lastSwitchDetectedMIllis > debounceInterval) {
   // treat this as a new action rather than a bounce
}

This concept works with or without interrupts. It is used (without interrupts) in Several Things at a Time

...R

Edit to correct deboundInterval - it should be debounceInterval

thank you Robin.

So it's called bounce. I shall remember this. Thanks again

Bounces may number from none to many hundreds, the usual switch contacts settle down within a few hundredths of a second though. Any mechnical bounce of few nanometres or more will break contact at
5V.

It would be good practice to use debounce hardware when using an ISR, otherwise multiple bounces can
steal lots of processor cycles and might interfere with the rest of the code, with worst case bounce
behaviour.

SniffTheGlove:
Doing some work and need a bit better response time for button switch detection, so I have turned to using Interrupts.

Now I have got a few examples from here but all these examples detect multiples times for a single keypress.

So now you know why using interrupts to respond to switch presses is such an awful idea! :roll_eyes:

If you are having problems responding to pushbuttons in your code, clearly your code is at fault! :grinning:

Perhaps you should resolve the XY problem.