Arduino and ISR
The idea
I think a way to exploit a processor and specially this one (Arduino, ESP8266, and so on) is optimizing the throughput of the processor. In order to do it, I’m starting to explore the ISR capabilities or interrupt service routine. When I studied in the University, the concept and was like mind-blowing for me. Because having more than a single of piece of code running was a piece of news. Amazing!
So I knew ESP8266 has it. But then I discovered (thanks to a friend) that Arduino (the ATMega chips) has the capability as well.
So the idea is not so complex using the Arduino wrapper (instead of using the low level AVR code). There are 2 main operations:
I think it’s cristal clear what it means each of it. A heads up about this concept in Arduino. Depending on Arduino model, there are more or less interruptions available. Here is a table which summarize:
- Uno, Nano, Mini, other 328-based:2, 3
- Mega, Mega2560, MegaADK:2, 3, 18, 19, 20, 21
- Micro, Leonardo, other 32u4-based: 0, 1, 2, 3, 7
- Zero:all digital pins, except 4
- MKR1000 Rev.:10, 1, 4, 5, 6, 7, 8, 9, A1, A2
- Due: all digital pins
Example
So for this case an Arduino UNO is based. But you can do it whichever you have.

The idea is fairly simple. A push button connected to Digital Pin 2 as input. When the push button is pushed the interruption will on or off the internal led 13. And all the code not running in the loop method! So you have it released for another task.
Finally here is the code, the idea is simple: Attaching a method to be triggered when Digital Input 2 changes in state, and toggling the internal led.
const byte ledPin = 13;
const byte interruptPin = 2;void setup() {
pinMode(ledPin, OUTPUT);
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);
Serial.begin(9600);
}void loop() {
Serial.println(“Another task”);
}void blink() {
digitalWrite(13, !digitalRead(13)); // Toggle LED on pin 13
}
So another idea now is to making work better the fancy alarm I did in previous post using this last new concepts: Independence in esp8266–01 and Arduino’s interruptions. :)