PreAP Computer Science Lego Lab Assignments
Lego Lab 06 / Repetition with the Loop Program Block
80 & 100 Point Versions
Lab Objectives
Configure the Lego loop block to repeat a fixed number of iterations.
Using a single loop structure.
Using sequential loop structures.
Using nested loop structures.
Learn how to use the Sound block.
Integrate the Sound block with the Move block.
# / Actions
01 / Start a New Lego Lab
Create a new Lego lab file, called LegoLab06a.rbt and save in your LegoLab06 folder.
Connect the Lego program block to the computer.
Turn on the Lego program block.
Add a Display block and a Wait block on the program sequence beam, like Figure 01.
Figure 01

02 / Configure the two blocks
Click on the Display block to open its configuration window, like Figure 02a.
Select Image from the Action window.
Check Clear in the Display window.
Select Smile 01 in the File window.
Click on the Wait block to open its configuration window, like Figure 02b.
Select Time from the Action window.
Select 2 seconds from the Until window.
Save, compile and execute the program.
Observe the results on the program block display window.
So far this program seems to be very simple and uninteresting.
Figure 02a

Figure 02b

03 / Add and configure the Loop block
Click the common block (green circle) bar.
Place a Loop block on the sequence beam, like Figure 03a.
Drag the display block and the Wait block inside the Loop block, like Figure 03b.
Click on the Loop block to open its configuration window, like Figure 03c.
Select Count from the Control window.
Select 3 from the Until window.
Save, compile and execute the program.
Observe the results on the program block display window.
Figure 03a

Figure 03b

Figure 03c

04 / Add more blocks
The program execution in step 03 displays a smiley face for six seconds, but the loop part of repeating some instructions three times is not really visible. You will now add two more blocks that will clearly show that the blocks inside the Loop block are repeated three times.
Drag an additional Display block and Wait block inside the Loop block, like Figure 04a.
Click on the second Display block to open its configuration window, like Figure 04b.
Select Image from the Action window.
Check Clear in the Display window.
Select Smile 02 in the File window.
Click on the second Wait block to open its configuration window, like Figure 04c.
Select Time from the Action window.
Select 2 seconds from the Until window.
Save, compile and execute the program.
Observe the results on the program block display window.
The repetition performed by the Loop block should now be more visible.
Figure 04a

Figure 04b

Figure 04c

05 / Creating nested loops
It is possible to nest a Loop block inside another Loop block. Each Loop block needs its own configuration,
Drag a second Loop block next to the first Loop block, like Figure 05a.
Configure the second Loop block to repeat two times.
Drag the entire first Loop block, and its contents, inside the second loop structure, like Figure 05b.
Save, compile and execute the program.
Observe the results on the program block display window.
The repetition performed should now be a total of three times two equals six times.
Figure 05a

Figure 05b

06 / Save Another File and Add a Third Set of Display and Wait Blocks
Save your File as LegoLab06b.rbt and continue to follow the instructions.
Drag a third Display block to the right side of the outer Loop block, like Figure 06a.
Drag a third Wait block to the right side of the third Display block, like Figure 06a.
Configure the new Display block to display a stop sign, like Figure 06b.
Configure the new Wait block to wait for 3 seconds, like Figure 06c.
Save, compile and execute the program.
You should observe that the stop sign displays once after the repeated displays of the smiley faces.
It is very important to understand that the stop sign is not part of any loop structure.
Figure 06a

Figure 06b

Figure 06c

07 / Nest the Third Set of Blocks
You have seen the result of nesting loops. The blocks located inside the inner loop repeat as often as the inner loop and then obey the instructions of the outer loop. If the outer loop repeats two times and the inner loop repeats three times, the contents of the inner loop repeat six times.
This final stage shows that loop contents can be inside a loop structure, but not necessarily inside the inner loop structure. Do you know what will happen?
Drag the latest Display and Wait blocks inside the outer loop structure, like Figure 07.
Be careful not to drag the blocks to the inner Loop block.
Save, compile and execute the program.
Does your observation match your expectations?
Figure 07

08 / Start a New Program With the Sound Block
Start a new file called LegoLab06c.rbt.
Drag a Sound block from the common block group to the program sequence beam, like Figure 08a.
Click on the Sound block to enter the following attribute values in the configuration window.
Click Tone in the Action window.
Click the A key on the keyboard and play it for 1 second.
Slide the sound up to 100 in the Volume window and compare with Figure 08b.
Save the result, compile and execute.
Add seven additional Sounds and configure each block with increasing tones of B C D E F G A.
Save the result, compile and execute.
Figure 08a

Figure 08b

Figure 08c

09 / Continuing for the 80-Point Graded Lab Assignment
Start a new program titled LegoLab06v80.rbt.
It is time to make your robot move again.
(The little robot has been sitting still too much providing a screen.)
For this lab assignment you need the robot to follow a square path.
Now this is certainly the same as a previous program, however, this time you need to use a Loop block.
The square path had many Move blocks, but there was much repetition.
Now that you know how to loop, use only two Move block.
The first block goes straight and the second block makes a 90 degree turn.
After that works correctly, repeat the sequence four times.
To complete the 80-Point lab you also need to follow a square path moving in reverse.
10 / Continuing for the 100-Point Graded Lab Assignment
Save the LegoLab06v80.rbt program as LegoLab06v100.rbt.
The 100-point version starts with the movement of the 80-point version.
Your job now is to add sound to the robot while it moves.
There need to be three sounds.
One for moving forward.
One for turning.
One for moving backward.
Make sure that your sound play continuously while the robot moves. You will not get credit if the robot stops to make sound, moves quietly for a while, and then stops again to make sound.
It is also important to use coasting with the Move block; it makes the movement smoother.

Lego Mindstorms 2010, PreAPCS Edition Lego Lab 06 Page 42 05-26-10