0% found this document useful (0 votes)
630 views

Activity 3.1.5 Variables & Functions VEX V5 2021

Uploaded by

Kidus Fisseha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
630 views

Activity 3.1.5 Variables & Functions VEX V5 2021

Uploaded by

Kidus Fisseha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

PLTW ENGINEERING

Activity 3.1.5

Variables and Functions (VEX V5)

Distance Learning Support


If you are doing this activity away from the classroom, jump to
the flexible path DL: Activity 3.1.5 Variables and Functions.

INTRODUCTION

A program can accomplish a given task in any number of ways. Programs can
quickly grow to an unmanageable size; variables and functions provide a technique
to reduce the size of the program. A variable is a space in your robot’s memory
where you can store data , such as whole numbers, decimal numbers, and
words. Functions group together several lines of code, which can then be
referenced many times in task main or even other functions. The use of variables
and functions allows for complex control of a system.

EQUIPMENT

Computer with VEXcode software


VEX® POE V5 Testbed
RESOURCES

Variables and Functions: VEXcode Software


Procedure

1 With your partner, get your group’s VEX® POE V5 Kit from your teacher. 

2 Recall which of the two-student teams you belong to: 


Team A has the VEX Testbed without the optical sensor.

Team B has the VEX Testbed without the distance sensor and the servo
motor.
At the appropriate time, both teams will exchange testbeds.

3 Connect the VEX POE V5 testbed to the PC.

Figure 1. VEX® POE V5 Testbed


Variables

A variable is a storage location inside a program that can hold a value. Each variable
has associated data storage that represents one value at a time. Every time you save a
value to a specific variable, the previous value is replaced. You can use variables to
track data in a program, such as the number of times a certain action has been done.

4 Open your Lesson3_1_testbed_template and click File > Save As, select the


folder that your teacher designated, and then name the file “A3_1_5_Part1”.

5 Create a new variable named motorCount,  which will track the number of
loop iterations. After you create the variable, blocks that are specific to that
variable appear in the Variables section of the Code menu.

Figure 3. Naming a Variable

Figure 4. New Variable Blocks

Figure 2. Variables Menu

6 Under the when started block, use the Variables blocks to set


motorCount to 1. 
Variable Initialization: You must set an initial value for your
variable before you use it. This establishes a starting value for the
variable that can be changed as the code iterates. For example,
in the next step, you will add a loop, and with each iteration, you
will add 1 to the initial motorCount value.

7 Add a while loop to your code and create its conditional expression as


shown. Be sure to place the while loop after motorCount is initialized.

Figure 5.  while Condition Using a Variable

8 Change motorCount<50 to  motorCount<4.

9 Inside your while loop, add your robot’s movement and increment the index
variable.

Figure 6. Change Variable by 1

Part 1: Using a Variable to Loop n Times


Now that you have set up your loop based on the value of a variable, you need to add
the actions inside the loop you want to repeat.

10 Add to your program to create the program shown.

Figure 7. Part 1 Sample Code

11 Download and run the program. Observe the behaviors.

 PLTW ENGINEERING NOTEBOOK


Document what this program would look like as simple behaviors in
pseudocode.
Consider the placement of the print statement. Could it be moved to
other locations and still have the same result? Move the print
statement to different locations; predict the results before you run
the program. If you are unsure of the variable print results, flip over
the images below.
12 Create a program that will run a motor backward and forward 20 times, 0.5
seconds each way. Use a print block to show how many times the motor has
run back and forth.
a. Test the program and troubleshoot until the expected behavior occurs. 
b. Make sure your code is documented with a task description, pseudocode,
and comments. Save and select share to save the pdf of your code.

Part 2: Using a Variable to Remember a Maximum or Minimum Value

13 Open your Lesson3_1_testbed_template. Click File > Save As, select the


folder that your teacher designated, and then name the file “A3_1_5_Part2”.

14 Create the program below in your A3_1_5_Part2 file.

Figure 8. Provided Code

15 Look through the code and document what this program would look like as
pseudocode.

16 Download and start the program. Slowly turn the potentiometer back and
forth. 
17 Restart the program on the brain and press the bumper switch. Note the effect
as you move the potentiometer back and forth. 

18 Restart the program, and alternate between pressing and not pressing the
button while turning the potentiometer.

 PLTW ENGINEERING NOTEBOOK


Summarize what this program does.

19 Modify the code to include comments describing each section of code. 

20 Click File > Save As, select the folder that your teacher designated, and then
name the file “A3_1_5_Part2_ Modify_A”.
If you have the distance sensor, modify the program so that a variable
“closest” will remember the closest distance that the ultrasonic distance
sensor detects. The limit switch should reset the record.

Need Help?
If you have the optical sensor, modify the program so that a variable
“brightest” will remember the brightest light that the light sensor detects.
The limit switch should reset the record. 

Need Help?

21 Test your program and troubleshoot until the expected behavior occurs. Make
sure your code is commented.

 PLTW ENGINEERING NOTEBOOK


Use pseudocode to describe the behaviors observed.

Part 3: Define a Function


Functions are useful when sections of code are lengthy or when an algorithm needs to
be executed from different parts of a program. To write a function in VEXcode, you
create a custom block. To execute the function, you connect the custom block to your
program. Connecting your function block to execute its code is referred to as “calling a
function.”

22 Open your Lesson3_1_testbed_template. Click File > Save As, select the


folder that your teacher designated, and then name the file “A3_1_5_Part3”.

23 In the My Block section of the code menu, select Make a Block to create a


custom block.

24 In the Make a Block (Preview) pane, enter “turnOnLED” for the block name.
You do not need to add an input, Boolean, or label, so click OK.

Figure 10. Assigning a Name


to a Custom Block

Figure 9. My Blocks Section in


Code Menu

25 A define turnOnLED block appears in your program. 


a. You can attach blocks to this block like you have been doing with the
when started block.
b. Under the define turnOnLED block, connect blocks that make the LED
turn on when the bumper switch is pressed, and off when it is not pressed.

Check your code

26 Connect your new flashLED block to the when started block inside a
forever loop. 

Figure 11. Calling the


turnOnLED Function

27 Download your program. To test your program, press the bumper switch.

 PLTW ENGINEERING NOTEBOOK


Record information about what a function is, and how to set one up in
VEXcode.

Part 4: Reusing a Function

28 Apply the turnOnLED Function to your provided code from Part 2: Figure 8.  
a. Using only the turnOnLED function call block, add the ability for your
program to turn the LED on while the program is running. 
b. Make sure that the original version of the program still functions properly,
the only difference being that you can press the bumper switch to turn the
light on whenever you want while the program is running.

29 Test the program and troubleshoot until the expected behavior occurs. Save
the program.

 PLTW ENGINEERING NOTEBOOK


Use pseudocode and describe the behaviors observed for using a
function in your existing code.

CONCLUSION

1 Describe any challenges that you encountered while developing the


program. The challenges I encountered included

2 Describe applications for which variables and functions could be used.


Applications for variables and functions include

Proceed to next activity

You might also like