retrolefty:
Again don't use LOW mode, it's a snake pit, use FALLING.
This is just a test, what fun is it if I can't do bad things? ![]()
I've found the problem. When in LOW mode, the ISR was continuously being fired, hogging the CPU, so the main loop never hardly had a chance to run. When the switch was turned on, then the ISR stopped being fired, and the main loop then executed the handler for the last ISR request. Thus the beep was only heard in the change from low to high.
Interestingly, in my initial sketch i did not flip a variable in the ISR, but directly called tone. This produced the same behaviour probably due to the internals of the tone function. The effect was the same as if it were called in a for loop, no sound was produced.
So the LOW mode works as expected after all. The proof is in this new sketch:
static const int INTERRUPT_NUMBER = 0;
static const int INTERRUPT_MODE = LOW;
static int isrPasses = 0;
void setup() {
Serial.begin(9600);
attachInterrupt(INTERRUPT_NUMBER, isr, INTERRUPT_MODE);
}
void loop() {
if(isrPasses){
Serial.print("ISR was executed ");
Serial.print(isrPasses);
Serial.println(" times");
isrPasses = 0;
beep();
}
}
void beep(){
//noTone(7);
tone(7, 440, 1000);
}
//ISR. Must take no parameters and return nothing.
void isr(){
isrPasses++;
}
For the CHANGE, RISING and FALLING modes, the log output shows the ISR is only called once:
ISR was executed 1 times
However for the LOW mode, the log shows the ISR has been executed multiple times:
ISR was executed 1815 times
ISR was executed 5366 times
ISR was executed 7695 times
Mystery solved then. Thanks everybody.