8. Making magic

Making magic

In this exercise, we will control our mBot using only our hands. 

We will make it stop and even reverse.  Just like real wizards. 

You will learn:

  • How to stop your mBot by using the ultrasonic sensor (distance < 30)
  • How to make your mBot drive backwards by using the ultrasonic sensor (distance < 20)
  • Working with various if ...then ... else functions.

Open the mBlock programme on your computer, turn on your mBot and connect.


Check the steps in "execute". Answer the following questions:

  • Which steps should I follow?
  • Which steps can I take?
  • How much time do I need?
  • What tools do I need?


Perform the steps one by one. Have you forgotten how to do it? Look at the hints or download the solution.


Making the mBot drive

In the previous mBot exercise, you already learned how to make the sprite tell the distance of the ultrasonic sensor.  We will now also be using the distance to make the mBot do something. 
If the distance is shorter than 30, then our mBot must drive backwards. Otherwise, he must drive forwards.  Additionally, ensure that a light turns on every time. Which light is best for driving backwards or forwards?
The choice is yours. 

Do you already know which blocks you will need for this? 


Stopping the mBot.

Now, we simply have to make the mBot stop whenever we want it to.

  • Our mBot must stop if the distance is less than 30.
  • It must drive backwards if the distance is less than 20. 
  • Else, it must drive forwards. 

The additional condition is once again an “if... then... else”. Place it in the "if... then... else" condition you already created. .



Of course, we want to make our friends believe that we can enchant the mBot. 

Place the programme you just wrote onto the mBot. Do you remember how to do this?  Use the USB cable. 

If you place the programme onto the mBot, you will need to change a few blocks.  Also, add a block so that the programme will not start until the "on board button" is pressed. Which blocks will you need for this? 

The programme is now on your mBot. 

Reach out to some friends and surprise them with your magic skills. 

Have fun!


Look back at the project you just made. Ask yourself the following questions:

  • Does the programme work like I expected?
  • What is working well?
  • What is not working well?
  • How can I do things differently?


Click on the links below to download the solutions: