#define AN_ISR anISR2 // set to anISR1 or anISR2
// wire pin A to pin B
#define PinA 3 // interrupt input
#define PinB 4
volatile uint32_t changeCnt = 1;
void setup() {
Serial.begin( 1);
pinMode( PinA, INPUT );
pinMode( PinB, OUTPUT );
pinMode( LED_BUILTIN, OUTPUT );
digitalWriteFast( PinB, LOW );
Serial.println( "Hello World" );
attachInterrupt( PinA, AN_ISR, CHANGE );
}
void anISR1() {
digitalToggleFast( PinB );
digitalToggleFast( LED_BUILTIN );
changeCnt++;
if ( changeCnt> 100) {
changeCnt = 0;
detachInterrupt( PinA ); // ABORT - THIS ISR WILL NEVER ALLOW loop() to run
}
delayMicroseconds( 50000 ); // 50 ms
}
void anISR2() {
detachInterrupt( PinA );
digitalToggleFast( PinB );
digitalToggleFast( LED_BUILTIN );
changeCnt++;
delayMicroseconds( 50000 ); // 50 ms
attachInterrupt( PinA, AN_ISR, CHANGE );
}
void loop() {
static uint32_t lastChange = 1;
if ( 0 == changeCnt ) {
Serial.println( "ISR deadlock aborted!" );
delay(200);
digitalToggleFast( LED_BUILTIN );
}
else if ( lastChange != changeCnt ) {
Serial.println( changeCnt );
lastChange = changeCnt;
delay(900);
}
digitalToggleFast( PinB );
delay(100);
}