Skip to main content
Teacher Portal

Teacher Toolbox icon Teacher Toolbox - Activity Outline

  • This exploration will introduce students to programming repetitive behaviors by using repeat or forever loops.

  • Learning to program using repeat and forever loops allows students to save time when building a project that uses the same repeated actions. For more information about the instructions used in a text project, visit the help information.

The Clawbot V5 is ready to move!

This exploration will give you the tools to be able to start creating some cool projects that use loops.

  • VEXcode V5 Python instructions that will be used in this exploration:
    • drivetrain.drive_for(FORWARD, 300, MM)
    • drivetrain.turn_for(RIGHT, 90, DEGREES)
    • claw_motor.spin_for(REVERSE, 70, DEGREES)
    • arm_motor.spin_for(FORWARD, 360, DEGREES)
      bumper_b.pressing()
    • while True: 
    • for repeat_count in range(4): 
    • wait(5, SECONDS)

You can use the Help information inside of VEXcode V5 to learn about individual Python commands. 

Image showing the VEXcode V5 Workspace with the help notes for the Drive command open

Teacher Tips icon Teacher Tips

If this is the student's first time using VEXcode V5, they can read a variety of articles in the VEX Library.

Image of the VEXcode V5 section of the VEX Library

Teacher Tips icon Teacher Tips - Using Autocomplete

Autocomplete is a feature in VEXcode V5 that predicts the rest of the command you are typing. As students are working in VEXcode V5, encourage them to use the Autocomplete feature to help with the Python syntax. You may notice an autocomplete function as you start to type the instruction. Use your “Up” and “Down” keys to select the name you want, then press “Tab'' or (Enter/Return) on your keyboard to make the selection. For more information on using Autocomplete check out the Python article.

Image of the autocomplete feature in a Python project

Make sure you have the hardware required, your engineering notebook, and VEXcode V5 downloaded and ready.

Materials Required:
Quantity Materials Needed
1

VEX V5 Classroom Starter Kit (with up-to-date firmware)

1

VEXcode V5 (latest version, Windows, macOS)

1

Engineering Notebook

1

Clawbot Template (Drivetrain 2-motor, No Gyro) example project

Step 1:  Let's start programming with loops

  • Before you begin your project, select the correct template project. The Clawbot Template (Drivetrain 2-motor, No Gyro) example project contains the Clawbot's motor configuration. If the template is not used, your robot will not run the project correctly.
  • Select File and Open Examples.

    Image showing the File menu open in VEXcode V5 showing the Open Examples option

     

  • Scroll through the different Example projects. These projects demonstrate a variety of actions your Clawbot can perform. Select and open the Clawbot Template (Drivetrain 2-motor, No Gyro) example project.

    Image showing the Example projects with a red box around the Speedbot (Drivetrain 2-motor, No Gyro) Template Project

     

  • Name the project RepeatingActions.

    Image showing the Repeating Actions project Name in VEXcode V5

  • Type the following code:

    A Python code snippet for a VEX robot program. The code imports the VEX and random libraries, then begins a while loop that drives the robot forward until the bumper switch is pressed. It waits for 5 milliseconds, stops the drivetrain, and then turns the robot right by a random angle between 0 and 90 degrees. The random angle is generated using the random.randint method, and the robot waits for 5 milliseconds after the turn

Look over the project and then do the following in your engineering notebook.

  1. Predict what the project will have the Clawbot do. Explain more than the fact that the project repeats.

    What is it repeating? What is the Clawbot doing?

  2. Write your prediction, but do not break the short project into more than two parts.

Teacher Toolbox icon Teacher Toolbox - Answers

  1. This project will have the robot: drive forward for 300 millimeters , turn right 90 degrees, and then wait for 5 seconds 4 times to complete a square. Instead of using the same 3 instructions 4 times, the repeat instruction reduces the amount that to just 1 time. The repeat instruction repeats the actions of driving forward and then turning.

  2. The prediction might simply be "The Clawbot moves in a square." This would be a succinct way to capture the repeated movements of the Clawbot while lacking any context.

Students' engineering notebooks can be maintained and scored individually or as a team. The previous links provide a different rubric for each approach. Whenever a rubric is included in educational planning, it is good practice to explain the rubric or at least give copies to students before the activity begins.

  • Save, download, and run the Repeating Actions project.

    Screenshot showing the Repeating Actions project title in VEXcode V5

  • Check your explanations of the project in your engineering notebook and add notes to correct them as needed.

Step 2: Run the project and observe the robot

A comparison of two Python code snippets for VEX robots. The top snippet uses sensor input to determine when to turn, with a while loop checking if the bumper switch is pressed; if pressed, the robot turns right 90 degrees, otherwise, it drives forward. The bottom snippet uses a fixed distance to determine when to turn, with a for loop that drives the robot forward 300 mm, turns right 90 degrees, and waits for 5 seconds, repeating this sequence four times

Look at the Repeating Actions project (the second project) again. This project will repeat the forward and then turn behavior four times. A "repeat" loop structure (using a for loop) is used when you want to use a set of behaviors a certain number of times.

If the repeat structure is replaced with a while loop structure, the robot will repeat the forward and then turn behaviors "while" the condition is true. You can also set the condition to "true" to have the while loop continue forever.

In the first project, a sensor's input is used to determine when to begin turning. The project on the right uses a fixed Drivetrain distance to determine when to begin turning.

In order to continually check a sensor's input, an if else statement is used together a while loop. In the project on left, the robot will turn right when the "bumper_b" sensor is pressed, otherwise the robot will drive forward forever if the "bumper_b" sensor is not pressed. To continually check the bumper_b sensor's value, the if statement is within a while loop.

The first project is a practical use-case of a structure that repeats forever – using while loops and if statements together. Imagine a self-driving sweeper that continues to drive forward until it runs into a wall or object, then turns before continuing to drive.

Extend Your Learning icon Extend Your Learning

To further explore how you can use loops with conditionals, have the students build a Floor Sweeper project in VEXcode V5.

If the students need help with any of the instructions, refer them to the Help information.

Ask the students to download and run the project to observe how the robot moves. Then, begin a class discussion and ask the students to explain why the forever structure was used instead of a repeat structure.

The students should note that a forever structure is used because this project continuously checks to see if the bumper switch is being pressed.

Step 3: The Squared Loops Challenge!

A diagram showing a green square pathway with arrows indicating the direction of movement around the square. Additional orange arrows outside the square point in different directions, corresponding to the turns and movements required to follow the pathway

  • Have your Clawbot drive in a square.
  • Before each turn, the claw must be opened and closed, and the arm must be raised and lowered.
  • The Clawbot cannot drive along a side of the square more than once.
  • You can use the RepeatingActions project from above as a starting point but save it as SquaredLoops before making any changes.

Image showing the Squared Loops project name in the toolbar in VEXcode V5

In your engineering notebook, plan the following:

  • Plan out your solution and predict what each instruction in your project will have the Clawbot do.
  • Download and run your project to test it before submitting it.
  • Make changes to the project as needed and take notes about what was changed during testing.

Teacher Toolbox icon Teacher Toolbox - Solution

The following is a potential solution to the Squared Loops Challenge:

Image of the Squared Loops Python project example solution

You can provide students with a programming rubric for scoring their projects.
Students' engineering notebooks can be maintained and scored individually or as a team.