1.4 Loops

In this task you will learn some techniques for putting some structure into your program so that you can come up with a longer dance routine. As you work through this challenge page, you will learn about:

  • the Loop block
  • how to adjust a Loop block – Forever, Time, and Count
  • nested Loop blocks

The challenge

How can you program a robot to repeat a sequence of moves?

dance2_01

A dance robot that uses nested loops?

 

1. Get your robot ready and start NXT-G

To begin, you will need your robot from last week. Start NXT-G with a new program called ‘Loopy’… dance2_02

2. Create a simple dance

a) Please copy the following program… dance2_03

Take note of the configuration panel for the first Move block. I’ve set the Duration (2 Rotations) and Next Action (Coast) the same for all four blocks.

Note: The only difference for each block is the Steering slider. To see how I’ve set this, take a close look at the bottom left corner of each block.

b) Can you predict what this program will do before you run it?

c) Download and run the program. Does it do what you were expecting?

3. Introducing the Loop block

What if we want this sequence of moves to be repeated?

One approach could be to select the blocks, then Copy and Paste them onto the end of the program, but there is much better way that involves using the Loop block.

a) Drag a Loop block from the Common palette … dance2_04

dance2_05

… and drop it into position in your program … (It doesn’t really matter where it’s placed, but at the beginning works pretty well.)

dance2_06

… then drag each of the Move blocks you already created into the Loop … (You can either do this one-by-one as shown below, or by dragging in the whole lot in one go.)

dance2_07

… and hopefully you’ve got a program that looks like this …

dance2_08

b) Now, try out the program and see what it does!

Note: As you may have guessed/realised, this program will never stop itself (or at least not until the battery runs flat!).

Just in case you’re wondering, you can stop a program running at any time by pressing the dark grey ‘Back’ button on the NXT.

4. Adjusting the Loop block

a) If you select the Loop block, you will be able to access the Loop block’s configuration panel (bottom left below)…

dance2_09

The setting that you will be adjusting here is the Control. There are five options, but we’ll just be concentrating on three of these at the moment…

  • Forever – Not surprisingly, this means repeat the set of instructions inside the loop forever.
  • Time – Repeats the loop until the specified amount of time.
  • Count – Repeats the loop the specified number of times.

Note: The ‘Show: Counter’ option at the bottom of the configuration panel is pretty handy for some more advanced programming techniques. We might come back to it in a later challenge, but it won’t mean a lot at the moment, so you can leave it deselected for now.

b) Experiment with each of the Time and Count loop settings to see how they work. For each of the following, adjust the loop and then use your robot to test it out.

  • How do you set the loop to repeat five times?
  • How do you set the loop to repeat for 20 seconds?

Extension question: If you set the loop to repeat for 20 seconds, does it run for exactly 20 seconds? If not, why not?

5. Nested loops

Here’s another example showing how loops can be ‘nested’ inside each other.

dance2_10

In this program, the inside loop (selected) is repeated four times. If you look at the directions indicated on the Move blocks, you will see that this means the robot will turn left and then right, four times. After this, the robot will move forward for two rotations. The entire routine is then repeated over and over and over…

Copy it and try it for yourself.

Feel free to experiment with the Move block duration settings.

Next: The great dance off!