Lesson 14: The car park gate
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 |
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.
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 |
In the exam
Car parks come up often in IGCSE Computer Science (0478) questions on sensors and microprocessors.
- 0478/13/M/J/21 Q9: a car park counts the cars that come in, so it can show how many places are still free, and each place has a red and a green light. (a) Name a sensor for each job. The mark scheme accepts: too high, an infra-red (light) sensor; counting the cars going in, a magnetic field or pressure sensor; a car in a parking place, a pressure, magnetic field or infra-red sensor. (b) The sensor sends data to the microprocessor, which compares it with a stored value; if a car is there it switches the red light on and the green light off, otherwise the reverse. The checks repeat.
- 0478/11/O/N/23 Q2(e): at the exit barrier the system reads the ticket number. The microprocessor compares the ticket number with the stored data (a file of ticket numbers) and checks that it is listed as paid. If yes, it sends a signal to the actuator to raise the barrier; if no, the barrier stays down. Your
if (cars < 3)is the same kind of decision.
Dificultad 2/5 · 140 monedas + un pequeño bono en el primer intento de hoy
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.
Haz clic en Ejecutar para ver la salida aquí.
Dificultad 4/5 · 280 monedas + un pequeño bono en el primer intento de hoy
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.
Haz clic en Ejecutar para ver la salida aquí.