Two lights, one sequence
Make two outputs take turns and use a timing table to predict their behaviour.
Step 1 — What you will discover
Control two independent outputs and read a program as a sequence of states.
Predict: At any moment, how many of the two LEDs should be on?
Before you begin
Recall that each output keeps its last written state.
Your goal: Coordinate outputs in a sequence. Show this with a prediction, a tested change and an explanation using the program’s names.
Retrieve one idea: what input, state or output did you change in the previous project?
Need a reminder? Make your own light rhythm.
Step 2 — Meet this circuit
Use an Arduino Uno with the Crack the Code shield. The shield already connects its LEDs, knob, light sensor and buttons; this activity needs no jumper wires or breadboard.
If the shield is not fitted, disconnect USB power, align every shield pin with the Uno sockets, and press evenly without bending the pins. Reconnect the Uno with a USB data cable. Leave the traffic-light module disconnected for this activity.
Step 3 — Upload your program
Open the example below. Read the English plan, then choose Arduino C++ from the language selector and Arduino Uno from the board selector. Select Verify to compile. Select Pair board…, choose the Uno’s serial port, then select Upload. Use Chrome or Edge on a desktop computer with this HTTPS page for browser upload. If browser upload is unavailable, copy the complete C++ sketch into Arduino IDE, choose Arduino Uno and its port, and upload there.
Each upload replaces the previous program. Edit the supplied C++ when trying the challenges, then verify and upload again.
Step 4 — Read, edit and run
The English plan and complete Arduino sketch describe the same program. Keep one working copy before making changes.
Step 5 — Run it and collect evidence
Watch the two ends of the shield’s LED row. D13 and D8 should alternate, changing places every half second.
Step 6 — Explain what happened
Set both outputs before each wait. Turning D13 off does not turn D8 on automatically: the program gives each pin its own command. The commands happen much faster than the half-second pause.
Read this part of the actual starter
void loop() {
digitalWrite(firstLed, HIGH);
digitalWrite(secondLed, LOW);
delay(500);
digitalWrite(firstLed, LOW);
digitalWrite(secondLed, HIGH);
delay(500);
}Each state writes both LED outputs before waiting. Writing the new HIGH does not automatically turn the previous LED off. firstLed and secondLed remain fixed while output states change.
Work through one case
At 0 ms D13 is HIGH and D8 LOW. At 500 ms those states swap. At 1000 ms the first pair returns. Each LED completes one cycle per second.
Your turn: complete the trace
Start with both off. Write firstLed HIGH, secondLed LOW; then firstLed LOW, secondLed HIGH. Which LED is on after each pair?
Compare your trace after trying
First D13, then D8. Both states are explicitly written.
If your answer differs
Trace each output separately; setting one pin does not reset another.
Step 7 — Change one thing
Make both lights flash together. Then add an all-off pause between the alternating states. Draw a state table before editing.
Run a controlled experiment
Add a 200 ms interval with both LEDs off between the two states. Predict the new cycle length and diagnose what happens if one LOW write is omitted.
- Save a copy of the working starter. Reset the board so stored state begins from the declared values.
- Write the expected result before editing. Change only the named factor; keep wiring and other settings fixed.
- Edit the C++ in the editor, Verify, then Upload to the connected Uno. The starter simulation does not execute your edited C++.
- Repeat the same input sequence. Record input, expected output, observed output and an explanation. Use labelled serial values where the sketch provides them.
- If the result differs, inspect the relevant condition and pin before changing another factor. Restore and upload the saved starter to recover.
Core task: explain one changed case. Optional extension: choose a boundary or timing case and justify the extra test. Use a paper trace or annotated screenshot when physical manipulation is inaccessible; distinguish predictions from measurements.
Step 8 — Check your understanding
What happens if you remove the line that turns firstLed off? If a light stays on, trace every write to that pin through a complete loop.
If nothing changes: check the power light, successful upload and the selected Uno port. Disconnect power before reseating a shield. Read the first compiler error before changing several lines at once.
Independent check — try before revealing
Remove digitalWrite(firstLed, LOW) from the second state. What will be visible after the first swap?
Hint
Trace each output separately; setting one pin does not reset another.
Reasoning and feedback
Both LEDs remain on: firstLed was never commanded off and secondLed is now HIGH.
Sequential instructions can produce an apparently simultaneous state because the writes are very close together, but code still executes in order.
If your explanation missed a condition or stored value, add that column to your trace and try a new input. A working upload alone does not answer this check.
Step 9 — What happens inside each alternating LED
The sketch drives D13 and D8 to opposite states, then exchanges those states after 500 ms. This red, two-lead LED exposes the same light-emitting principle used by the shield’s indicators; their colour and package can differ.
Choose Cutaway and find the tiny chip, reflector cup and bond wire. Leave Reverse polarity off, set Supply voltage to 5 V, then press Play blink. Watch current and light switch together. Pause before changing the supply to 3 V and then 1 V; the explorer keeps a 220 Ω resistor in series.
Predict and explain: When D13 turns off and D8 turns on, has light travelled from one LED to the other?
Check your explanation
Each LED has its own electrically driven junction. The program changes which circuit carries forward current; light is produced separately in the active chip. The explorer shows one such LED, while the board demonstration shows both outputs. The supply slider changes forward current during an on state. The lesson’s sketch separately determines which outputs are on and for how long.