Interrupt Service Routine (DE version)
An Interrupt Service Routine (ISR) is an interruption of the main routine of program, to execute a time-critical task. In the case of the Arduino, this means that the loop() can be interrupted for a something more important. This interruption is usually triggered by a digital input. The use of an ISR is helpful when measuring speed or for reading out rotary encoders - i.e. whenever inputs have to be processed very quickly.
We use the function attachInterrupt(INTERRUPT, ISR, MODE) in setup() to "listen" to a PIN for an event. The Arduino UNO has two interrupt pins (D2 = INTERRUPT 0, D3 = INTERRUPT 1). The ISR parameter designates the function that is to be called when the interrupt is triggered. As a rule, we only do simple routines in this function. For example, it is not recommended to use the serial interface because sending data takes too long. We also use so-called volatile variables within the ISR. These variables are marked as volatile because they can change at any time. Finally, the mode specifies when the ISR is to be executed. There are three modes: RISING, FALLING or CHANGE. RISING means that the interrupt is set from LOW to HIGH. FALLING means that the interrupt is set from HIGH to LOW and CHANGE is executed on both events.
Below is an example reading a rotary encoder with the Arduino. We use the EC12 from ALPS (data sheet).
Further Information:
attachInterrupt() - Arduino.cc
Interrupts - from Nick Gammon