Sharing interrupt pin with two modules

Hello, I am learning to get two modules to share the interrupt pin 2 on the Nano.

My code is like this:

define moduleA_INTERRUPT_PIN 2

define moduleB_INTERRUPT_PIN 2

loop () { attachInterrupt(digitalPinToInterrupt(moduleA_INTERRUPT_PIN), doModuleA, RISING); detachInterrupt(digitalPinToInterrupt(moduleA_INTERRUPT_PIN));

attachInterrupt(digitalPinToInterrupt(moduleB_INTERRUPT_PIN), doModuleB, RISING); detachInterrupt(digitalPinToInterrupt(moduleB_INTERRUPT_PIN)); }

Am I approaching this the correct way? Any suggestion will be greatly appreciated.

How will the program know which module caused the interrupt?

No, repeatedly attaching and detaching the interrupt is not what you want to do. Write one handler that handles things and attach it once in setup.

For more specific help add some specifics about the project.

Your approach won't work because you keep attaching and detaching different interrupt functions for interrupt on pin 2 (interrupt 0) but when and if you are lucky and a rising edge is caught, you will never know which real hardware sent that signal to the pin and you'll be executing a random function for module a or b

Why don't you use the 2 interrupts pins, one for each module?

If you really need to share an interrupt pin between 2 devices, the you could use an 74LVC1G32 Single 2-input OR gate, have your 2 devices at the input side as well as also routed to 2 different pins of the Arduino and attach 1 interrupt function

When one of the 2 lines rises, the or will deliver a one (those chips are fast, just a few tens of nano seconds) and trigger the interrupt. In the interrupt function you need to find out which device really triggered the interrupt, so you go read the 2 pins that are attached to the or input and find out which one(s) is/are high. Then you will know which module raised the IRQ and you take the appropriate action.

If your interrupts are very transient and super fast to rise and drop, that might be a problem as by the time you go perform the digitalReads of the2 pins, the state may have changed... So not a 100% foolproof solution but would work for most standard cases where the interrupt pin stays high for at least a hundreds of microseconds

Thanks you all for your reply.

J-M-L: Your approach won't work because you keep attaching and detaching different interrupt functions for interrupt on pin 2 (interrupt 0) but when and if you are lucky and a rising edge is caught, you will never know which real hardware sent that signal to the pin and you'll be executing a random function for module a or b

Why don't you use the 2 interrupts pins, one for each module?

The reason of wanting to share the interrupt is I want to use a Nano with a LCD screen and a MPU6050. The LCD takes up pin2 and 3 and the MPU6050 takes pin2, but the Nano has only tow interrupt pins, so the only way to get them to work together is to share an interrupt pin

You can use pin-change interrupts. http://www.gammon.com.au/interrupts

These are available on all pins.

The LCD takes up pin2 and 3

Are you saying that the LCD uses 2 interrupts or that it just happens to use pins 2 and 3 ? If the latter then can't you just use two other pins instead ?

UKHeliBob: Are you saying that the LCD uses 2 interrupts or that it just happens to use pins 2 and 3 ? If the latter then can't you just use two other pins instead ?

I am not sue if the LCD uses pin2 and pin 3 as interrupt or other purposes, But nonetheless, they are being used. Once I disconnected anyone of them, the LCD stops working.

But you did give me an idea, perhaps I can change the codes to re-assing them to different pins. But Iam not sure how to go about doing that, perhaps it is in the library .cpp code?

Post a working program that uses the LCD.