So “my blocks” are the cute pink code blocks in VEXCode IQ that you can name whatever you like. And yes they are also those blocks that you just keep skipping while you scroll to find the drive train, your motor functions, your events and so on. But if you aren’t using them you don’t know how helpful and necessary they are to the autonomous code.
Let me start by explaining what they are with a real world scenario. Let’s say I want my kids to always empty the dishwasher. And when I say “Girls, empty the dishwasher please” they will first say “oh nooooo!”, “can I do it later?”, “but didn’t we empty this like 10 minutes ago?” (it was 3 days ago!) but their laziness might take them to the extreme of asking “What does it mean to empty the dishwasher?” to which I might have the reply every single time by saying: “It means you open that machine called the dishwasher and remove all the plates, forks, knives, glasses that have been washed and put them where they belong so that the machine is empty for us to use it again”. But I don’t want to explain all this every time so I create a “My Block” called “Empty the dishwasher” and I place the instructions in it. Like this:
Yes, I know the gray lines are comments but for our example’s sake, let’s say they are tasks. So with this block written, I can “push the empty the dishwasher” button and kids can’t ask me what I mean anymore. They can look up the “empty the dishwasher” “my block” and find all the instructions. But let’s take this a step further, I want both of them to do the job and I don’t want them fighting about who did what so I set up a “variable” in “my block”. Variables when used in “my blocks” are called “parameters”. A variable is a piece of code where you can store information to reuse later. Like a plate to put food in. So I need two variables, one to define the person I want to assign the task to and one to define which task to do. …Yes, I’m that lazy, I don’t want to say these things I just want to push my button… So I could define my block like so.
So in this “my block” I’m taking care of every possible directive. To put it down into words, this means whenever I call this “my block”, if the person I picked is Lara and the area I assigned her is the top then she would open the dishwasher, empty the top shelf, close the machine, tell us she is done and party. If the person I picked is Mila and the area I assigned her is the bottom it would go to the “else” section of our first if and it would go to the else section of the if in there and tell Mila to remove the plates and silverware on the bottom, put them away, close the machine, tell us she is done and party. And for this I would need to push these two buttons:
And they should know what to do and my dishwasher should be empty after this with no fights about who does what.
But wait one day I pushed these 2 buttons by accident and even though the machine was full of dirty dishes my kids did not realize that and they put all the yucky dirty dishes away!!!! How can I take care of this? I can create a “Boolean” variable and no “booooo-lean” variables are not the scary variables, Boolean means parameters (or variables) that can only carry the value of true or false (so it can’t be a word or a number but it can only be true or false). So I would upgrade “my block” like this:
So if I push the button by accident when everything is dirty, kids would read the directives, follow the tasks relating to emptying the machine ONLY IF TheyAreClean is true and if it is false they would go into the “else” section of the block and they would “not empty the machine” and “Say “mummy!, you pushed the button by accident again, dishes are clean!”
So there you go, now I have a “button” to push to get my dishwasher empty whenever needed….? yeah, right… They will complain, refuse or delay until they are old enough to code their own buttons and assign it to their kids.
I will go on to describe a “my block” that is more applicable to our VEX Robot in my next article.