0Pricing
Arduino & IoT Academy · Lektion

Eine sichere ISR schreiben

Halten Sie Interrupt-Handler kurz und verwenden Sie volatile.

Eine sichere ISR schreiben ist eine kostenlose Arduino & IoT Academy-Lektion auf CoddyKit. Dies ist Lektion 3 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Arduino & IoT Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Arduino & IoT Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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. 🎉

Häufig gestellte Fragen

Ist die Lektion „Eine sichere ISR schreiben“ kostenlos?

Ja — der vollständige Text von „Eine sichere ISR schreiben“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Arduino & IoT Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Arduino & IoT Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Eine sichere ISR schreiben“?

Halten Sie Interrupt-Handler kurz und verwenden Sie volatile. Du übst Arduino & IoT Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Arduino & IoT Academy zu starten?

Keine Vorkenntnisse erforderlich. Arduino & IoT Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 3 von 4.

Wie lange dauert die Lektion „Eine sichere ISR schreiben“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Arduino & IoT Academy-Lektion Code schreiben und ausführen?

Ja. Jede Arduino & IoT Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Polling vs. Interrupts
  2. attachInterrupt an einem Pin
  3. Eine sichere ISR schreiben
  4. Impulse eines Encoders zählen
← Zurück zu Arduino & IoT Academy