Programming your Robot Friend

Connect your robot to a computer

The micro:bit slides right into the Adafruit Crickit just like you see in the photo. You will want to use a USB cable to connect the micro:bit to a computer.

Do not use the Micro USB port on the Adafruit Crickit to program your robot with MakeCode.

Once your code is downloaded, your robot will do it's thing.

Here is a link to some more info if you get stuck.

https://microbit.org/guide/quick/

The USB cable can also be used to display information using the Serial monitor.

Basic Robot Movement

This robot moves using the two motors attached to the wheels. For this, we are using the Crickit tank motors command to tell both motors to move at the same time. To make things easier, there are some functions made that will handle the movement.

These functions use a parameter to set the number of milliseconds that you want your robot to do that particular function.

Each movement function will use tank motors to turn the wheels based on the value set by the speed variable.

Once the motors are driving, there is a pause command that uses the value that was set in the function parameter. This is measured in milliseconds, so remember that 1000 milliseconds is equal to 1 second.

Afre the pause, the robot will use the call stop function to stop the robot form moving until the next command.

So how do I make it move?

These functions make it super easy for you to program your robots movements. 

First you will want to set a value of 0 to 100 for the speed and place that in the on start block. I also added a happy face, but that part is optional.

First you will want to set a value of 0 to 100 for the speed and place that in the on start block. I also added a happy face, but that part is optional.

Next you want to click Advanced in the blocks list and click Functions.

From here you can just drag out any of the functions that you want to use.

For my code, the robot will drive forward for 3 seconds, turn left for a half of a second, turn right for one second, then drive backwards for 2 seconds before stopping.

This will only happen one time when the robot is powered on or the reset button is pressed. If you want this to happen over and over again, move your function calls to the forever block.

But I want my robot to drive in inches

Your robot does not know what an inch is unless you teach it. This is a good chance for you to work with your robot friend and learn something new. If you have programmed a robot before and it let you tell it to drive for a certain distance, that is because a person showed the robot how to calculate that distance based on the diameter of the wheels, the speed of the motor, and the time that the motor spent moving.

It is a rather simple calculation and I am sure that you can find some info with a little research on the internet. Also keep in mind that while it is good to do the math for things like this, things in the real world are subject to all kinds of things that are very tricky to predict. When it comes to the movement of a robots wheels, things like traction, slippage, and even the exact angle of the wheels can account for small changes in how far a robot actually travels. Advanced robots use a bunch of sensors to account for this because better motors and wheels will only get you so far.

The Sonar Sensor

Your new robot friend can see but not like you and me. A sonar sensor used sound like bats and dolphins do for echo location. Basically the sensor sends out a sound called a Ping that we can not hear. That Ping will bounce off of most solid objects and the sensor will listen for the echo from that Ping. Since we know how fast sound can travel, just a little math can tell us how far away something is based on the time it took for our Ping to echo back to the sensor. You just take half of that value and you know about how far the sensor is from an object.

Thankfully someone has made a special block that we can use to read the distance from our sonar sensor and assign it to a variable so that we can use some logic to help our robot not bump into so many things when it drives.

Click on Sonar from the blocks list.

There is only one block here and it will need to be placed into a block that accepts a value like a variable.

You will want to put this Sonar block into a set variable to block because you may want to use that sensor reading a few times.

Set the ping trig to P8

Then set echo to P2

The unit can be set however you like.

I made a variable named ping and it will hold the distance in centimeters that my robot is away for any solid object in front of the sonar sensor.

I also added a serial write value block so that you can see the data from the sonar sensor plotted out using the Serial monitor... because data can be useful.

From there you just need to use logic to tell the robot what to do when it gets close to an object.

In my code, the robot will drive forward until the sonar sensor can detects something less than 10cm in front of it. If it detects an object less than 10cm in front, the robot will drive backwards for a half of a second. The robot will then pick a random number to decide what direction it will turn in and then turn right or left. If there is nothing in the way of the robot, it will drive forward until it sees another object less than 10cm in front.

Experiment Time!

The code example that I gave you is a good start, but you may notice that it does not work perfectly. Like the wheels, object detection can be a tricky thing. Some objects absorb sound rather than reflect it and may not give back an echo. Other objects may just bounce sound in another direction so that the echo never gets back to your sensor. Even the sensor itself only has a small range of space where it can accurately detect objects.

I challenge you to look at the data from the Serial monitor while your robot is connected to your computer. See if you can figure out the blind spots for your robot and even test to see what objects may be invisible to it.

This guide was first published on Sep 26, 2019. It was last updated on Sep 26, 2019. This page (Programming your Robot Friend) was last updated on Dec 11, 2019.