Skip to main content

Lesson 3: Variables

Lesson 3: Variables

In the earlier Lessons of this Unit, you used a combination of absolute and relative movements to draw a square. In this Lesson, you will learn about variables and how they can be used to store values in a project. Variables can be used to make projects more efficient.

At the end of this Lesson, you will use variables to draw squares of different sizes.

Two 6-Axis Arms with the Whiteboard attachment are shown side by side. On the left, the 6-Axis Arm has drawn two small squares, one on the lower left of the whiteboard, and one on the upper right of the whiteboard. On the right, the 6-Axis Arm has drawn two larger squares, in the same locations on the whiteboard.

Drawing A Square

Previously, you coded the 6-Axis Robotic Arm to draw a square on the Whiteboard.

Open your VEXcode EXP project from Lesson 1 of this Unit and edit the project to match the one in the image shown here.

VEXcode project begins with a when started block, and a comment that reads get set up to draw, followed by a set arm end effector block set to pen. Next a comment reads Move to the start of the square using absolute movement, followed by a move to position block that reads 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, followed by 4 increment position blocks that read increment arm position by x 50 mm, increment arm position by y 50 mm, increment arm position by x - 50 mm, and finally, increment arm position by y -50 mm.

Ensure the 6-Axis Arm is connected to VEXcode EXP and run the project.

Notice that the 6-Axis Arm draws the square as intended.

The VEXcode EXP Toolbar with the Start button highlighted in a red box, between a green Arm icon to the left and the Step button to the right.

Look at the project again. You have parameters that you were repeatedly using (50, 50, –50, –50).

The same project from above, with parameters of the increment position blocks called out. The x parameter of the first and third block, and the y parameter of the second and fourth block are highlighted in a red box, showing that the values repeat.

What would happen if you needed to change the size of your square? You would need to type the updated value into every Increment position block. This could lead to possible errors with typing mistakes, forgetting to update a block, or forgetting to add the negative before the parameter. 

Instead, you can use a variable to store these values and quickly update them to draw squares of different sizes.

Using Variables

A variable is a way of storing a value to be used later in a project. You will now add a variable to your project to store the value of the side length of the square.

To add a variable, scroll down to the bottom of the Toolbox and select Make a Variable.

The Variables section of the VEXcode Toolbox, with the 'Make a Variable' button highlighted in a red box.

Name your variable "sideLength". Then select Submit.

This variable will be used to store the value of the side length for the square.

New Numeric Variable window in VEXcode reads New numeric variable name: with a dialog box to enter the name. The name reads side length. On the bottom, there are two buttons, a gray cancel button to the left and a blue Submit button to the right.

The sideLength variable will now appear as a block in the Toolbox.

The Variables section of the VEXcode Toolbox with the side length variable reporter block highlighted in a red box between the my variable reporter block and the set variable block.

Add the Set variable block to the beginning of your project. 

The same project from above, with a set variable block added between the first comment of Get set up to draw, and the set end effector block. The start of the project now reads When started, Get set up to draw, set my variable to 0, set arm end effector to pen.

Change the drop-down parameter of the Set variable block from myVariable to sideLength

The start of the project from above, with the variable parameter drop down menu open, and side length selected from the options.

Set the sideLength variable to 50 to match the side length of the square being drawn. 

50mm is the same side length of the square from the original project.

The start of the same project, with a red highlight box around the value parameter of the set variable block set to 50. The start of the project now reads When started, get set up to draw, set side length to 50, set arm end effector to pen.

Add to the top Comment block to the project to explain the Set variable block.

The start of the same project with the first comment edited to now read Get set up to draw and set the side length of the square.

Add the sideLength variable into the first Increment position block. In the video clip, the sideLength variable is selected in the Toolbox, and dragged into the x-parameter of the first Increment position block.

Notice the yellow highlight around the x-parameter when the sideLength variable moves over the Increment position block. This indicates that when the block is released, the block will be added in that parameter. This is because the variable block is a circular reporter block. These blocks will fit in any space that has a circular shape.

Video file

Add the sideLength variable to the next Increment position block as the y-parameter.

The bottom of the project, showing the four increment position blocks. The y parameter of the second block is highlighted in a red box, showing that the side length block has been added as the parameter.

Notice that the next two Increment position blocks in the project move the 6-Axis Arm in the negative direction.

The bottom of the project from above, with the four increment position blocks shown. The x parameter of the third block and the y parameter of the fourth read negative 50, and are highlighted in a red box.

You can use a green Function block with the negative parameter selected in order to use the negative value of a variable in your project. A function block with the negative parameter selected. The block reads negative of 0.

Add the Function block into the x-parameter of the third Increment position block as shown here.

In this video clip, the Function block is selected in the Toolbox, and dragged into the Workspace, and dropped into the x-parameter of the third Increment position block. 

Video file

Set the dropdown parameter of the Function block to negative.
 

The end of the project, showing the function block's parameter dropdown open, and negative selected.

