Full teaching narration is free with Private Starter.Create free account
Back to curriculum
Computer ScienceGrade 4· U.S. National — Common Core & NGSS
Aligned to:U.S. educational frameworks

Program a Pedestrian Signal with Loops

Students create and test a repeating sequence of commands that models a safe pedestrian crossing signal.

Program a Pedestrian Signal with Loops

Illustrations are auto-generated and may be placeholders. They can be refreshed to match the narration.

Full teaching narration is included free with a Private Starter account.Create free account

Observe the Signal Pattern

A pedestrian signal uses lights, symbols, and sometimes a countdown to tell people when to cross. Watch one full cycle before trying to program it. In our model, the white walking person appears for 6 seconds. Next, an orange hand flashes for 4 seconds while the countdown changes from 4 to 1. Finally, a steady orange hand appears for 6 seconds. Then the same cycle begins again. The walking person means people may begin crossing after checking for traffic. The flashing hand means people already crossing should finish, but others should not start. The steady hand means wait. Record the order, color, symbol, and time for each phase. These observations will help you find the repeating pattern.

A three-phase pedestrian signal timeline shows the white walking person, flashing orange hand with countdown, and steady orange hand with their times.
A three-phase pedestrian signal timeline shows the white walking person, flashing orange hand with countdown, and steady orange hand with their times.Source: Illustrated for this lesson

Identify the Repeating Steps

A pattern is an ordered set of elements that follows a rule. The repeating unit is the smallest complete group of steps that occurs again and again. For this signal, the unit is walk, flashing hand with countdown, and steady hand. The times are part of the pattern too: 6 seconds, 4 seconds, and 6 seconds. If the cycle repeats three times, the phases are walk, flash, wait, walk, flash, wait, walk, flash, wait. You can predict that the next phase will be walk. Compare observations with a partner. If one person writes only walk and wait, ask whether a safety phase is missing. Working together helps the group identify every step and place the steps in the correct order.

Three identical groups of walk, flash, and wait appear in order, with a bracket marking the smallest repeating group.
Three identical groups of walk, flash, and wait appear in order, with a bracket marking the smallest repeating group.Source: Illustrated for this lesson

Build the Command Sequence

Now turn each phase into precise commands a computer can follow. A command tells the program to perform one action. Begin by turning on the walk symbol and turning off the hand. Wait 6 seconds. Then turn off the walk symbol and flash the hand while showing 4, 3, 2, and 1, one number each second. After the countdown, keep the hand on steadily and wait 6 seconds. At every phase change, make sure the old symbol turns off before the new one turns on. Otherwise, both symbols could appear together and give confusing instructions. Arrange command cards in order, then read them aloud with your group. A classmate can act like the computer and perform exactly what each card says.

Ordered command cards show the walk symbol turning on, turning off, the hand flashing through four numbers, and the hand staying steady.
Ordered command cards show the walk symbol turning on, turning off, the hand flashing through four numbers, and the hand staying steady.Source: Illustrated for this lesson

Add a Loop

Writing the same commands many times makes a program long and harder to change. A loop tells the computer to repeat a group of commands. Place the entire signal sequence inside a repeat-forever loop: show walk for 6 seconds, flash the hand and count down for 4 seconds, and show the steady hand for 6 seconds. When the computer reaches the end, it returns to the first command. One cycle lasts 16 seconds because 6 plus 4 plus 6 equals 16. After 32 seconds, the loop has completed two cycles. Check the loop boundary carefully. If only the countdown is inside the loop, the countdown will repeat, but the signal will never return to walk. The loop must surround the full repeating unit.

A large loop boundary surrounds the complete walk, flashing countdown, and steady-hand sequence, with the total cycle time shown.
A large loop boundary surrounds the complete walk, flashing countdown, and steady-hand sequence, with the total cycle time shown.Source: Illustrated for this lesson

Test and Debug the Signal

Testing checks whether the program meets its goals. Run the signal and use a table to record each symbol, its time, and what happens next. The design criteria are a clear order, correct timing, no conflicting symbols, and continuous repetition. A constraint might be that the model has only two lights or must complete a cycle in 16 seconds. Suppose testing shows the walk symbol stays on while the hand flashes. This bug could confuse a pedestrian. Debug the program by adding a command that turns off the walk symbol before the flashing hand begins. Test again after every change. Compare two possible fixes with your group and choose the one that meets all criteria within the constraints. Explain your evidence, listen to other ideas, and revise when needed.

A testing table reveals overlapping walk and flashing-hand symbols, followed by a corrected sequence with the overlap removed.
A testing table reveals overlapping walk and flashing-hand symbols, followed by a corrected sequence with the overlap removed.Source: Illustrated for this lesson

Discuss Safety Rules

Pedestrian signals support traffic laws and community safety rules. These rules organize how drivers, cyclists, and walkers share intersections. A walk symbol does not guarantee that every vehicle has stopped, so pedestrians should still look left, right, and left again and watch for turning vehicles. People should not begin crossing when the hand is flashing or steady. Rules affect society by making behavior more predictable and reducing danger. People can also work together to improve rules and signal designs. For example, a community may request a longer crossing time near a school so children and people with disabilities have enough time. Discuss the proposal respectfully: What safety problem does it address? Who would benefit? What traffic constraints must engineers consider? Use evidence from signal tests to explain whether the change should be made.

A school intersection shows a pedestrian checking both directions while turning vehicles wait and a proposal requests more crossing time.
A school intersection shows a pedestrian checking both directions while turning vehicles wait and a proposal requests more crossing time.Source: Illustrated for this lesson