Variables within the EV3 Software 101 Pro Preview

Variables are at the core of every program. We use them when we have values that we do not know prior to the execution of the program, but during the execution of the program are known. In this tutorial we will take an overview of what a variable is and how can be used in the EV3 Software.

To access the full video please subscribe to FLLCasts.com

Subscribe

  • #934
  • 24 Sep 2018

What is a variable?

Variables are little boxes within the computer, that store some important for us information. Imagine the following situation: you own a bar which offers the best lemonade in town. The lemonade is stored in a small non-transparent barrel, so one can not see how much lemonade there is inside. It is important that you always have fresh lemonade - if the lemonade finish before the next delivery, you will not have lemonade and your customers will be unsatisfied, if you order too early you risk that the lemonade would not be fresh and the customers will not be satisfied again. So the solution is to take a white piece of paper, which will be your variable, and put down the amount of lemonade you have. When a customer order a lemonade, you subtract his order and put down the new amount of lemonade. You repeat that process until you are left with lets say 10 liters of lemonade, when you order a new barrel.

As mentioned in the above situation, the white piece of paper is a variable. It stores a valuable for us information i.e... the amount of lemonade inside the barrel. If we need to program this operation we will have to read the number written on the paper (or in the variable) subtract the amount we have sold and write down on the paper (or in the variable) the new amount. We do not know those values before hand, but they does not matter. We will repeat the same action until the variable has value less or equal to 10.

Creating a new variable i.e.. Initialization

The first step is to create a new variable and set its initial value. The process is called initialization of the variable. Take a variable block and put it on the canvas. The block is the first one under the red (data) palette.

Variable type

Next select the drop down menu and from it Write.

You will see several options for the type of the variable i.e.. the type of the data that the variable will store. There are 5 options:

  • Text- if you select that type you will be able to store any kind of text - just a single symbol like "+" ,or a word like "blue" or even a whole sentence like "This is the text stored within my first variable".
  • Numeric- if you select that type you will be able to store both integers like 1 or -5 and floating point numbers like 3.1415926.
  • Logic- if you select that type you will be able to store either True or False.
    Those values are represented by images as follow:
    •  is used to represent False values
    •  is used to represent True values
  • Arrays- we will be covering them in a later tutorial.

Depending on the case choose the appropriate type. For the purpose of this tutorial however, choose Numeric.

Variable name

Once you have selected the appropriate type of the variable is time to name it. All variables have their names and that is the way to distinguish them. That it why it is extremely important to choose a proper name for the variable. At first that may seem as a silly task and you will prefer to name your variable with names such as "a" or "var1" or "asdf".

However, once you start working on more complicated tasks, where you will need multiple variables you will get into the situation where you have a project with variables that are hard to distinguish. Probably you will be able to know what a and b store during the time you work on your program, but after few days it would be impossible, let aside for someone else. It is important the the variable names are descriptive enough, so that another person could easily understand your program. That program looks a lot better:

To set the name of the variable click on the upper right corner of the block and from the list select Add variable 

Then enter the name you have chosen in the popup window and select ok. 

Reading and writing modes

Once you have initialized your variable you will need to read and write it. That is why the variable block has two modes. If you want to create a new variable or want to replace the old value of a variable and write a new one, choose Write from the drop-down menu.

If you want to read the current value stored in the variable choose Read from the drop-down menu.

Simple example usage

Finally let us take a look at a simple example of a program making use of variables. Sometimes you may need to use a variable, although its' value is not really changing. Let us take a look at the following example:

We are creating a program for precise turn at given number of degrees. For such a program, if you are not using any sensors you will need the distance between the wheels of the robot and the diameter of the tires of the robot. Then you have to perform some calculations with those two values. However if you change your robot, the program should work again. To achieve that we will need just to change those two values, because the distance between the wheels and the diameter of the tires are not changing during the execution of the program. But may change between different runs of the program.

One way is to just type them in throughout the program, but if you reference them in many places, it is easy to make a mistake when you want to change the values. Such mistakes are extremely hard to be found. That is why in such cases the best practice is to define a variable at the beginning of the program for the distance between the wheels (WheelDistance) and one for the diameter of the tires (TyreDiameter) and later on just read from them to perform the calculations.

Note that we have also initialized one additional variable, called Degrees. Its purpose is to store the amount of degrees we want our robot to turn at.

Then we calculate what part of the circle are those Degrees:

Base on the previous calculation and the distance between the wheels, we calculate the distance the wheel of the robot needs to travel in order for the robot to rotate at the desire angle.

Next we calculate the circumference of the tyre i.e. the distance the wheel will travel for one rotation.

Finally, we divide the last two calculated values and get how many rotations the motor should do.