Teach lesson
Visual programming with a real Arduino (2/4): sequences and loops
Students build LED sequences with visual blocks, replace repeated copies with loops, and test timing on a real Arduino board.
Learning Outcomes
Create a light pattern with a clear sequence.
Use a loop to reduce repetition.
Explain which part of the program repeats and why.
Student activity preview
Activity Content
Preview only. In a class session, students can fill in responses and submit their work to the teacher.
Design the pattern
10 min
A pattern is a sequence you can recognize and that repeats. For example:
- Three quick blinks and a long pause.
- Short on, short off, long on.
Notice the difference between two ways of repeating:
- Arduino run first: runs once when the program starts; today's light pattern
does not go there.
- Arduino loop forever: repeats everything inside it, without stopping.
- repeat N times: a block (in the *Loops* category) that repeats only one part a fixed number of times. It saves you from copying the same blocks over
and over.
Describe the pattern you will try to create with the built-in LED. It must have at least three observable steps (for example: blink, blink, blink, pause).
Fill the table with one row for each step of your pattern before opening the
lab. If your pattern has four steps, use four rows and leave the extra rows
empty. In each row, state the output (built-in LED), the action (on, off,
or wait), and the approximate duration in milliseconds.
| Step | Output | Action | Duration (ms) |
|---|---|---|---|
Long version: step by step
15 min
First program the pattern without the repeat block. Copy on, off, and wait
blocks as many times as needed so the pattern plays once. Put it all inside
Arduino loop forever.
Open
arduino-visual-board.Inside Arduino loop forever, build the pattern with Input/Output blocks (
set built-in LEDHIGH/LOW) and Time blocks (wait).Press Verify / compile.
Press Upload into device.
Watch one full repetition of the pattern on the real board.
What is the drawback of building the whole pattern by copying the same blocks over and over?
Loop version
22 min
Now use the repeat N times block (*Loops* category) to repeat only the part
that repeats. Choose how many times (for example, 3).
In the Loops category, drag the repeat [ ] times block.
Put inside its
dosection only the part that repeats (one blink).Leave outside the loop whatever should happen only once (for example, the long pause at the end).
Press Verify / compile, Upload into device, and observe.
If the pattern is cut off or too fast to see on camera, raise the times (200-1000 ms).
Your loop program should look roughly like this (three quick blinks and a pause,
repeating):
Reference diagram for the loop pattern. The long 1000 milliseconds pause stays outside repeat 3 times, but inside Arduino loop forever.
Arduino loop forever:
repeat [3] times
do:
[set built-in LED] [HIGH]
[wait] 200 milliseconds
[set built-in LED] [LOW]
[wait] 200 milliseconds
[wait] 1000 millisecondsWhich blocks ended up inside the repeat loop? What would happen if you also put the long pause (which should happen only once) inside the loop?
How many blinks in a row did you observe before the pause on the real board?
Reflection
8 min
Explain one improvement you made between the long version and the loop version. What evidence made you decide it?