Skip to main content
Teacher Portal

Programming Loops - Blocks-based

Teacher Tips icon Teacher Tips - Purpose of this Activity

Programming repetitive behaviors can be accomplished by using repeat or forever loops. In short, repeat blocks allow the programmer to set a specific number of times for the blocks within its loop to repeat. The forever block repeats the blocks within its loop forever, or until the project is stopped. In this activity, students learn about both.

For more information about programming with repeat or forever loops, visit the help information within VEXcode V5.

The following is an outline of what you students will do in this activity:

  • Watch the Using Loops tutorial video.

  • Explore the Repeating Actions (No Gyro) example project.

  • Compare and contrast between forever and repeat blocks.

  • Complete the Squared Loops Challenge that asks students to move the Clawbot in a square and to action the claw and arm before each turn.

The V5 Clawbot is ready to move!

You can use the Help information inside of VEXcode V5  to learn about the blocks. For guidance in using the Help feature, see the Using Help tutorial.

Teacher Tips icon Teacher Tips

If this is the student's first time using VEXcode V5, they can also see the Tutorials in the toolbar to learn other basic skills.

Materials Required:
Quantity Materials Needed
1

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

1

VEXcode V5 (latest version, Windows, MacOS, Chromebook)

1

Engineering Notebook

1

Using Loops (Tutorial)
 

1

Repeating Actions (No Gyro) example project
 

Step 1:  Let's start programming with loops.

  • Start by watching the Using Loops tutorial video.

  • Open the Repeating Actions (No Gyro) example project.

    Repeating Actions (No Gyro) template icon, illustrating which template project to select for this activity.

  • View the opened example project.

    Example project in the VEXcode V5 Workspace. To the left, the project has a When Started block with a Repeat block attached, set to repeat 4 times. Within the loop is a Drive For block set to forward for 300mm, and a Turn For block set to right for 90 degrees. To the right a note reads 'This program drives a robot in a 300x300mm square by repeating the drive forward and turn commands 4 times'.

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 and then turn right 90 degrees 4 times to complete a square. Instead of using the same 2 blocks 4 times, the repeat block reduces the same actions that 8 blocks would - into 3 blocks. The repeat block 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 (Google Doc / .docx / .pdf) or as a team (Google Doc / .docx / .pdf). 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 (No Gyro) example project.

    VEXcode V5 Toolbar displaying the project name 'Repeating Actions (No Gyro)'.

  • For help, see the tutorial in VEXcode V5 that explains how to Download and Run a Project.

    VEX V5 Toolbar with a red arrow calling out the Tutorials button to the right of the File button.

     

  • 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.

Two code snippets side by side showing the difference in use case between a forever loop (to the left) and a repeat loop (to the right). To the left there is a Comment that reads 'Using sensor input to determine when to turn' with a forever block with an if then block inside. If the Bumper is pressed the robot will turn right 90 degrees, else it will drive forward. To the right the comment reads 'Using a fixed distance to determine when to turn' with a repeat loop set to 4 to drive the robot in a 300mm square.

Look at the Repeating Actions (No Gyro) project (on the right) again. This project will repeat the forward and then turn behavior four times. A repeat block is used when you want to use a set of behaviors for a certain number of times. If the repeat block is replaced with a forever block, the robot would repeat the forward and then turn behaviors forever.

In the project on the left, 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 block is used together with a forever block. In the project on left, the robot will turn right when the BumperH sensor is pressed, otherwise the robot will drive forward forever if the BumperH sensor is not pressed. To continually check the BumperH sensor's value, the if/then block is within a forever block.

The above project on the left is a practical use-case of a structure that repeats forever - using forever blocks and if/then blocks 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 using loops with conditionals, build the Floor Sweeper example project in VEXcode V5.

VEXcode V5 project with a When Started block and a Forever block attached. Inside the forever block is a Repeat until block with the condition set to Bumper8 pressed, and a Drive forward block inside. Next is a Stop driving block with a turn for block set to right for 90 plus pick random 0 to 90 degrees.

  • Begin by having the students mount and wire the bumper switches.
  • Ask students to open the Clawbot (Drivetrain 2-motor, No Gyro) example project.

    Example project icon reads 'Clawbot (Drivetrain two motor, No Gyro)' at the bottom to indicate which template to choose for this project.

  • Ask students to build the project above.
  • Have the students save the project as Floor Sweeper.

    Project dialog box in the VEXcode V5 Toolbar. Slot 1 is selected and the project name reads 'Floor Sweeper'.

  • If students need help saving the project, refer to the Naming and Saving tutorial.

    VEXcode V5 Toolbar with a red arrow calling out the Tutorials button to the right of the File Menu.

     

If the students need help with any of the blocks, refer them to the Help information or tutorial.

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 block was used instead of a repeat block.

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

Step 3: The Squared Loops Challenge!

Sketched path of the movement of the robot showing a green square with arrows indicating moving counterclockwise in a square. At each corner there is a doublesided orange arrow indicating a motion at this point in the path.

  • 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 Repeating Actions (No Gyro) example project as a starting point but save it as SquaredLoops before making any changes.

Project name dialog box in the VEXcode IQ Toolbar. Slot 1 is selected and the project name reads Squared Loops.

In your engineering notebook, plan the following:

  • Plan out your solution and predict what each block 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:

VEXcode V5 sample solution with a When started block and a Repeat block set to 4 attached. There are 6 blocks within the repeat block that read, in order, Drive forward for 300 mm, Spin claw motor open for 70 degrees, spin arm motor up for 360 degrees, spin claw motor closed for 70 degrees, spin arm motor down for 360 degrees, turn right for 90 degrees.

You can provide students with a programming rubric for scoring their projects. Click one of the following links (Google Doc / .docx / .pdf)
Students' engineering notebooks can be maintained and scored individually (Google Doc / .docx / .pdf) or as a team (Google Doc / .docx / .pdf).