Language | Libraries | Comparison

attachInterrupt(interrupt, function, mode)

Description

Specifies the function to call when an external interrupt occurs. There are two external interrupts (0 and 1), on digital pins 2 and 3 respectively.

Note: millis() and delay() won't work in your interrupt-handling function. Any serial data received while in your interrupt-handling function will be lost.

Note: you may need to declare as volatile any variables that you modify within your interrupt handling function (see the example).

Parameters

interrupt: the number of the interrupt (int)

function: the function to call when the interrupt occurs; this function must take no parameters and return nothing.

mode: when the interrupt should be triggered: LOW to trigger the interrupt whenever the pin is low, CHANGE to trigger the interrupt whenever the pin changes value, RISING to trigger when the pin goes from low to high, and FALLING for when the pin goes from high to low.

Returns

none

Example

int pin = 13;
volatile int state = LOW;

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

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

void blink()
{
  state = !state;
}

See also

Reference Home

Corrections, suggestions, and new documentation should be posted to the Forum.