Programming the Tinker Pi Robot with Microbit #tt18

Tinkerer: Lory Livezey

This tutorial builds on the 4 previous tutorials, where we learned how to use the accelerometer, radio and the Kitronik Motor Board, and assembled the Tinker Pi Robot. Now, it's time to get our robot up and running!

Previous Step

  Assembling the Tinker Pi Robot for Microbit #tt13

To complete this tutorial, you should follow the previous steps or be familiar with the following:

  • The basics the Make Code Web Site
  • How to use the Accelerometer and Radio on the Micro:bit
  • How to upload a program (hex) to the Micro:bit
  • How to install a package into the Make Code web site (specifically the Kitronik Motor Board)
  • How to hook up and program the Kitronik Motor Board
  • Have an assembled Tinker Pi Robot for Microbit

If you are up to speed, you can simply load this breakpoint: Unzip this and load it onto your Microbits

What you will need

Materials List

Step 1 - Log Into MakeCode for Micro:bit

Browse to the Make Code Web Site

If you are continuing from the previous step, you'll have the two projects we're going to need. Otherwise, use the import option to upload the hex files above.

To Import:

Projects > Import File > Browse to the hex file

Import Button

You should have both starter projects, one for the remote and one for the robot:

Both Projects

Step 2 - Program the Remote Micro:bit

Open the Tinker Pi Robot Remote project. This is what your blocks should look like to start:

Blocks at Start

First, duplicate the radio send block.

Right click on block > Duplicate

Duplicate the Radio Send Block

Change the x values to y, and then add a pause block after each one:

Basic > Pause (ms) 100

Your blocks should look like this:

Checkpoint 1

  • Save and Download the Hex and save it to your Remote Microbit.

Step 3 - Program the Robot Microbit's On Radio Received Event

Next, open the Tinker Pi Robot project. This is how we left it in the last tutorial:

Robot Starting Blocks

If your blocks don't look like this, either create them, or upload the hex provided above.

Right now, we are only detecting if the value y=__ is coming from the remote. But we will also need to get the x=__ axis. To do this, add an else if statement to the if block by clicking on the + as shown below:

Add an Else Block

Duplicate the block set y to value and drag it into your else block:

Duplicate Set Y to Create Set X

Tinker Thinker #1

What do you think will happen if the value passed in from the remote is z?

(answer at bottom)

To help with this we could add a block that will run if that happens. In programming, this is called error handling. Error handling allows us to identify problems with our code when something goes wrong. If you see the pitchfork, you will know that a value other than x or y was passed from the remote:

Error Handling

We are done with the on radio received block. Let's move on to the forever loop.

Step 4 - Program the Robot Microbit's Forever Loop

Our Forever loop currently sets Motor 1 to the value of the y axis divided by 10. This is because the motors accept values of 1 through 100 for their speed. The accelerometer will give us values between 0 and 1024.

Tinker Thinker #2

What would happen if we did not divide the numbers coming from the remote by 10?

(answer at bottom)

So, we need to divide the accelerometer values by 10 so that we can use them directly. Your blocks should look like this:

Error Handling

If they don't, you can find divided by here:

Math > drag 0 / 0 block > set to value / 10

The next problem to tackle is this -- how do we determine how fast our wheels should go in order to turn the robot from the rotation (accelerometer) of the Microbit? Formulas allow us to make calculations that would otherwise be very difficult or impossible. We're going to use a fairly simple formula now, then in the next tutorial we're going to use a more advanced formula, one that will give us better control of our robot.

The formula we'll use is:

leftWheel = (y + x) / 10
rightWheel = (y - x) / 10

When moving in reverse, we'll need to switch the motors (wheels), and get rid of the -, since the speed is always a positive number. To do this, we'll use the absolute function, which returns the positive value of the number:

rightWheel = absolute((y + x) / 10)
leftWheel = absolute((y - x) / 10)

Tinker Thinker #3 (Extra Credit)

How far will your robot go if the temperature is absolute zero?

