Basic Library
Digital I/O
Analog I/O
Advanced I/O
Time
Math
Trigonometry
Random Numbers
Bits and Bytes
Interrupts
Serial Comm.
Standard Library
Servo Motor
Character LCD
SPI
I2C (Wire)
SD Card
SD (File Operations)
Periodic Operation
Clock (RTC)
Interrupt
This library allows users to trigger a process interrupt function or enable/disable an interrupt function when the input to a pin changes value.
attachInterrupt
- Description
- Specifies a function to execute (call) when an external interrupt occurs (signal on external pin).
- Syntax
- attachInterrupt(pin, void(*)(void) func, mode)
- Parameters
- pin: The number of pin used for the interrupt(part of IntX in pin map)
func: The function to call when the interrupt occurs
mode:
LOW (to trigger the interrupt whenever the pin is low)
CHANGE (to trigger the interrupt whenever the pin changes value)
FALLING (to trigger when the pin goes from high to low)
RISING (to trigger when the pin goes from low to high) - Returns
- None
detachInterrupt
- Description
- Turns off the interrupt specified in attachInterrupt.
- Syntax
- detatachInterrupt(pin)
- Parameters
- pin: The number of pin used for the interrupt (part of IntX in pin map)
- Returns
- None
interrupts
- Description
- Re-enables the interrupt disabled in noInterrupts.
- Syntax
- interrupts()
- Parameters
- None
- Returns
- None
noInterrupts
- Description
- Disables an interrupt process. Use this when you need to protect the timing of a specific process; this can even be used to disable an important task run in the background.
- Syntax
- noInterrupts()
- Parameters
- None
- Returns
- None
Sample Program
This is a sample that generates an interrupt when the state if pin 0 changes, which in turn changes the state of the LED.
#include <Arduino.h>
int ledpin = PIN_LED0;
int intpin = 1;
bool state = LOW;
void blink()
{
state = !state;
}
void setup()
{
pinMode(ledpin, OUTPUT);
pinMode(intpin, INPUT_PULLUP);
attachInterrupt(0, blink, CHANGE);
}
void loop()
{
digitalWrite(ledpin, state);
}