Robot Programming 2: Loops

With a loop, you can program your robot to perform a task numerous times. The computer science term for such repetition is iteration.

If we were programming with a language like Python instead of the visual robot language, we'd use a 'while' statement to show our loop:

while some condition is true:
    move forward
    turn left
    move forward
    turn left
    move forward
    turn left
    move forward


With Mindstorms, you can add a loop to your program by choosing the icon with the two circular arrows (6th down in the palette). You drag that icon into your program, then place operations like Move into it. When the robot gets to a loop command, it performs all the operations within it, over and over, until some condition becomes true.


How does a loop end?

With Mindstorms, you can program a loop to complete in a number of ways:
  • count -- have the robot perform the operation n times.
  • time -- have the robot perform the operation for n seconds
  • sensor -- have the robot perform the operation until it senses something (e.g., it sees a color or hits a wall).
In this lab, we'll explore the first two of these.

Student Assignment

1. Write a program rectangle3.rbt that has the robot move in a rectangle three times.
2. Write a program that has the robot move forward and backward for 15 seconds.






    

Recent site activity