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.
Parameter | Notes |
---|---|
interrupt | Id of the interrupt. Not to be mistaken for pin number. |
ISR | Interrupt Service Routine. This is the method which will be executed when the interrupt occurs. |
mode | What should cause the interrupt to trigger. One of LOW, CHANGE, RISING, or FALLING. Due boards also allow HIGH. |
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.