В момента ресурсът е наличен само на английски

Using arrays within the EV3 Software 101 Pro Preview

Sometime we have to use a lot of variables storing similar but different data. Let us say we want to 

Необходимо е да се абонирате за FLLCasts.com, за да достъпите това видео

Абонирай се

  • #953
  • 12 Dec 2020

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 to 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:

You 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 circumference 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. We will need just to change those two values. The distance between the wheels and the circumference of the tires are not changing during the execution of the program, but may change between different runs of the program. In the case you can just type them in, but if you reference them in many places 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 and one for the circumference of the tires and later on just read from them to perform the calculations.