Traffic Light With Night Mode
On an Arduino Nano 33 BLE, run a junction traffic light (red, red + yellow, green, yellow) on exact millis() timing that never drifts, and add a button that switches to a blinking-yellow night mode and back.
- Board
- Nano 33 BLE · Arduino
- Schwierigkeit
- Mittel
- Thema
- Timing & Program Flow
- Zugang
- Kostenlos
Die Aufgabe
Junction Traffic Light With Night Mode
A small junction gets a new traffic light. During the day it runs the usual four-phase cycle. At night the operator presses a button on the controller and the light switches to blinking yellow; pressing it again brings the normal cycle back.
Hardware
- Arduino Nano 33 BLE (nRF52840, 3.3 V logic). Serial console on
Serial1(D0/D1), 115200 baud.Serialis USB and is not seen by the tests. - D2: red lamp, D3: yellow lamp, D4: green lamp. Each is an LED with a series resistor to GND;
HIGH= on. - D5: mode button to GND. Use the internal pull-up (
LOW= pressed) and debounce it (a press counts after 20 ms of steadyLOW).
Requirements
Write the sketch in sketch/sketch.ino (setup() and loop()).
-
Start-up. Open
Serial1at 115200 baud, set up the pins, printREADY, then start the cycle with the red phase straight away. -
Day cycle. The phases repeat in this order, each with exactly these lamps on and the others off:
Phase Lamps on Duration REDred 2000 ms RED_YELLOWred + yellow 500 ms GREENgreen 2000 ms YELLOWyellow 500 ms -
No drift. The schedule must not slip: after any number of cycles, every phase still starts a whole multiple of 5 s after
READYplus its offset. Usemillis()(nodelay()), and plan each phase from the moment the previous one was due, not from when your loop noticed it. -
Console. Every time a phase starts, print its name (
RED,RED_YELLOW,GREENorYELLOW) on its own line. -
Night mode. A debounced press during the day cycle switches to night mode: print
NIGHT, red and green go off, and yellow blinks 500 ms on, 500 ms off, starting on. -
Back to day. Another press leaves night mode: print
DAY, then start the day cycle again from theREDphase (printREDas usual).
Example console output:
READY
RED
RED_YELLOW
GREEN
YELLOW
RED
NIGHT
DAY
RED
Why does the cycle drift?
If each phase is timed as millis() - start with start = millis() taken when the loop sees the change, every phase gets the loop's reaction time added. After many cycles that adds up. Keep the time each phase was due (start += duration) instead.
Wiring
Build the circuit in the Wiring tab: three LEDs, each with its own series resistor, on D2, D3 and D4, and the mode button between D5 and GND.
Success criteria
- The sketch builds.
- The lamps follow the four phases with the given durations (checked 150 ms before and after every change, also in the second cycle).
- Red and green are never on at the same time.
- Night mode blinks only the yellow lamp, and leaving it restarts the day cycle at red.
Was du bekommst
- Deine Firmware wird auf einem emulierten Board gebaut, ausgeführt und durch Hardware-Tests geprüft.
- Kein Entwicklungsboard und keine lokale Toolchain nötig.
Neu bei Embedded? Starte mit der Roadmap: Embedded-Systeme lernen (Englisch)