attachInterrupt() — Maple v0.0.9 Documentation

### Navigation

- next
- previous |
- LeafLabs |
- Docs Home |
- Index »
- Complete Language Index »

# attachInterrupt()

Used to specify a function to call when an external interrupt (like an GPIO changing from LOW to HIGH, a button getting pressed, etc.) occurs.

Contents

- Library Documentation
- Discussion
- Using Interrupts
- Example
- Arduino Compatibility
- See also

## Library Documentation

void attachInterrupt(int channel, voidFuncPtr handler)

  Attach an interrupt handler to the given channel.

  This interrupt handler will be called when the timer’s counter reaches the given channel compare value.

  The argument should be a function which takes no arguments and has no return value; i.e. it should have signature

  void (*handler)(void);

  Note: The function (often called an interrupt service routine, or ISR) should attempt to return as quickly as possible. Blinking the LED, some logic, PWM updates, and Serial writes are fine; writing to SerialUSB or waiting for user input can take a long time and other compare interrupts won’t fire. Tip: if you have a delay() in your interrupt routine, you’re probably doing it wrong.

  Parameters

  - `channel` -

    the channel to attach the ISR to, from 1 to 4.

  - `handler` -

    The ISR to attach to the given channel.

  See
    voidFuncPtr
ExtIntTriggerMode enum

  The kind of transition on an external pin which should trigger an interrupt.

  Values:

  - `RISING` -

    To trigger an interrupt when the pin transitions LOW to HIGH.

  - `FALLING` -

    To trigger an interrupt when the pin transitions HIGH to LOW.

  - `CHANGE` -

    To trigger an interrupt when the pin transitions from LOW to HIGH or HIGH to LOW (i.e., when the pin changes).

typedef void(* voidFuncPtr)(void)

## Discussion

Because the function will run in interrupt context, inside of it, delay() won’t work, and the value returned by millis() will not increment. Serial data received while in the function may be lost. You should declare as `volatile` any global variables that you modify within the attached function.

There are a few constraints you should be aware of if you’re using more than one interrupt at a time; the External Interrupts page has the details.

## Using Interrupts

Interrupts are useful for making things happen automatically in microcontroller programs, and can help solve timing problems. A good task for using an interrupt might be reading a rotary encoder, or monitoring user input.

If you wanted to insure that a program always caught the pulses from a rotary encoder, never missing a pulse, it would make it very tricky to write a program to do anything else, because the program would need to constantly poll the sensor lines for the encoder, in order to catch pulses when they occurred. Other sensors have a similar interface dynamic too, such as trying to read a sound sensor that is trying to catch a click, or an infrared slot sensor (photo-interrupter) trying to catch a coin drop. In all of these situations, using an interrupt can free the microcontroller to get some other work done while not missing the doorbell.

## Example

    int maple_led_pin = 13;
    volatile int state = LOW; // must declare volatile, since it's
                              // modified within the blink handler

    void setup() {
      pinMode(maple_led_pin, OUTPUT);
      attachInterrupt(0, blink, CHANGE);
    }

    void loop() {
      digitalWrite(maple_led_pin, state);
    }

    void blink() {
      state = !state;
    }

## Arduino Compatibility

Most Arduino boards have two external interrupts: numbers 0 (on digital pin 2) and 1 (on digital pin 3). The Arduino Mega has an additional four: numbers 2 (pin 21), 3 (pin 20), 4 (pin 19), and 5 (pin 18). On the Maple, you don’t have to remember which interrupt number goes with which pin – just tell `attachInterrupt()` the pin you want.

## See also

- detachInterrupt
- External Interrupts

License and Attribution

This documentation page was adapted from the Arduino Reference Documentation, which is released under a Creative Commons Attribution-ShareAlike 3.0 License.

### Navigation

- next
- previous |
- LeafLabs |
- Docs Home |
- Index »
- Complete Language Index »