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.

Carte
Nano 33 BLE · Arduino
Difficulté
Moyen
Thème
Timing & Program Flow
Accès
Gratuit

La tâche

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. Serial is 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 steady LOW).

Requirements

Write the sketch in sketch/sketch.ino (setup() and loop()).

  1. Start-up. Open Serial1 at 115200 baud, set up the pins, print READY, then start the cycle with the red phase straight away.

  2. Day cycle. The phases repeat in this order, each with exactly these lamps on and the others off:

    PhaseLamps onDuration
    REDred2000 ms
    RED_YELLOWred + yellow500 ms
    GREENgreen2000 ms
    YELLOWyellow500 ms
  3. No drift. The schedule must not slip: after any number of cycles, every phase still starts a whole multiple of 5 s after READY plus its offset. Use millis() (no delay()), and plan each phase from the moment the previous one was due, not from when your loop noticed it.

  4. Console. Every time a phase starts, print its name (RED, RED_YELLOW, GREEN or YELLOW) on its own line.

  5. 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.

  6. Back to day. Another press leaves night mode: print DAY, then start the day cycle again from the RED phase (print RED as 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

  1. The sketch builds.
  2. The lamps follow the four phases with the given durations (checked 150 ms before and after every change, also in the second cycle).
  3. Red and green are never on at the same time.
  4. Night mode blinks only the yellow lamp, and leaving it restarts the day cycle at red.

Ce que vous obtenez

  • Votre firmware est compilé et exécuté sur une carte émulée, puis vérifié par des tests matériels.
  • Aucune carte de développement ni toolchain locale requise.

Débutant en embarqué ? Commencez par la feuille de route : Apprendre les systèmes embarqués (en anglais)