attachInterrupt only working sometimes?

Okay, I have a program that has a routine in the loop() that calibrates my circuit. Before I start calibrating, I detach both interrupts because I'm using pulseIn(). After my calibration routine, I attach my interrupts again. Problem is that only one of my interrupts will attach. It doesn't matter which order I attach or detach them in, it will always only attach the interrupt for pin 2.

The other odd part about this is, if I comment out the lines for the interrupt, compile, then uncomment, compile, both interrupts will work as they should. Below is a snippet of my code. Note, I do not attach or detach anywhere else in code.

if(mode == CALIBRATION_MODE)
  {
    detachInterrupt(0);
    detachInterrupt(1);
    turnOffAllOutputs();
    Serial.write("Calibrating\n");
    digitalWrite(calibrateLED,HIGH);
    digitalWrite(readyLED,LOW);
//    calibrateResult = calibrate();
    calibrateResult = quickCalibrate();
    if(calibrateResult >= 0)
    {
      offset = calibrateResult;     //Set global offset
      Serial.print("Offset: ");
      Serial.print(offset);
      Serial.print("\n");
      mode = COMMUNICATION_MODE;    //Change to communication mode
      lastCommTime = millis();
      digitalWrite(calibrateLED,LOW);
      digitalWrite(readyLED,HIGH);
      attachInterrupt(1,sendComm,CHANGE);
      delay(100);
      attachInterrupt(0,receiveComm,CHANGE);
      //delay(100);
    }
  }

Any ideas would be greatly appreciated. -Jeremy

Problem is that only one of my interrupts will attach

How do you know?

Because when the second interrupt catches I should be turning on an LED when the signal goes high. I’ve watched the signal on an oscilloscope and it is changing like it should but I get no change in my LED. I’ve manually tested my circuit outside of the interrupt so I know electrically, it is fine.

Has anyone else seen this issue?
-Jeremy

P.S. Currently, as a workaround I took out the detach and moved the attach to setup(). This isn’t the best workaround because I use pulseIn() and the interrupt will mess with this.

The code for attachInterrupt / detachInterrupt is very simple. So long as you're passing valid parameters, there is no way either function can fail. The problem is [u]very[/u] likely somewhere else.

Because when the second interrupt catches I should be turning on an LED when the signal goes high

Did you accidently change the LED pin to an input? That would explain why it doesn't turn on.

The LED pin is setup as an output.

Has anyone else seen this issue?

I think the answer here is - No - attach/detach interrrupt is working as expected in Arduino 0018 (Duemillanove) and I don't recall any issues with prior versions either.

So the advice from Coding Badly to look eleswhere in your code makes sense.

Some things to try:

  • Increment a global volatile variable (one for each) whenever interrupts fire and print these in your loop for debugging whenver they change (seeing is believing).
  • Check that the code turning on the Led is indeed called and that pinMode is correct.

P.S. Currently, as a workaround I took out the detach and moved the attach to setup(). This isn't the best workaround because I use pulseIn() and the interrupt will mess with this.

After you call detachInterrupt, the interrupt will actually still fire - only the handler code you supplied will not be called. Just servicing the interrupt with the basic interrupt handler however carries overhead and may still upset the pulseIn function.