0Pricing
Arduino & IoT Academy · Lekcja

Pisanie bezpiecznej procedury ISR

Skracać procedury obsługi przerwań i używać volatile.

Pisanie bezpiecznej procedury ISR to bezpłatna lekcja Arduino & IoT Academy na CoddyKit. To lekcja 3 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Arduino & IoT Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Arduino & IoT Academy zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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

Często zadawane pytania

Czy lekcja „Pisanie bezpiecznej procedury ISR” jest bezpłatna?

Tak — pełny tekst „Pisanie bezpiecznej procedury ISR” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Arduino & IoT Academy, przejdź na CoddyKit PRO. Kurs Arduino & IoT Academy zawiera 4 lekcji w sumie.

Co nauczysz się w „Pisanie bezpiecznej procedury ISR”?

Skracać procedury obsługi przerwań i używać volatile. Ćwiczysz Arduino & IoT Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Arduino & IoT Academy?

Nie wymagamy żadnego doświadczenia. Arduino & IoT Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 3 z 4.

Ile czasu zajmuje lekcja „Pisanie bezpiecznej procedury ISR”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Arduino & IoT Academy?

Tak. Każda lekcja Arduino & IoT Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Odpytywanie a przerwania
  2. attachInterrupt na pinie
  3. Pisanie bezpiecznej procedury ISR
  4. Zliczanie impulsów z enkodera
← Powrót do Arduino & IoT Academy