arduinoInterrupts


Syntax

  • digitalPinToInterrupt(pin); // converts a pin id to an interrupt id, for use with attachInterrupt() and detachInterrupt().

  • attachInterrupt(digitalPinToInterrupt(pin), ISR, mode); // recommended

  • attachInterrupt(interrupt, ISR, mode); // not recommended

  • detachInterrupt(digitalPinToInterrupt(pin));

  • detachInterrupt(interrupt);

  • noInterrupts(); // disables interrupts

  • interrupts(); // re-enable interrupts after noInterrupts() has been called.

Parameters

ParameterNotes
interruptId of the interrupt. Not to be mistaken for pin number.
ISRInterrupt Service Routine. This is the method which will be executed when the interrupt occurs.
modeWhat should cause the interrupt to trigger. One of LOW, CHANGE, RISING, or FALLING. Due boards also allow HIGH.

Remarks

Interrupt Service Routines (ISRs) should be as short as possible, since they pause main program execution and can thus screw up time-dependent code. Generally this means in the ISR you set a flag and exit, and in the main program loop you check the flag and do whatever that flag is supposed to do.

You cannot use delay() or millis() in an ISR because those methods themselves rely on interrupts.