Multiple triggers on a single interruption on Arduino

442 views Asked by At

I am working on Arduinos and I would like to use interrupts to use a rotary encoder. But I would like to reduce the interrupt code to the minimum.

Can I use multiple triggers on a single interrupt?

I would like to replcace my actual code :

attachInterrupt(0, ChangeA, CHANGE);

To something like

attachInterrupt(0, FailingA, FAILING);
attachInterrupt(0, RisingA, RISING);

Is it possible?

1

There are 1 answers

1
UncleO On

No, not exactly possible. The external interrupt is configured to react only to one of the possibilities internally.

One thing you can do is to use a CHANGE interrupt service routine and test the value of the pin at the beginning of the ISR to do FallingA() or RisingA().

There is a potential problem here. The pin may have changed again before you test it, so the interrupt that triggered the interrupt could have been falling, say, and your test determines that it was rising. One way to guard against two quick interrupts is to check if the flag is still set. It should have been cleared if at the start of the interrupt, and if it is set then a change happened again. There is no practical way to guard against three quick changes.

If you really want to use two separate ISRs, do you have an extra pin available? If so, you could just wire the pins together and run a FALLING ISR on one and a RISING ISR on the other.