Add the sideLength variable to the Function block. Now the 6-Axis Arm will move for 50mm in the negative x-direction when the block is executed.

The same bottom section of the project, with the side length block added to the value parameter of the third increment block. The block now reads, increment arm position by x negative of sidelength millimeters.

Duplicate this for the y-parameter of the fourth Increment position block.

Remember that you can duplicate blocks by right-clicking to open the Context menu, then selecting "Duplicate Blocks."

The four increment arm blocks are shown, with the y parameter of the final block set using the function and side length blocks. The block now reads increment arm position by y negative of side length mm.

Ensure the 6-Axis Arm is connected to VEXcode EXP and run the project.

VEXcode Toolbar with the Run button highlighted in a red box, between a green Arm icon to the left and the Step button to the right.

Stop the project after the 6-Axis Arm completes the square. 

Note that the square drawn by the 6-Axis Arm is the same as the one created earlier using the Increment position blocks and setting each parameter.

VEXcode Toolbar with the Stop button highlighted in a red box, to the left of the Share button.

Changing Variable Values

Now that you have created the variable, you can easily change it to another value. This is one of the benefits of using a variable in a project rather than inputting each value separately. You will now practice changing the value of the variable in the Set variable block, then running the project to see the different size squares being drawn by the 6-Axis Arm.

Change the Set variable block from 50 to 90.

What do you think will happen when the project is run? Record your prediction in your engineering notebook.

The full project from above, with the set variable block set to 90. The start of the project now reads When started, Get set up to draw and set the side length of the square, set side length to 90.

Run the project.

Do the behaviors of the 6-Axis Arm match your prediction for the project?

The 6-Axis Arm draws a square with side lengths of 90mm. This is because the sideLength variable is set to 90. All of the values in the Increment position blocks are updated to be 90 or –90 because of the variable.

Stop the project after the 6-Axis Arm stops drawing.

VEXcode Toolbar with the Run button highlighted in a red box between a green Arm icon on the left, and the Step button on the right.

Change the Set variable block from 100 to 30.

What do you think will happen when the project is run? Record your prediction in your engineering notebook.

The same project from above, with the set variable block highlighted in a red box, and the parameter set to 30. The start of the project now reads When started, Get set up to draw and set the side length of the square, set side length to 30.

Run the project.

Do the behaviors of the 6-Axis Arm match your prediction for the project?

The 6-Axis Arm draws a square with side lengths of 30mm. This is because the sideLength variable is set to 30. All of the values in the Increment position blocks are updated to be 30 or –30 because of the variable.

Stop the project after the 6-Axis Arm stops drawing.

VEXcode Toolbar with the Run button highlighted in a red box, between the green Arm icon to the left and the Step button to the right.

Be sure to rename and save your project.

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

Activity

In this Lesson you learned about variables and how to use them effectively in a project to draw a square. Now, you will practice this concept to draw multiple squares of different sizes. Two 6-Axis Arms with the Whiteboard attachment are shown side by side. On the left, the 6-Axis Arm has drawn two small squares, one on the lower left of the whiteboard, and one on the upper right of the whiteboard. On the right, the 6-Axis Arm has drawn two larger squares, in the same locations on the whiteboard.

Activity: Edit your project from this Lesson to draw different squares. 

  • The starting coordinates of the first square are (75, 125, 0). 
  • The starting coordinates of the second square are (125, 25, 0). 
  • All side lengths should be 35mm.

Part 1: Build onto your project to draw two 35mm squares.

A top down view of the 6-Axis Arm with the Whiteboard attachment on the Tile. Two 35mm squares are drawn, one to the lower left and one to the upper right.

  • Plan how you will build onto your project to draw the squares with your group. Be sure that you are all agreed on your approach before you begin editing the project. 
  • Edit the project in VEXcode to draw two separate 35mm squares. 
  • Run the project to test it. Does it draw the squares as you intended? If not, continue to edit the project until you have successfully drawn two separate squares. 

Part 2: Edit your project to draw two 70mm squares. 

A top down view of the 6-Axis Arm with the Whiteboard attachment on the Tile. Two 70mm squares are drawn, one to the lower left and one to the upper right.

  • Plan how you will build onto your project to draw the squares with your group. Be sure that you are all agreed on your approach before you begin editing the project. 
  • Edit the project in VEXcode to draw two separate 70mm squares. 
  • Run the project to test it. Does it draw the squares as you intended? If not, continue to edit the project until you have successfully drawn two separate squares. 

Pro Tips for this Activity: 

  • Erase the Whiteboard after each run of your project, so that you can clearly see what the Pen is drawing each time you test your project. 
  • Practice your code reading skills – Have someone in your group read your code before you run the project. This can be helpful to find any mistakes in the code before it is run, and to be sure that your group has a clear understanding of what the project is intended to do. 

Check Your Understanding

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

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


Select Next > to move on to Lesson 4.