Defining Blocks

When you have a long line of coding blocks, it can be quite difficult to see what is happening where.

This is especially tricky when something is not working correctly and you need to "debug" and fix it.

Here is my original coding for making the mBot follow the black line.

It is not that long but there is still a few elements that need to be correct and working together in the right order to make it work.

A good way of making your coding easier to understand and easier to "debug" and fix if something is not correct is to chunk it and use your own defined blocks.

Defining a block

When you click on the pink My Blocks down at the bottom of the menu, you get the option to make a block.

Give your block a name - make it the thing you want it to do - in this example I am calling it "Move Forward"

You then get a block in the menu with that name and a "define" block on the coding screen.

Under the "define" block, you put all the coding that is needed for the specific task you want that block to do. This is a really easy example so it just has the one movement block used.

When you code, you use the "move forward" block that you defined.

Using defined blocks for the line follower code

My code for the line follower now looks like this.

It has 4 specifically defined blocks

   * Black line

   * Off to the left

   * Off to the right

   * On the white

The defined blocks of code for these look like this.

When the mBot runs the main code, it gets to a defined block and looks to where it has been defined. It then runs the bit of defined code for that block.

This method makes it easier for you to code in sections and then put all the sections together.

This also makes it easier to make sure that all the seperate parts are working. It is easier to see what is wrong if your coding does not work.