Arduino pushbutton - interrupt

Thread Starter

Prajeet Anand

Joined Aug 26, 2014
21
Im trying to use the arduino pushbutton to have a seperate function to be called when interrupted. Im having three push buttons and I have three functions respectively. Each pushbutton push should call its functions. I'm having difficulties executing this, because all the arduino interrupt can do is make a LED set high or low. Any info on this would be helpful.
 

danadak

Joined Mar 10, 2018
4,057
Normally good practice using interrupts is to enter the ISR,
set a flag, and return. Then based on flag status process that
interrupt request in main().

Reason for this is to get good/fast response by ISR request.
If you execute code, and f() calls inside ISR, you get a lot of stack
push/pop handling variables which is wasted machine cycles.


Regards, Dana.
 

djsfantasi

Joined Apr 11, 2010
9,163
If you can write a function for the Arduino to do what you want, then it can be attached to an interrupt. The function is called an ISR or Interrupt Service Routine.

In your case, an interrupt can be defined for a pushbutton.

It’s advisable to have the interrupt do as little as possible. One reason is that other interrupts that occur while the ISR is processing, are ignored. Another reason is that delay()s don’t work. Programmers set a flag and test that in the main loop.

The Uno and Nano only have 2 interrupt pins. You have three pushbuttons, so I’d advise using regular digit pins and diode Or them to an interrupt pin (2 or 3).

Are these mechanical pushbuttons? If you need to debounce then do so in the main loop when a flag has been set.
 

danadak

Joined Mar 10, 2018
4,057
If you can write a function for the Arduino to do what you want, then it can be attached to an interrupt. The function is called an ISR or Interrupt Service Routine.

In your case, an interrupt can be defined for a pushbutton.

It’s advisable to have the interrupt do as little as possible. One reason is that other interrupts that occur while the ISR is processing, are ignored. Another reason is that delay()s don’t work. Programmers set a flag and test that in the main loop.

The Uno and Nano only have 2 interrupt pins. You have three pushbuttons, so I’d advise using regular digit pins and diode Or them to an interrupt pin (2 or 3).

Are these mechanical pushbuttons? If you need to debounce then do so in the main loop when a flag has been set.
Arduino can in fact have nested interrupts -

https://stackoverflow.com/questions/5111393/do-interrupts-interrupt-other-interrupts-on-arduino


Regards, Dana.
 

Thread Starter

Prajeet Anand

Joined Aug 26, 2014
21
If you can write a function for the Arduino to do what you want, then it can be attached to an interrupt. The function is called an ISR or Interrupt Service Routine.

In your case, an interrupt can be defined for a pushbutton.

It’s advisable to have the interrupt do as little as possible. One reason is that other interrupts that occur while the ISR is processing, are ignored. Another reason is that delay()s don’t work. Programmers set a flag and test that in the main loop.

The Uno and Nano only have 2 interrupt pins. You have three pushbuttons, so I’d advise using regular digit pins and diode Or them to an interrupt pin (2 or 3).

Are these mechanical pushbuttons? If you need to debounce then do so in the main loop when a flag has been set.
Thanks for the info djsfantasi
 

Phil-S

Joined Dec 4, 2015
238
Check out Nick Gammon's thorough treatise on interrupts on his own website gammon.au.
Covers all the obvious and not so obvious
 
Top