Lesson 14: The car park gate
このページには最新のブラウザ(SharedArrayBuffer対応)が必要です。Chrome、Edge、Firefox、Safariを最新バージョンに更新してください。
English
A gate that counts
- At a car park the driver presses a button, the arm goes up, and the car drives in.
- The arm is a servo motor.
gate.write(90)turns it to 90°,gate.write(0)turns it back. - The Arduino also counts the cars. When the car park is full, the gate stays closed and a red light says FULL.
Words for this lesson
| Word | Meaning |
|---|---|
| servo | a motor that turns to an angle you choose and stays there |
| gate arm | the long bar that stops cars |
| count | add 1 each time something happens |
| global variable | a variable made outside setup() and loop(); it keeps its value |
| full | there is no free place left |
English
Move the arm
#include <Servo.h>
Servo gate;
void setup() {
gate.attach(9);
}
void loop() {
gate.write(0); // closed
delay(1000);
gate.write(90); // open
delay(1000);
}
#include <Servo.h>brings in the servo code.Servo gate;makes a servo calledgate, andattach(9)joins it to pin 9.
English
Counting with a variable
int cars = 0;abovesetup()makes a global variable. It lives as long as the board runs.cars = cars + 1;takes the old number, adds 1, and keeps the new one.if (cars < 3)asks: is there still a free place? With 3 places the car park is full whencarsis 3.
| Press | cars before |
cars < 3? |
What happens |
|---|---|---|---|
| 1st | 0 | yes | gate opens, Cars: 1 |
| 2nd | 1 | yes | gate opens, Cars: 2 |
| 3rd | 2 | yes | gate opens, Cars: 3, FULL light on |
| 4th | 3 | no | FULL, gate stays closed |
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
A servo on pin 9 is the gate arm of a car park: 0° is closed, 90° is open. A driver presses the button on pin 2. Open the gate, keep it open for 2 seconds, then close it again.
実行ボタンをクリックして出力を確認してください。
難易度 4/5 · 280 コイン + 今日初回達成時の小ボーナス
The car park has only 3 places. Count the cars with a variable. For each press: if there is still a place, add 1, print it like Cars: 2, and open the gate for 1 second. When the third car is in, switch on the red FULL light on pin 12. If the car park is full, print FULL and keep the gate closed.
実行ボタンをクリックして出力を確認してください。