Escribir una ISR segura
Mantenga breves los controladores de interrupciones y use volatile.
Escribir una ISR segura es una lección gratuita de Arduino & IoT Academy en CoddyKit. Esta es la lección 3 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Arduino & IoT Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Arduino & IoT Academy incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
A Special Function
An ISR, or interrupt service routine, is the function that runs when an interrupt fires. It has rules that ordinary functions do not.
Keep It Short
The golden rule: make your ISR as short as possible. The main program is paused while it runs, so finish fast and get out.
Do the Heavy Work Later
Inside the ISR, just set a flag or bump a counter. Let the loop do the slow work like printing or math once it sees the flag.
void myISR() {
pressed = true;
}Shared Variables
An ISR and your loop both touch the same variable. The compiler may not expect it to change behind the loop's back, which causes subtle bugs. 🐞
The volatile Keyword
Mark any variable shared with an ISR as volatile. It tells the compiler the value can change at any moment, so always reread it.
volatile bool pressed = false;No delay Inside
Never call delay in an ISR. The timing that delay needs is itself paused during an interrupt, so it simply will not work.
Serial Is Risky Too
Avoid Serial.print inside an ISR. It relies on interrupts that are blocked while your handler runs, so output can stall or garble.
millis Freezes
Inside an ISR, millis stops advancing because its background counting is paused. Read elapsed time in the loop, not the handler.
Handle the Flag in Loop
Back in the loop, check the flag, act on it, then clear it. This is where the real response safely takes place.
if (pressed) {
pressed = false;
handlePress();
}Reading Multi-Byte Values
When the loop reads a multi-byte volatile value an ISR updates, briefly disable interrupts so you get a clean, unsplit number.
noInterrupts();
long c = count;
interrupts();Why These Rules Matter
Follow these rules and your ISR stays fast and predictable. Break them and you get freezes, lost data, or values that never seem to update.
Quick Check
Why must a variable shared between an ISR and the loop be marked volatile?
Recap
You learned to keep an ISR short, mark shared data volatile, skip delay and Serial, and do the real work back in the loop. 🎉
Preguntas frecuentes
¿La lección «Escribir una ISR segura» es gratis?
Sí — el texto completo de «Escribir una ISR segura» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Arduino & IoT Academy, actualiza a CoddyKit PRO. El curso de Arduino & IoT Academy incluye 4 lecciones en total.
¿Qué aprenderé en «Escribir una ISR segura»?
Mantenga breves los controladores de interrupciones y use volatile. Practicas Arduino & IoT Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar Arduino & IoT Academy?
No se requiere experiencia previa. Arduino & IoT Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 3 de 4.
¿Cuánto tiempo toma la lección «Escribir una ISR segura»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de Arduino & IoT Academy?
Sí. Cada lección de Arduino & IoT Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- Sondeo frente a interrupciones
- attachInterrupt en un pin
- Escribir una ISR segura
- Contar pulsos de un encoder