EDIT: The problem is an interrupt is happening when it is not expected. Actually, as Dave explains, it's being triggered before the attach call, not after the detach call (which I first thought). There is now a work-around available.
I wrote a simple sketch to play with interrupts. When the led is on, the interrupt is attached. When the led is off, the interrupt is detached. However, if I pulse the input pin low while the interrupt is detached, the isr still gets called. Any insight?
Let's see if this "copy for forum" option works.
volatile long interrTime; //micros() when isr0 is called
void setup() {
pinMode(2, INPUT); //interrupt 0
pinMode(13, OUTPUT); //LED
digitalWrite(2, HIGH); //pullup 2
Serial.begin(9600);
}void loop() {
digitalWrite(13, HIGH); //led on
attachInterrupt(0, isr0, FALLING); //enable interrupt
delay(3000); //wait 3 seconds
detachInterrupt(0); //disable interrupt
digitalWrite(13, LOW); //led offSerial.print("interrTime = " ); //print time of isr being called. "0" if not called.
Serial.println(interrTime, DEC );
Serial.println("" );interrTime = 0; //clear the interrupt time
delay(3000);
}void isr0 () {
interrTime = micros(); //record time isr called
}