AttachInterrupt()

From Microduino Wiki
Jump to: navigation, search
void attachInterrupt (uint8_t interruptNum, void(*)(void)userFunc, int mode)
  • Set the interrupt.

Specify the interrupt function. There are two kinds of external as 0 and 1, which are generally corresponding to the digital pins 2 and 3.


  • Pin description(interruptNum):

Uno/Nano/Mega上

Interrupt pin 0 is D2. Interrupt pin 1 is D3.


  • Parameters:

interrupt Interrupt type, 0 or 1.

fun The corresponding function

mode Trigger mode. There several types as following:

--LOW Trigger interrupt in low level.

--CHANGE Trigger interrupt when it changes.

--RISING Trigger interrupt when the low level changes into high level.

--FALLING Trigger interrupt when the high level changes into low level.


  • Note:

In the interrupt function, function delay cannot be used,because millis will always return the value before entering the interrupt. If read the serial data, it will be lost. The variables used in the interrupt function need to be defined as volatile.
In the following example, if trigger interrupt function through external pins, and then control the twinkle of .

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;
}

[Return to Arduino Syntax Manual]