Lesson 16: Traffic lights
עמוד זה דורש דפדפן עדכני (תומך ב-SharedArrayBuffer). אנא עדכן את Chrome, Edge, Firefox או Safari לגרסה העדכנית ביותר.
English
A light on every corner
- Traffic lights are small computers: they switch lights on and off in a fixed order, all day.
- In lesson 2 the LEDs were already on the board. Now you build the whole circuit yourself.
- You build one with three LEDs: red, amber and green, each with its own resistor.
- Then you turn it into a crossing: people on foot press a button, and the cars get a red light.
Words for this lesson
| Word | Meaning |
|---|---|
| traffic light | red, amber and green lights that control cars |
| amber | the orange-yellow light between green and red |
| cycle | steps that repeat in the same order |
| crossing | a place where people walk over the road |
| const int | a name for a number that never changes, like RED = 13 |
English
One cycle
| Step | Red | Amber | Green | How long |
|---|---|---|---|---|
| go | off | off | on | 2 s |
| get ready to stop | off | on | off | 1 s |
| stop | on | off | off | 2 s |
- In each step, switch off the light that was on, then switch on the next one.
- Three LEDs share one GND wire: their short legs are all joined to the same line down to GND.
English
Names for pins
const int GREEN = 11;
void setup() {
pinMode(GREEN, OUTPUT);
}
void loop() {
digitalWrite(GREEN, HIGH);
delay(500);
digitalWrite(GREEN, LOW);
delay(500);
}
const int GREEN = 11;gives pin 11 a name. If you move the LED to another pin later, you change one line, not ten.
רמת קושי 3/5 · 200 מטבעות + בונוס קטן בהשלמה הראשונה היום
Wire three LEDs, each with a 220 Ω resistor: red on pin 13 (top row), amber on pin 12, green on pin 11. Join their short legs to GND. Then finish the cycle: green for 2 s, amber for 1 s, red for 2 s, and again.
לחץ על הרץ כדי לראות את התוצא כאן.
רמת קושי 4/5 · 280 מטבעות + בונוס קטן בהשלמה הראשונה היום
Make it a crossing for people on foot. Add a button from pin 2 to GND. The light stays green until someone presses. Then: wait 1 s, amber for 1 s, red for 3 s and print WALK, then green again and print WAIT.
לחץ על הרץ כדי לראות את התוצא כאן.