Detach Interrupt doesn't seem to be quite working..

Hi there, I'm very new to arduino and I'm working on my first project. The project has a HID badge reader connected to the arduino (uno) via data pins 2 and 3. I'm also using interrupts for those data pins:

#define MAX_BITS 35
unsigned int    badgeBinaryData[MAX_BITS];
volatile unsigned int    bitCount;
volatile unsigned long   badgeNumber;
unsigned int    data0pin = 2;
unsigned int    data1pin = 3;


attachInterrupt(digitalPinToInterrupt(data0pin), ISR_INT0, FALLING);  // Arm data0pin with interrupt sense, triggering on falling edges
attachInterrupt(digitalPinToInterrupt(data1pin), ISR_INT1, FALLING);  // Arm data1pin with interrupt sense, triggering on falling edges 

void ISR_INT0(void){

  bitCount++;

}

void ISR_INT1(void){

  badgeBinaryData[bitCount] = 1;
  bitCount++;

}

The interrupt is used to capture the serial data from the HID badge reader when I tap my badge. After collecting the data that I needed, I would like to disable interrupts on these two data pins. I use. the detachInterrupt() function. I did not use the noInterrupt() function because I am also using the timer1/counter module as an interrupt and need it to run elsewhere in my project.

My problem is, after detaching interrupts to data pins 2 and 3, if I were to tap my badge again, my program freezes/stops running. I'm extremely stuck here. I consider this as a 'bug bug' in my project and would like to fix it.

Any advice would be greatly appreciated! Thanks!

You can't use detach with ISR's Just as you don't use attach with them.

Mark

holmes4:
You can’t use detach with ISR’s Just as you don’t use attach with them.

Mark

Hi Mark, thanks for your reply.

So if I’m understanding this correctly, I should not use ISR_INT0 and ISR_INT1 in the attach/detach functions?

I changed my attachInterrupt to a different function call (e.g. data0Interrupt, data1Interrupt), however that did not fix my problem.

William

whuang73: My problem is, after detaching interrupts to data pins 2 and 3....

The code you initially posted doesn't detach the interrupts.

jboyton: The code you initially posted doesn't detach the interrupts.

Hi jboyton,

Sorry, my fault. I actually do have the detachInterrupt() in my code, I just didn't post it.

Anyways, I managed to find a solution to my problem. Instead of using detachInterrupt, I wrote 0x00 to the external interrupt mask register.

And if I were to re-attach my interrupts, I would need clear the external interrupt flag register.

I have one more question. I'm curious as to why I can't/should not use ISRs with attach and detach.

Thanks, William

That's because the ISR is just the name of the process that the interrupt jumps to when an interrupt occurs.

whuang73: I have one more question. I'm curious as to why I can't/should not use ISRs with attach and detach.

Because the two ways work in different and incompatible ways. You can either use attachInterrupt and detachInterrupt with an ordinary function that takes void and returns void, OR you can use the ISR macro. But mixing the two can create issues.