Two external interrupts and Serial.print

I have read some other topics about the external interrupts can interfere with the Serial.print function as the serial is using interrupts on it's own. However I could not reproduce any kind of malfunction/interruptions or issues even without detaching the interrupts before using the serial.println (doesn't mean there won't be any).

Again, this topic isn't about having serial.println inside an interrupt but in other places of the program and can the interrupts interfere with it or not?

Here is an example code:

const byte interruptPin = 20;
const byte interruptPin2 = 21;

int x;

void setup() {
  Serial.begin(9600);

  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), count, FALLING);

  pinMode(interruptPin2, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin2), count2, FALLING);
}

void some_normal_function () {
 // bit longer
}

void loop() {

  //detachInterrupt(interruptPin);
  //detachInterrupt(interruptPin2);  

  some_normal_function();
  Serial.print("Current loop: ");
  Serial.println(x);

  //attachInterrupt(digitalPinToInterrupt(interruptPin), count, FALLING);
  //attachInterrupt(digitalPinToInterrupt(interruptPin2), count2, FALLING);

  x++;
  delay(1000);
}

void count() {

// do some stuff

}

void count2() {

// do some stuff

}

If I understand it correctly then any other parts of the code including the delay functions can be interrupted except the ones in case I comment out the detach/attach interrupt.

Another question can the 2 external interrupt interfere with each other? For example the second sensor just gets data while the first ISR is being executed?

You can safely use Serial.print outside interrupts without trouble.
The question about priority between the two interrupts has to be answered by another helper.

Serial.print is compatible with external interrupts as long as you aren't using serial.print inside an interrupt handler.

"// do some stuff"
Again, in general, you want to do as little as possible inside an interrupt handler. Set a global flag that will trigger further processing in the loop() function.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.