Arduino Forum

Using Arduino => Interfacing w/ Software on the Computer => Topic started by: wyx2001 on Mar 06, 2019, 06:54 pm

Title: Can I attach more than on handler to a interupt pin?
Post by: wyx2001 on Mar 06, 2019, 06:54 pm
attachInterrupt, can I attach more than one handler? The ideal way is check to get current handler attached, and then attachInterrupt to my own handler and at then end of my handler, I can call the original handler.
Title: Re: Can I attach more than on handler to a interupt pin?
Post by: Robin2 on Mar 06, 2019, 08:11 pm
Why would you want to?

An Arduino can only do one thing at a time.

...R
Title: Re: Can I attach more than on handler to a interupt pin?
Post by: wyx2001 on Mar 06, 2019, 09:07 pm
Why would you want to?

An Arduino can only do one thing at a time.

...R
My board now installed with RCSwitchFirmata, it can handle RC signals. I want it to handle other signal like those from acurite temp sensor.
Title: Re: Can I attach more than on handler to a interupt pin?
Post by: Robin2 on Mar 06, 2019, 11:04 pm
My board now installed with RCSwitchFirmata, it can handle RC signals. I want it to handle other signal like those from acurite temp sensor.
You need to provide a lot more information. Describe the project you are trying to create.

What is RCSwitchFirmata?
What do you mean by RC signals?

Why would you need an interrupt to deal with a temperature sensor? Temperatures usually change slowly.

Posting your program would be a big help.

...R
Title: Re: Can I attach more than on handler to a interupt pin?
Post by: ron_sutherland on Mar 12, 2019, 05:14 am
attachInterrupt takes a pointer to a function and saves it. It can then be used as a callback function.

https://en.wikipedia.org/wiki/Callback_(computer_programming) (https://en.wikipedia.org/wiki/Callback_(computer_programming))

If you give attachInterrupt a new pointer it will not keep the old one, there is room for just one pointer value per pin, so one callback function's address can be registered per pin.

https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/ (https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/)
Title: Re: Can I attach more than on handler to a interupt pin?
Post by: PaulS on Mar 12, 2019, 01:08 pm
Since attachInterrupt() takes a pointer to a function, you could write a function to get the currently registered pointer, or NULL if there is no registered function. Then, you could register your function. Before your function ends, you could then call the previously registered function.

Not that this would necessarily be easy, mind you, but it could be done.

If you were adventurous, you could even change the process to have a list of pointers to functions, for each interrupt. Attaching a handler would simply add the pointer to the list. Detaching a handler would simply remove the pointer from the list. When an interrupt happened, each handler in the list would be called, in the order registered.

Not that I would do something like that, but it IS possible.
Title: Re: Can I attach more than on handler to a interupt pin?
Post by: DrAzzy on Mar 14, 2019, 01:00 am
Or just attach a function that calls both the functions you want to attach to it...
Title: Re: Can I attach more than on handler to a interupt pin?
Post by: PaulS on Mar 14, 2019, 10:48 am
Or just attach a function that calls both the functions you want to attach to it...
If you know what the currently attached function is, that would work. If you don't, well there's a creek, a boat, and no paddle.
Title: Re: Can I attach more than on handler to a interupt pin?
Post by: Robin2 on Mar 14, 2019, 03:09 pm
If you know what the currently attached function is,
Why wouldn't you?

...R
Title: Re: Can I attach more than on handler to a interupt pin?
Post by: ron_sutherland on Mar 14, 2019, 10:28 pm
When the callback is run interrupts will be disabled so I think the best advice is to keep it as short and simple as possible, in other words, set a flag (or some such thing) and let the interrupt finish. Then look for the flag in the main loop and do whatever jobs are needed from there (I think that is the correct advice).
Title: Re: Can I attach more than on handler to a interupt pin?
Post by: PaulS on Mar 20, 2019, 11:39 am
Quote
Why wouldn't you?
Do you know what every library you use does? Most users here do not. A library could attach an interrupt handler, and most people wouldn't know.
Title: Re: Can I attach more than on handler to a interupt pin?
Post by: Robin2 on Mar 20, 2019, 02:41 pm
Do you know what every library you use does? Most users here do not. A library could attach an interrupt handler, and most people wouldn't know.
I have been foolishly assuming that this Thread is about interrupt handlers that the OP has created.

...R