Escreva uma ISR segura
Mantenha os manipuladores de interrupção curtos e use volatile.
Escreva uma ISR segura é uma aula grátis de Arduino & IoT Academy no CoddyKit. Esta é a aula 3 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Arduino & IoT Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Arduino & IoT Academy inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em 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. 🎉
Perguntas Frequentes
A aula “Escreva uma ISR segura” é grátis?
Sim — o texto completo de “Escreva uma ISR segura” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Arduino & IoT Academy, atualize para CoddyKit PRO. O curso de Arduino & IoT Academy inclui 4 aulas no total.
O que vou aprender em “Escreva uma ISR segura”?
Mantenha os manipuladores de interrupção curtos e use volatile. Você pratica Arduino & IoT Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar Arduino & IoT Academy?
Nenhuma experiência prévia é necessária. Arduino & IoT Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 3 de 4.
Quanto tempo leva a aula “Escreva uma ISR segura”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de Arduino & IoT Academy?
Sim. Cada aula de Arduino & IoT Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- Sondagem versus interrupções
- attachInterrupt em um pino
- Escreva uma ISR segura
- Conte pulsos de um codificador