Electronic – Arduino interrupts for button input

arduinoavrinterrupts

I have 6 buttons on my printed board, and I'd like to use interrupts on them all, but they don't correspond to the interrupts listed on the specs.

Is it true that you cannot choose arbitrary pins to use as interrupts on Arduino boards (specifically the Mega 2560)? If not, I am a bit sadface – any pragmatic workarounds?

Best Answer

You can do what Majenko suggested, but all it takes is a diode per button to make the ORed line. I'm assuming the buttons are normally open and tied between ground and a pullup resistor. The ORed line is in addition to the individual button lines, which still need to be wired to processor pins. However, the advantage is that the ORed line will go low when any of the buttons are pressed. This can be used to wake the processor or cause a interrupt, which then has to look at the other lines to see which buttons are really pressed.

Another way is to not use button interrupts at all. Just scan the button lines every few ms. The rough rule of thumb is that 50 ms or less feels instantaneous to a human user. In other words, if you press a button and whatever it does is delayed by 50 ms, it still feels instantaneous to you.

If the point is to save power, you can actively drive the high side of the pullups. Keep them low most of the time so there will be no current even if a button is pressed. Turn on the pullups for maybe 10 µs before sampling the buttons, then turn them off again. This can be done in a short routine the processor wakes to every 10 ms, for example, when nothing is otherwise going on. The average power will be low since it will only be on for a few 10s of µs every few 10s of ms.