Multiple attachInterrupt acting weird

Hi there,

I want to trigger and send a message based on two interrupts, but only send once a minute. So I use two interrupts and the watchdog for this as below. So in theory, every watchdog cycle, SendData should be outputted only once. It gets outputted three times however.

The problem is that after the multiple interrupts are activated. So if the first pin interrupt is attached, and then deactivated, the second attachinterrupt on pin 2 activates the first interrupt. To me it seems like a bug, but it could be that I’m missing some point here. What do you think?

#include "Sodaq_wdt.h"

#define Led_Pin LED_BUILTIN

int interruptPin1 = 0;
int interruptPin2 = 1;
void setup()
{

  pinMode(interruptPin1, INPUT);
  pinMode(interruptPin2, INPUT);

  sodaq_wdt_enable(WDT_PERIOD_8X);

}

void loop() {
   if (sodaq_wdt_flag) {
    sodaq_wdt_flag = false;
    sodaq_wdt_reset();
    SerialUSB.println("interrupt");
    SerialUSB.print("attachInterrupt1 ");
    attachInterrupt(interruptPin1, SendData, HIGH);
    SerialUSB.print("attachInterrupt2 ");
    attachInterrupt(interruptPin2, SendData, HIGH);
   }
}

void SendData() {
    detachInterrupt(interruptPin1);
    detachInterrupt(interruptPin2);
    SerialUSB.println("SendData");
}

You should really keep the ISR code as minimal as possible. I would recommend just setting another flag, and then in loop() if that flag is set, execute the code you have currently in SendData().

It seems that you are configuring the watch-dog timer to tick once every 8 seconds.

Both pins are attached to the execute the same ISR.

The Arduino core uses the same level of priority for each of the pin interrupts. That means that if the code is executing an ISR when a pin is triggered, that new pin’s ISR will be queued until the first instance of the ISR is finished. This means that if the other pin is triggered again while the ISR is executing, the ISR will be queued again. The detachInterrupt() will not clear the pending interrupt.