Skip to main content

Lesson 4: Repeating Behaviors

Lesson 4: Repeating Behaviors

In the previous Lesson, you learned about variables and used them in a project to draw different sized squares with the 6-Axis Robotic Arm and the Pen Holder Tool. In this Lesson, you will be introduced to the Repeat block and the Change variable block. You will build onto the project from Lesson 3 to draw four squares of different sizes with the same starting location. 

At the end of this Lesson, you will be presented with a project to read. You will sketch what shape you think the 6-Axis Arm will draw, based on the code, in your engineering notebook. You will also determine what the value of the variable will be at the end of the project.

An example of the project that will be built during the course of this lesson, using a Repeat loop to draw squares of different sizes with the Pen. The project will be described in detail as it is built.

Drawing Four Squares of Different Sizes

Now we are going to build on the project from Lesson 3 to draw four squares of different sizes with the same starting location. 

The Repeat Block

A Repeat block is used to repeat the blocks within it a set number of times. The Repeat block saves time and effort while creating projects where blocks repeat. Rather than taking the time to drag in additional blocks or duplicate existing blocks in the workspace, the Repeat block can be used to save space and time.

Open your Unit 6 Lesson 3 project in VEXcode.

The Unit 6 Lesson 3 project reads: When started, Comment: Get set up to draw and set the side length of the square, set side length to 30, and set arm end effector to pen. Next is a comment of Move to the start of the square using absolute movement, and move arm to position x 75, y 125, z 0 mm. Next a comment reads Use relative movement to draw the sides of the square, and there are 4 increment position blocks below it. These read: increment arm position by x side length; increment arm position by y side length; increment arm position by x negative of side length; and increment arm position by y negative of side length.

Add a Repeat block to the end of your project, as shown in this video. In the video clip, the Repeat block is selected from the Toolbox, dragged into the Workspace and attached to the project, below the final Increment position block.

Video file

Drag the Increment position blocks that draw each side of the square into the Repeat block. This will cause drawing all four sides of the square to be repeated.

View this video to see how the blocks should be moved into the Repeat block. In the video clip, the Repeat block is first selected and dragged upwards, and placed beneath the Move to position block. The Comment block reading ‘Use relative movement to draw the sides of the square’ is then selected, and dragged and dropped, with the four Increment position blocks attached, within the C of the Repeat block.

Video file

Change the parameter of the Repeat block from 10 to 4. The parameter of the Repeat block is the number of times the blocks inside it will be repeated.

Since we want to draw our square four times, the parameter is set to 4.

The Repeat block section of the project with the parameter of the repeat set to 4. The blocks within the loop are the comment and four increment position blocks.

Predict how the 6-Axis Arm will move based on this project. 

Write your prediction in your engineering notebook, and sketch what you think the 6-Axis Arm will draw when the project is run.

The complete project at this point. The project now reads: When started, Get set up to draw and set the side length of the square, set side length to 30; set arm end effector to pen. Next Move to the start of the square using absolute movement and move arm to position x 75, y 125, z 0 mm. Finally the Repeat loop is set to 4, and within the loop it reads Use relative movement to draw the sides of the square; increment arm position by x side length mm; increment arm position by y side length position; increment arm position by x negative of side length mm; and finally increment arm position by y negative of side length.

Be sure the 6-Axis Arm is connected to VEXcode. Run the project to test it. (Remember to stop the project when the 6-Axis Arm has finished moving.) 

Did the 6-Axis Arm move as you predicted? Why or why not? Document the behaviors in your engineering notebook.

The 6-Axis Arm with the Pen attachment in the ending position having drawn a 30mm square in the center of the Whiteboard.

Increasing the Variable

When you tested the project after adding the Repeat block, you noticed that the 6-Axis Arm did draw four squares but they were all the same size. That is because the sideLength variable did not change at any time. To change the size of the square that is drawn, we need to change the value of the sideLength variable. We can change the value of the sideLength variable each time the Repeat block runs. This will change the size of each of the four squares.

