Which pin caused interrupt?

I have several pins attached to the same interrupt handler:

attachInterrupt(pin_1, interrupt_handler, CHANGE);
attachInterrupt(pin_2, interrupt_handler, CHANGE);
attachInterrupt(pin_3, interrupt_handler, CHANGE);

Since I can’t do something like pass an additional parameter to the interrupt handler:

attachInterrupt(pin_1, interrupt_handler(1), CHANGE);

Is there a way to figure out which pin caused the interrupt inside the handler?

AFAIK, not really since the system does clear the HW flags (and introduces ~1µs+ latency) prior calling your handler .

The easiest way would be to donate one ISR per pin to register which pin actually fired and from that ISR you can call a generic function that does the common jobs.

Or, sample the pins (using pinReadFast() for example) to see which one changed, causing the interrupt. This will require that you maintain the previous pin states.

and that the trigger pulse is considerably longer than the interrupt latency (>5µs should do)

1 Like