🔢Lesson 7: Variables and Functions
Last updated
Last updated
This lesson aims to give you an understanding of how you can improve your program with variables and functions. You've already tried out some functions, but here you will learn how to combine them with variables to create more readable, expandable and more efficient code. If you have any questions about variable blocks, you can check out the Advanced Blocks section.
Variables are a fundamental concept in programming that allow you to store and manage data throughout your program. They are like containers that hold values, and you can refer to the values by using names. For example if you want to associate the variable with velocity, you can call it 'speed'.
Do you remember your first robot dance in Lesson 1? Well, what if you want to modify the speed of your arms? Normally you would have to modify them one by one, but with variables it's enough that you change only the variable's value and the rest of your arm values will change accordingly as seen in the example below.
In Lesson 5 and 6 we've already covered some of the basics of creating multiple events using the 'skill tree' concept. Here you will learn how to build more complex skill trees using variables.
Instead of nesting your individual sequences in each bump sensor event block, this time you can create variables to control how events are triggered. When you go to the 'Variables' tab in Advanced Blocks, you will find that it doesn't have any default variables, no worries, you can create one by clicking on 'Create Variable'. A desktop tab will appear where you can name your variable, for this example let's use the name 'Reaction'.
Once you have created your variable, Misty will automatically populate your 'Variables' tab with the variable block, a set-variable-to block and a change-variable-by block.
Now you will need to decide what you want Misty to do, and then create multiple bump sensor events that will determine how Misty should respond when the events are triggered. By using variables you can create a more complex skill tree. To do that you will need to add a set-variable-to block and define it's conditions. For example if we use Logic blocks 'True' or 'False', we can create additional events based on the condition if the first event is true or false. For example, if we nest a variable that is 'False' in the front left bumper event, it will trigger the next event that is associated with the condition 'False', and if we do the same for the front right bump sensor using 'True' , it will trigger an event associated with that condition.
Once we set the value of the variables and the conditions, we can start creating the sequences associated with the 'True' and 'False' events.
For Misty to be able to monitor all sensors at the same time and detect the ones you've selected for the condition, you will need to set your next bump sensor event to 'Any'. The next stage is to use the if-do or else-if-do function blocks and connect them with our variables and conditions. In the example below you can see that if the Front Left bumper is triggered, this tells Misty that the condition is 'False' and she has to trigger the corresponding sequence. Remember that you have to make sure that the variable 'Reaction' is equal to the condition you want to associate it with.
Using the same principle as in the previous example, we could also build a different type of 'skill tree' with the if-do-else block. Instead of creating a condition for each sequence, you can create only one condition, in this case 'True'. If you click the Front right bumper as in the example above, Misty will run the first sequence, if you click any other bumper, it will not match the condition and automatically runs the second sequence.
We suggest using LED lights for event feedback to let you know which condition you have triggered. This way you can easily follow visually where Misty is going.
Now that you've mastered how to use variables, let's discover the power of functions. If you every find that you want to re-use a particular group of sequences in your Blockly workspace, there is a neat solution that allows you to call that group and run it no matter where you are in your program. Functions only run when they are called, so you can easily modify them at any point. Now let's build your first set of functions!
To be able to call your sequence group you with you will need to nest it in a function block and give it a unique name.
Once you've written the name of your function, the call block with its name will appear in your 'Function' tab in Advanced blocks.
This block is used to call the function in your program. Try creating your won sequence group in a function and insert its call black in a new sequence to see what happens!
If you have a very small set of events you can write a block of code for each one of them, but if you want Misty to do many things, or write complex programs to diversify the interaction, you'll get tired of scrolling or looking for a specific block every time. That's why functions are very useful in creating multiple events. In the example below, you can see that the function 'Misty Waves' has been nested and called inside the face recognition event. Try building a new program where you call a function and use it in one of the event blocks.
Remember that you want to generalize it as much as possible, so to improve and compact your code even more, you can take the sequences associated with your 'Event messages' and simply group them with a function block.
Functions can also include variables. This makes them very versatile, in this way, your program will get all the pros of the variables and the pros of the functions. Here is an example of how to use a variable in a function. You can start programming cool skills with all the knowledge you have now. Just remember that you need to define your event message variable inside an event block.