attachInterrupt()

Interrupts are useful for making things happen automatically in microcontroller programs and can help solve timing problems. Good tasks for using an interrupt may include reading a rotary encoder, or monitoring user input. If you wanted to ensure that a program always caught the pulses from a rotary encoder, so that it never misses 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 input. You are able to attach up to 16 interrupts simultaneously. Each pin has to have different pin index in its port. You can't use the same pin index for different ports. For example if you attach interrupt to PC7 then you are unable to attach interrupt to PA7 or to PB7. Please check the pin table below.
Z-Uno pin # Cortex port & pin name
0 PC8
1 PC9
2 PF2
24(TX0) PD13
25(RX0) PC11
3 PF6
4 PF7
5 PD9
6 PD10
7 PF4
8 PF5
9 PD11
10 PD12
11 PD14
12 PD15
13 PA0
14 PA1
15 PA2
16 PA3
17 PA4
18 PB13
19 PB12
20 PB11
21 PC6
22 PC7
23 PC10
attachInterrupt(pin, userFunc, mode)
pin: the number of the pin whose mode you wish to set. userFunc: the ISR to call when the interrupt occurs; this function must take no parameters and return nothing. This function is sometimes referred to as an interrupt service routine. mode: defines when the interrupt should be triggered. Four constants are predefined as valid values:
  • 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
  • FALLING for when the pin goes from high to low
  • ZunoErrorExtInt Can't attach handler the interrupt index is busy
  • ZunoErrorOk Attached successfuly
		
const byte ledPin = 13;
const byte interruptPin = 2;
volatile byte state = LOW;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(interruptPin, blink, CHANGE);
}

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

void blink() {
  state = !state;
}
		
	
detachInterrupt() function