The Change variable block will run in each iteration (or repetition) through the Repeat block. By changing the value of the sideLength variable with each iteration, we can create a project that draws four squares that increase in size. 

Add a Change variable block to the bottom of the project. In the video clip, the Change variable block is selected in the Toolbox, dragged to the Workspace and dropped between the final Increment position block and the bottom of the Repeat block.

The Change variable block allows a variable to be set to different values in a project. This is helpful as it can change a variable that is used in multiple places within a project.

Video file

Set the Change variable block to the variable sideLength and the parameter to 20. In the video clip, the variable parameter is selected, and sideLength is selected from the dropdown menu. Then the value parameter is selected, and 20 is typed in the space.

The parameter is the amount we want to change the value of the variable. In this case, it will change by 20 each time that block is run.

Video file

Add a Comment block to the project to explain the intention of the Change variable block. The purpose of the Change variable block in this project is to increase the value of the sideLength by 20mm in each iteration of the Repeat block.

The bottom portion of the project with a Comment block added between the final increment position block and the change variable block. The comment reads Increase the side length of the square.

Predict how the 6-Axis Arm will move based on this project. 

Write and sketch your prediction in your engineering notebook.

The complete project. The project now reads: When started, Get set up to draw and set the side length of the square, set side length to 30, and set arm end effector to pen. Next, a comment reads Move to the start of the square using absolute movement, then move arm to position x 75, y 125, z 0 mm. Then, the Repeat block is set to 4 and contains the following blocks: Comment: Use relative movement to draw the sides of the square; increment arm position by x side length mm; increment arm position by y side length mm; increment arm position by x negative of side length; increment arm position by y negative of side length mm; Comment: Increase the side length of the square; Change side length by 20.

Be sure the 6-Axis Arm is connected to VEXcode. Run the project to test it.

Did the 6-Axis Arm move as you predicted? Why or why not? 

What do you notice about the size of the squares? How did they change for each iteration through the Repeat loop? 

Document your observations in your engineering notebook.

The 6-Axis Arm with the Whiteboard Attachment after the project was run, showing four nested squares of different sizes on the Whiteboard all starting from the same coordinate.

Rename your project, and then save it to your device.

Project name box in the VEXcode Toolbar highlighted with a red box. The project name reads Unit 6 Lesson 4.

For Your Information

The Monitor Console can also be used to monitor variable values. The Monitor Console can provide real-time reports of a specific variable in a project. Variables in the Toolbox can be added to the Monitor Console by selecting and dragging the variable block to the Monitor Console icon in the Workspace. Watch the video below to see how a variable can be dragged from the Toolbox to the Monitor Console.

Video file

Activity

Now that you have drawn four squares of different sizes with the same starting location, you will complete one more activity to help you understand how the Change variable and Repeat blocks work in a project. Read the code below and sketch what shape you think the 6-Axis Arm will draw in your engineering notebook. You will then determine what the value of the variable will be at the end of the project.

Screenshot of the Unit 6 Lesson 4 activity code

Activity: Follow the procedure you learned in Lesson 2 to read and document the code in the project above.

  1. Document the project above in your engineering notebook.
    1. Predict what you think the 6-Axis Arm will do when the project is run. Document your prediction in your engineering notebook.
    2. Read each block in the project, and visualize the behavior the 6-Axis Arm would perform when executing the block. Record this behavior in your engineering notebook.
    3. Sketch the path the 6-Axis Arm would take when the project is run. What shape(s) are made?
  2. Predict the value that the sideLength variable will be at the end of the project. Document this value in your engineering notebook.
  3. Check in with your teacher when you are finished. You will watch the project run as a class. Then you will discuss your prediction and documentation.

Check Your Understanding

Before moving to the next Lesson, ensure that you understand the concepts in this Lesson by answering the questions in the document below in your engineering notebook. 

Check Your Understanding questions > (Google Doc / .docx / .pdf)


Select Next > to complete the Putting It All Together activity.