Preventing extra calls to an interrupt function

Having a problem with an Arduino project. I'm using a piezo and MOSFET to trigger an interrupt, which then calls a function that turns on/off LEDs and such. In theory, when the piezo is 'tapped,' voltage is applied to the MOSFET gate. The interrupt is connected to Digital Pin 2, which is connected to the 'Drain' of the MOSFET. Essentially, when the piezo is tapped, the interrupt is called.

My problem is this: one tap on the piezo registers several values, sometimes as many as 4 or 5. I only want the 'first' value - the max value from each tap. I checked this on the serial monitor - one tap might give me (for example) readings of 855, 560, 301, and 85.

In this case, the 855 and 560 values both trigger "RISING" and this the interrupt is called. How can I fix this, so the interrupt is only called once per 'tap?'

The obvious solution is to add a delay in the interrupt function, but after doing some research I found I can't do this (something to do with the 'delay' function relying on interrupts itself).

I need to prevent the interrupt from being called multiple times from the same 'tap' - please help!

When the interrupt is triggered you can arrange that the first thing it does is turn off interrupts on that pin. You can also arrange for a Timer to start which causes another interrupt after a suitable period which then re-enables th interrupt on the pin.


If you look up debouncing it might give you some ideas. Typically it is used for buttons or relays whose contacts don't necessarily make instantaneous solid contact but the concept is similar to your problem.

one tap on the piezo registers several values, sometimes as many as 4 or 5.

This is because your sensor is bouncing mechanically and producing several signals. While you can suppress all signals for a time like the others have suggested you could also tackle the problem at source.

I am assuming you have a 1M resistor across the sensor, try reducing that to 200K or lower to suppress the bounces.