All pages
Powered by GitBook
1 of 2

Loading...

Loading...

Variables Practice in Blocks

In Blocks, the Variable menu will first appear empty.

Default Variable menu

Once variables are created, they will populate in the menu.

Variable menu with created variables

HOWEVER, if a variable is not used when a program is saved then exited, it will be removed from the list again upon reopening the program. This is helpful for clearing unused or variables made by mistake, but may cause confusion for students of why something has been deleted.

Variable Activity

In a new OpMode, locate the telemetry blocks under Utilities. Add two for displaying numerical data to the OpMode's loop.

The keys will be set to "Answer 1" and "Answer 2" as seen below:

Telemetry blocks added to the OpMode

Create a variable called "A" and one called "B". Make "A" the number for the first telemetry and "B" for the second:

Variables added to the telemetry

Right now, our variables don't equal anything. Try saving your OpMode and seeing what happens when trying to run it.

Let's start with using Constant Variables. We're set the variables during initialization so need to add the appropriate blocks here from the Variable menu:

Now "A" and "B" can be set to a value using a number block from the Math menu. For this example, "A" is being set to 5 and "B" to 23:

What happens if you save and test the OpMode now?

With our variables set, we can have the robot do some math for us to test out our variables!

Using the Math menu we can choose different equations to add to our telemetry:

Save and test your OpMode!

Consider:

Setting Variables

Adding Math

Adding blocks to set the variables during initialization
Add a value to the variables
Example of math added to the telemetry blocks

Introduction to Variables

What is a Variable?

In math, it's common to see problems like below:

a+8=15a + 8 = 15a+8=15

We can solve for "a" to determine a = 7, right? Then we could solve a variety of additional calculations with that knowledge like:

a+10=?a + 10 = ?a+10=?
2a=?2a = ?2a=?
34/a=?34/a = ?34/a=?

Variables in programming serve the same role! We use a variable to represent a value, or other information, that then can be used throughout the program.

Why use a Variable?

Consider for a moment: Why would we use a variable when we could simply use the number on its own?

While there are several reasons, one of the biggest is that variables can greatly help with organization in a program and streamline making changes.

Rather than having to hunt down every place a value has been used in a program, if we need to make a change we can adjust just the variable and be done! This helps to prevent mismatches that could come from one value being updated, but another missed.

An example of a variable used in multiple places

Variables might also be used to store the results of a calculation in a program to then be used wherever it's needed, such as the values from joystick movements on the gamepad.

Types of Variables

Constant variables do not change after being set. These are often set during initialization or at the start of an OpMode.

These variables might include something like a target velocity, position, or power that will be reused or other settings that need to remain consistent whenever called.

Non-constant variables are often just called variables. These change throughout the program, but may be set during initialization or the beginning of an OpMode to a default.

These variables, for example, may store the results of a calculation run in the OpMode or data collected by a sensor.

Example of Constant Variables in Blocks
Example of Non-Constant Variables in Blocks