(answer at bottom)

We will use these values to calculate the speed and direction of the motors. But we will need variables to hold these values. Let's create two variables called leftMotor and rightMotor:

Variables > Make a Variable > leftMotor
Variables > Make a Variable > rightMotor

Create Variables

  • Create a block, set leftMotor to x + y / 10 (for the location, look at the diagram above. And remember, the + and / blocks are under Math)

  • Duplicate the block you just created and change leftMotor to rightMotor and set the formula to y - x / 10.

Your blocks should look like this:

Create Variables

Note: We are using a simple formula to make this project as simple as possible. It isn't perfect, but we can improve on it later. This is a core principle in programming called Iterative Development, which means get 'er done and improve later!

Understanding the Formulas:

You don't need to understand the formulas to make your robot work. But understanding them will enable you to make improvements to the design. So, in order to understand this better, let's look at a couple examples:

Example #1 - Full Speed Ahead


In the first example, we would have the Microbit tilted all the way forward. Using our formula, you can see that both wheels would be set to 100, since that is the maximum speed:

Example 1 - Full Speed

Example #2 - Reverse and Slightly Left


In this example, we have the Microbit tilted back (toward you), and slightly to the left. The coordinates come out to x=-300 and y=-750. Use the reverse formula this time:

Example 2 - Reverse and Slightly left

Because we need to switch the formula to go in reverse, the right wheel will bet set to -110 and the left wheel will be set to -40.

Example #3 - Hard Left Ahead


In this example, we're making a sharp left turn with the remote. Notice that we're adding two negative numbers, which is the same as adding them. Confusing, since two wrongs don't make a right...right??

Think of it like this. Let's say I loan you a banana per day from my lunch for 3 days. You really have -3 bananas. But if I feel generous and subtract 3 bananas from the number that you owe me. How many do you have now? Right! No bananas! Enough with the monkey business... let's continue.

Example 3 - Sharp left turn

In this case, the left wheel would not move, and the right wheel would be turning at 100, since that is the maximum.

I am going to give you pseudo-code, that you can translate into blocks. Pseudo-code is a way of writing in plain language that describes what the program should do. See if you can do this without looking at the final blocks below.

Set the leftWheel to y + x, then divide that number by 10 
Set the rightWheel to y - x, then divide that number by 10

If the leftWheel is greater than 0, then set motor 2 to the forward speed of leftWheel
Else set motor 1 to the reverse speed of leftWheel (if leftWheel is negative, make it positive)

If the rightWheel is greater than 0, then set motor 1 to the forward speed of rightWheel
Else set motor 2 to the reverse speed of rightWheel (if rightWheel is negative, make it positive)

  • Once you convert the pseudo-code into blocks, your blocks should look something like this:

Forever Blocks

Tinker Thinker #4

How many right ways are there to program?

(answer at bottom)

  • Upload the code to the Micro:bit

The final code can be found here. Unzip and load it onto your Microbits: Final Hex

Step 5 - Run Your Robot

As soon as you power up both Micro:bits, your robot should start moving!

In the next tutorial, we are going to dive in a little deeper and create a formula for determining the speed and direction of both motors, which will make the robot easier to handle.

Answers to the Tinker Thinkers

Tinker Thinker #1: If you pass something other than x or y to the robot, nothing will happen. That's why error handling is always a good idea.

Tinker Thinker #2: If you don't divide the numbers coming from the remote by 10, your robot will go full speed almost all the time. The remote will send values from 0-1024, and 100 is full speed for the motors. So, by tilting the remote only 10%, you've reached full speed.

Tinker Thinker #3: At absolute zero, your robot will go absolutely nowhere. This is because absolute zero is negative 459.67 degrees Fahrenheit.

Tinker Thinker #4: There are many, many right ways to program, and many, many wrong ways. This is what makes programming both fun and challenging. Good programming takes both technical ability and creativity. Always look to find better, more creative ways to program!

Next Up

Be sure to subscribe to our channel!