Variables

OVERVIEW


In this lesson we are going to take a look at variables. Remember, in static code blocks, such as "navigation" blocks, you can input distance and degree values. These values are static and can only be changed when creating your mission. In many cases, while programming you want certain inputs to be dynamic, or "variable". Variables allow inputs to change while code is executing. Let us explore this concept further.

CODING


A great way to demonstrate the use of variables is to program an aerial shuttle run with Tello. This mission will make use of loops and variables to accomplish the task in a clean and reusable manner. 

We will begin by demonstrating why variables are important. In our aerial shuttle run, Tello will fly forward 12 inches, then come back. During the second leg of the mission, Tello will turn around and fly 24 inches forward, then come back. These steps will repeat in 1 foot increments, with a maximum distance of 5 feet. Without the use of loops and variables our mission code would look similar to Figure 1.

Figure 1: Tello shuttle run at 1', 2', 3', 4', and 5' increments

It is clear that this program includes a great deal of redundant block code! Based on what you know about loops, you may already be thinking this is a great opportunity to refactor our code. You are right! However, there is a problem, given the distances inside each of the "fly forward" blocks are fixed. Somehow those need to be dynamic. This is where variables are valuable. Variables allow us to increment the distances with each loop and make the code more manageable. For example, if you wanted to modify the code from Figure 1 to fly in 24 inch increments instead of 12, you would utilize variables to perform this task. You would have to modify the distances in 10 different blocks to accomplish this.

Take a look at Figure 2, which is a revised version of the code and makes use of nested loops and variables.

Figure 2: Tello shuttle run with nested loops and variables

The block code in Figure 2 may look a little intimidating so here is a video to better explain what is happening:


As you can see, variables allow software to be flexible and will allow you to create powerful missions with DroneBlocks and Tello. Be creative and modify the mission as described in the video. A beautiful aspect of software is that it is easy to modify code and test the results.

CONCLUSION


In this lesson you learned about variables and how they can be included in DroneBlocks code. Continue to experiment with variables to become more familiar with them. In the next lesson we will build upon our knowledge and explore the ability to apply logic to our application.

With loops, variables, and logic you are well on your way to becoming a drone coding champion!