Activity 3.1.5 Variables & Functions VEX V5 2021
Activity 3.1.5 Variables & Functions VEX V5 2021
Activity 3.1.5
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
1 With your partner, get your group’s VEX® POE V5 Kit from your teacher.
Team B has the VEX Testbed without the distance sensor and the servo
motor.
At the appropriate time, both teams will exchange testbeds.
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.
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.
8 Change motorCount<50 to motorCount<4.
9 Inside your while loop, add your robot’s movement and increment the index
variable.
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.
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.
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.
26 Connect your new flashLED block to the when started block inside a
forever loop.
27 Download your program. To test your program, press the bumper switch.
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.
CONCLUSION