Can anyone tell me how to clear an interrupt stack on a mega?

Can anyone tell me how to clear an interrupt stack on a mega? I have a bouncing button that is causing problems.

What do you mean by “clear an interrupt stack”?

For reading pushbuttons, the Bounce library is usually best. It doesn’t need any interrupts at all.

I’m sure you are right about the Bounce library, and that may be the way to go, but if this can work I’d like to try it.

As I understand it, if a series of interrupts are registered by the arduino but not processed immediately, they set a series of flags saying that an interrupt happened. These then get processed in order, assuming they all have the same priority. So that is what I meant by an interrupt “stack”. They are software flags, so there should be a way to clear them…turn them off.

The reason for using interrupts is the idea that during a signalled interval (when the led in the button is lit) pushing the button will cause something to happen…but at an uncontrolled moment. The user pushes the button at will.

As I said, there are other ways, but if I can clear the stack before each ISR attachment it will be an easy fix.

And by the way, Craig Dorety speaks well of you and says I should move future projects to the Teensy.

I believe the technical term you want is “pending”. An interrupt is said to be “pending” if the hardware has detected the event and set a bit to remember it, but the interrupt code has not yet run, for whatever reason.

I’m pretty sure both AVR and ARM hardware remembers only a single pending event per interrupt source. If the hardware event happens many times before your interrupt code can run (eg, you’ve disabled interrupts, or another interrupt routine is running which prevents the hardware from causing your to run), I believe more detected events just set the same bit that’s already set. The hardware doesn’t remember the event happened more than once before your code could run.

On AVR, there are flags for each interrupt. I’ve never tried disabling interrupts and then writing to the flag after it’s been set, to try preventing the ISR from running when interrupts are re-enabled. But my understanding is that should be possible.

But just because you can do a thing doesn’t necessarily mean you should. Unless you have some very special circumstances, just using the tried and true Bounce library is probably much better.

And yes, you should totally consider Teensy for future projects! :slight_smile:

I believe that Paul’s correct: the AVR has a one-interrupt-deep queue only.

turn the interrupt off after processing the button press and turn it back on after 50ms to debounce it

If you’re going to delay for 50 ms, why bother with interrupts?

Well, this is even weirder than I thought. A single button press produced a normal execution of the ISR, followed by 17 repetitions in each of which the ISR executed as soon as the interrupt was attached, with the button not touched. And then it went back to normal. I checked the voltage at the button pin and the button is not sticking. It’s on a pullup, and the interrupt type is set to “falling”.

This thing leaves for the playa two weeks from today, so I don’t really have time to chase it down at the moment. I’ll switch to a polled, debounced version and tell people to keep pushing the button. But I’m really curious as to what is going on.

Thanks for your time.

How are you measuring the voltage?
A meter will never ever see the bounce, a decent scope will though. Also a counter/timer would also count the ‘buzz’ of the contacts.

Have a read of this very good article, won’t take you long:
http://www.ganssle.com/debouncing.htm

Thanks, Adam…interesting article.

I should have mentioned above that in my test earlier today I stretched out the timing on the attach/detach cycle. Those spurious calls to the ISR were coming six minutes apart.

Paul. glad the article was of interest.

Your problem sounds like a memory corruption issue. Or you are getting noise induced somewhere onto the I/O pin.
That’ll be boring, looking for a 6 minute event :frowning:
You’ll need to decide in the ISR if it was actually an interrupt that caused entry to the ISR (check the source flag bit correctly set) or from some other source, like a duff value on the stack (flag bit will not be set).

Yeah, if you want a button to just work properly with minimal effort, the Bounce library delivers!!!