Go Down

Topic: Can I attach more than on handler to a interupt pin? (Read 346 times) previous topic - next topic

wyx2001

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.

Robin2

Why would you want to?

An Arduino can only do one thing at a time.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

wyx2001

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.

Robin2

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
Two or three hours spent thinking and reading documentation solves most programming problems.

ron_sutherland

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)

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/
I use an R-Pi Zero on an RPUpi shield to have a tool-chain at the network edge.

PaulS

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.
The art of getting good answers lies in asking good questions.

DrAzzy

Or just attach a function that calls both the functions you want to attach to it...
ATtiny core for 841+1634+828 and x313/x4/x5/x61/x7/x8 series Board Manager:
http://drazzy.com/package_drazzy.com_index.json
ATtiny breakouts (some assembled), mosfets and awesome prototyping board in my store http://tindie.com/stores/DrAzzy

PaulS

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.
The art of getting good answers lies in asking good questions.

Robin2

If you know what the currently attached function is,
Why wouldn't you?

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

ron_sutherland

#9
Mar 14, 2019, 10:28 pm Last Edit: Mar 21, 2019, 03:22 am by ron_sutherland Reason: typo and rm fuzz
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).
I use an R-Pi Zero on an RPUpi shield to have a tool-chain at the network edge.

PaulS

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.
The art of getting good answers lies in asking good questions.

Robin2

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
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up