[Answered] Using interrupts to read buttons (not D2 or D3)

I would like to setup an interrupt, to be called when a button is pressed. I would rather not want to use D2 or D3 as they may be needed for other purposes (communication, etc). I also need to read 3 rather than 2 buttons.

Currently I am thinking about implementing this, using D8-D10.

The only page, that I can find is this: http://playground.arduino.cc/Main/PinChangeInterrupt

But it seems a bit crude. Is it not possible to use addInterrupt()?

If you have a link to an example or two, I would love to see them! :smiley:

Thanks!

I would like to setup an interrupt, to be called when a button is pressed.

Why? Interrupts are for things that are transient in nature, and of short duration - like pulsing a pin when serial data arrives. You can’t afford to miss that input. A switch press, on the other hand, is generally a relatively long duration event. Polling switches works just fine 99.999% of the time.

If you expect to stop what you are doing when the interrupt happens, and start doing something else, you don’t understand interrupts.

JanHolbo:
I would like to setup an interrupt, to be called when a button is pressed.

Normally you'd AVOID using interrupts with mechanical (bouncing) buttons.

Reading buttons is a thing you'd better do from "normal code". Mechanical buttons are slow devices, so there is no problem handling them from normal code.

At least if you want to use other interrupts in your program as you told: "not want to use D2 or D3 as they may be needed for other purposes".

The problem with interrupts from bouncing buttons may be: Dependent on the way the button is pressed and dependent on the quality of the contacts, a dozen interrupts may occur in very short time with just a single button press. This interrupt cascade will affect the timing of ALL OTHER INTERRUPTS in your system as only one interrupt at a time can be active. So the dozen interrupts from your bouncing button press may affect RF communication, serial communication, servo rotation, hardware timers and everything that is driven by interrupts. You not only want to AVOID interrupts with reading buttons, you want STRICTLY AVOID interrupts with mechanical buttons, if you don't want to disturb all the other interrupts running on your controller.

This is the arduino-pinchangeint library : Google Code Archive - Long-term storage for Google Code Project Hosting.

However, PaulS and jurs are right, don't use interrupts for a simple button.
About 20 or 30 years ago it was normal to connect buttons to interrupts. In the Arduino environment there is no need to do that.

Use the Bounce2 library : http://playground.arduino.cc/code/bounce

The code in planning and implementing a program reads buttons and does not need any special debounce code.

…R

Thank You all!

My reason for using an interrupt was to try to keep code lean and clean, both for execution and for reading purposes. But with your above input I think I am the wiser, and will avoid using interrupts for this use :slight_smile:

Again Thank You :slight_smile:

JanHolbo:
My reason for using an interrupt was to try to keep code lean and clean, both for execution and for reading purposes. But with your above input I think I am the wiser, and will avoid using interrupts for this use :slight_smile:

If you really want to use interrupts with reading mechanical buttons, you can.
But then do not use hardware interrupts and do not use pinchange interrupts.

Use an timer interrupt instead! Let the timer go with an interrupt rate slower than the bouncing time and read the buttons (i.e. 10ms timer). But normally you'd not do that with simple pushbuttons or switches, only with 'mechanical rotary encoders', which may provide relatively quick signal changing while rotating the knob.

On the one side, hardware and pinchange interrupts may lead to unwanted slowing down the loop() code while dozens of interrupts may occur with bouncing buttons as well as dozens of interrupts in a very short time may affect/delay the handling of all the other interrupts.

On the other side, interrupts are scarce resources on an Atmega controller and you'd perhaps like to save them up for usage when they are really needed.