Lesson 44: Interrupts
このページには最新のブラウザ(SharedArrayBuffer対応)が必要です。Chrome、Edge、Firefox、Safariを最新バージョンに更新してください。
English
Stop what you are doing!
- A program that waits in
delaydoes not look at its buttons. A short press is missed. - An interrupt is a signal that says "this needs attention now". The processor stops its program, runs a short function (the interrupt service routine, ISR), then goes back to where it was.
- On an Arduino Uno, pins 2 and 3 can make interrupts.
Words for this lesson
| Word | Meaning |
|---|---|
| interrupt | a signal that needs the processor's attention |
| ISR | the function that handles the interrupt |
| FALLING | the moment a pin goes from HIGH to LOW |
| volatile | a variable an interrupt can change |
English
Polling or an interrupt?
| Polling | Interrupt |
|---|---|
loop asks "pressed?" again and again |
the button tells the processor |
misses a press during delay |
never misses a press |
| the answer can be late | the answer comes at once |
English
In the exam
- 0478/12/F/M/24 Q3(b): (i) the purpose of an interrupt: to show that something needs the attention of the processor. (ii) An input device and its interrupt: a keyboard, a key is pressed (your button is the same idea).
- (iii) How the processor handles the program and the interrupt. Mark scheme points: the interrupt is given a priority and put in a queue; the processor finishes the current fetch-execute cycle; if the interrupt has a higher priority, it saves the current program's registers on the stack, finds the source and runs the right ISR; then it restores the saved values and carries on with the program. Your
pressed()is the ISR, and the blink carries on after it.
難易度 4/5 · 280 コイン + 今日初回達成時の小ボーナス
The loop is busy: it blinks the LED on pin 13 slowly, and while it waits in delay it does not look at the button. Make each press of the button on pin 2 switch the LED on pin 12 on or off at once, with an interrupt: write the function pressed() and connect it to pin 2 with attachInterrupt in setup (see the hint). Keep the blink.
実行ボタンをクリックして出力を確認してください。
難易度 4/5 · 280 コイン + 今日初回達成時の小ボーナス
Count the presses with an interrupt. count must be volatile because the interrupt changes it. The loop waits 2 seconds, then prints Presses: and the count. Check presses the button 3 times in the first 2 seconds and 2 more times after that.
実行ボタンをクリックして出力を確認してください。