attachInterrupt()

Page

Stránky / Arduino / C++ pre Arduino / Funkcie API /

The function connects the user's interrupt handler to the external interrupt pin. This allows you to avoid periodic checking of a pin and respond only to a event on a pin. Typical tasks when need do it is responding to a rotary encoder or a button.

Syntax

void attachInterrupts(uint8_t interruptNum, void (userFunc*)(void), int mode);

Parameters

  • interruptNum - Interrupt number. This number does not match the pin number, and it's best to use the macro digitalPinToInterrupt to get it. You can only use pins, that allow external interrupts.
  • userFunc - The user function that handles the interrupt.
  • mode - Mode defines when the interrupt should be triggered.
    • LOW - The interrupt occurs if the logical zero is on the pin.
    • CHANGE - The interrupt occurs when the logical level is changed.
    • RISING - The interrupt occurs when changing from LOW to HIGH.
    • FALLING - The interrupt occurs when changing from HIGH to LOW.
    • HIGH - The interrupt occurs if the logical zero is on the pin. Only for Arduino Due, Zero and MKR1000.

Return value

  • The function does not return any value.

Usable pins

Board Useable digital pins
Uno, Nano, Mini 2, 3
Mega 2, 3, 18, 19, 20, 21
Micro, Leonardo 0, 1, 2, 3, 7
Zero All digital pins except 4.
MKR1000 0, 1, 4, 5, 6, 7, 8, 9, A1, A2
Due All digital pins.

The pin numbers that support interrupts are different than digital pins. To avoid having to remember them, you can use the macro digitalPinToInterrupt(pin). In many examples you find on the Internet, this macro is not used. I'm not an exception, and many of my older codes do not use this macro.

Examples

Here are some examples of use.


14.11.2017


Menu