Simple example variable usage Pro Preview

To access the full video please subscribe to FLLCasts.com

Subscribe

  • #1288
  • 30 May 2019

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. 

Courses and lessons with this Tutorial

This Tutorial is used in the following courses and lessons

Image for Instructors Remote Training
  • 136
  • 280:11
  • 156