Odpytywanie a przerwania
Zrozumieć, dlaczego niektóre zdarzenia nie mogą czekać na wykonanie pętli.
Odpytywanie a przerwania to bezpłatna lekcja Arduino & IoT Academy na CoddyKit. To lekcja 1 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.
Always Checking
So far your loop uses polling: it runs around and around, asking each sensor and button over and over whether anything changed.
Polling in Action
A typical poll reads a pin every pass of the loop. If the value matches what you want, you react; otherwise you simply keep looping. 🔁
if (digitalRead(buttonPin) == LOW) {
doSomething();
}The Hidden Cost
Polling works, but it wastes effort asking when nothing has changed. Your board burns cycles checking a pin that stays the same for a long time.
When the Loop Is Busy
If a slow task or a long delay holds the loop, a quick press can come and go before you ever read the pin again.
Events That Cannot Wait
Some signals are fast and brief: a sensor spike, an encoder edge, a short pulse. Miss the moment and the event is gone for good.
A Smarter Way
An interrupt flips the model: instead of you asking, the hardware taps your shoulder the instant something happens on a pin.
Drop Everything
When an interrupt fires, the chip pauses whatever the loop was doing, runs your handler, then returns exactly where it left off. ⚡
Never Miss the Moment
Because the hardware watches the pin for you, even a tiny pulse triggers your code. You catch events a slow loop would skip.
Free Up the Loop
With interrupts handling urgent signals, your loop stays clean and responsive, free to do its slower work without constant checking.
When Polling Is Fine
Not everything needs an interrupt. For a button a human presses slowly, polling is simpler and perfectly good enough.
Choosing the Right Tool
Reach for an interrupt when an event is fast, brief, or might arrive while the loop is busy. Otherwise polling keeps things easy.
Quick Check
Which situation is the best reason to choose an interrupt over polling?
Recap
You saw that polling keeps asking while an interrupt gets notified. Use interrupts for fast events and polling for relaxed ones. 🎉
Często zadawane pytania
Czy lekcja „Odpytywanie a przerwania” jest bezpłatna?
Tak — pełny tekst „Odpytywanie a przerwania” 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 „Odpytywanie a przerwania”?
Zrozumieć, dlaczego niektóre zdarzenia nie mogą czekać na wykonanie pętli. Ć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 1 z 4.
Ile czasu zajmuje lekcja „Odpytywanie a przerwania”?
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
- Odpytywanie a przerwania
- attachInterrupt na pinie
- Pisanie bezpiecznej procedury ISR
- Zliczanie impulsów z enkodera