MYP Design · Year 8 · The robot car

Coding the TPBot

Eleven lessons for the TPBot robot car. Each one is a short program to build, run on the floor and then change. Open a lesson to see what you need, the steps and the blocks.

Getting the car ready

  1. Add the TPBot blocksIn MakeCode click Extensions, type tpbot and click the TPBot card. A teal drawer called TPBot joins the list.
  2. DownloadBuild your program and download it to the micro:bit, the same way as for your game.
  3. Slot it inUnplug the micro:bit and push it into the slot on top of the car until it sits firmly.
  4. Switch on, on the floorPut the car on the floor and press the power button once. Press it twice quickly to switch the car off.

Lessons

1Running controlMake the car drive: for a set time, on a button press, and round a curve.

You need

  • TPBot
  • micro:bit
  • A clear patch of floor

Build it

  1. Put Go Forward at speed 50 % for 2 seconds inside on start. Download it, slot the micro:bit into the car and switch the car on.
  2. Build the second program. Go Forward at speed 50 % starts the wheels and the car keeps going, so the program pauses for 2 seconds and then uses Stop the car immediately.
  3. Build the third program. Set left wheel speed at 50 % right wheel speed at 20 % gives each wheel its own speed.

The blocks

MakeCode blocks. Program A. Drive for 2 seconds as soon as the car starts.
Program A. Drive for 2 seconds as soon as the car starts.
MakeCode blocks. Program B. Drive for 2 seconds when you press A.
Program B. Drive for 2 seconds when you press A.
MakeCode blocks. Program C. Two different wheel speeds make a curve.
Program C. Two different wheel speeds make a curve.

What happens

A: the car drives forward for 2 seconds and stops. B: the same, each time you press A. C: the car curves to the right, because the left wheel is turning faster.

The idea. The car remembers its last order. It keeps doing it until a new block tells it something else.

Now change it

  • Change 50 % to 25 % in Program A. Measure how far the car goes each time.
  • Change Forward to Backward, Left and Right. Left and Right spin the car on the spot.
  • Swap the two wheel speeds in Program C and predict the curve before you run it.

ELECFREAKS Case 01

2Light controlChange the colour of the headlights with the buttons.

You need

  • TPBot
  • micro:bit

Build it

  1. Add three button events. In each one put Set headlight color to and click the colour to choose it.
  2. Put show icon in on start so you can see the program has started.
  3. Build the second program. Set headlight color to R: G: B: mixes a colour from red, green and blue, each from 0 to 255. Turn off the headlights switches them off.

The blocks

MakeCode blocks. Program A. A is red, B is blue, A and B together is green.
Program A. A is red, B is blue, A and B together is green.
MakeCode blocks. Program B. White for 2 seconds, then off.
Program B. White for 2 seconds, then off.

What happens

A: the headlights turn red, blue or green as you press. B: the headlights shine white for 2 seconds and go out.

Now change it

  • Mix your own colour with the R, G and B numbers. Try 255, 100, 0.
  • Make the headlights flash three times using a repeat block.
  • Make the headlights turn green while the car drives and red when it stops.

ELECFREAKS Case 02

3Line trackingThe car follows a black line using the two sensors underneath it.

You need

  • TPBot
  • micro:bit
  • The TPBot map, or black tape on a pale floor

Build it

  1. Put show icon in on start.
  2. In forever, add three if … then blocks. Drop Line sensor state is into each gap and choose a different pattern for each.
  3. Inside each if, add Set left wheel speed … right wheel speed … with the speeds shown.
  4. Place the car with the line between its two sensors and switch it on.

The blocks

MakeCode blocks. Black on both sides: drive straight. Black on one side only: slow that wheel to steer back.
Black on both sides: drive straight. Black on one side only: slow that wheel to steer back.
Block showsThe sensors see
● ●black under both sensors
● ◌black on the left, white on the right
◌ ●white on the left, black on the right
◌ ◌white under both sensors

What happens

The car drives along the line and steers itself back each time the line drifts to one side.

The idea. Stopping one wheel turns the car towards that side.

Now change it

  • Raise all three speeds by 10. Find the fastest speed that still holds the line.
  • Turn the headlights a different colour for each sensor pattern.
  • If the car hums and stays still, the batteries are low: raise the speeds until it moves.

ELECFREAKS Case 03

4Obstacle-avoidance drivingThe car measures the distance ahead and turns away from anything close.

You need

  • TPBot
  • micro:bit
  • A box or a wall

Build it

  1. Make a variable called distance.
  2. In on start: show an icon, set the headlights green and Go Forward at speed 40 %.
  3. In forever, set distance to Sonar distance unit cm. The two round eyes on the front are the sonar.
  4. Add if … then with the condition distance < 15 and distance ≠ 0. Build it from and with a comparison on each side. The sonar sends 0 when it has no reading, so the second half skips those.
  5. Inside the if: stop, headlights red, reverse for 1 second, spin left for half a second, headlights green, drive forward again.

The blocks

MakeCode blocks. Drive until something is closer than 15 cm, then back away and turn.
Drive until something is closer than 15 cm, then back away and turn.

What happens

The car drives with green headlights. Close to a box it stops, turns its lights red, reverses, turns left and drives on.

The idea. This is drive until: the car stops because of what a sensor measures.

Now change it

  • Change 15 to 30 and watch how much earlier the car reacts.
  • Change Left to Right.
  • Show the distance on the micro:bit with show number.

ELECFREAKS Case 04

5Automatic lampThe headlights switch on by themselves when it gets dark.

You need

  • TPBot
  • micro:bit
  • Your hand, or a tunnel made from a box

Build it

  1. In on start, put Go Forward at speed 30 %.
  2. In forever, add if … then … else. The condition is light level < 30. Light level runs from 0 for dark to 255 for bright.
  3. In the then gap set the headlights white. In the else gap put Turn off the headlights.

The blocks

MakeCode blocks. Dark: headlights on. Bright: headlights off.
Dark: headlights on. Bright: headlights off.

What happens

The car drives forward. Cover the micro:bit's screen with your hand, or drive into the tunnel, and the headlights come on.

The idea. The micro:bit measures light with its own LED screen.

Now change it

  • Show the light level on the micro:bit to find the number for your classroom.
  • Change 30 so the lights come on in the tunnel and go off outside it.
  • Choose a different headlight colour for the dark.

ELECFREAKS Case 05

6Drive at randomEach wheel gets a random speed, and the car makes up its own dance.

You need

  • TPBot
  • micro:bit
  • A wide clear patch of floor

Build it

  1. Make two variables, left and right.
  2. In forever, set each one to pick random -100 to 100.
  3. Add Set left wheel speed … right wheel speed … and drag the two variables into its gaps, then pause (ms) 2000.
  4. To finish, press the car's power button twice quickly.

The blocks

MakeCode blocks. New random speeds for both wheels every 2 seconds.
New random speeds for both wheels every 2 seconds.

What happens

Every 2 seconds the car changes what it is doing: forward, backward, a curve or a spin.

The idea. A negative speed turns the wheel backwards.

Now change it

  • Change the pause to 500 for a jumpier car.
  • Change the range to 0 to 100 so the car only goes forwards.
  • Give the headlights a random colour on each change, with three more random numbers from 0 to 255.

ELECFREAKS Case 06

7Here comes the policeA police car: driving forward with red and blue flashing lights.

You need

  • TPBot
  • micro:bit

Build it

  1. In on start: show an icon and Go Forward at speed 50 %.
  2. In forever: headlights red, pause (ms) 100, headlights blue, pause (ms) 100.

The blocks

MakeCode blocks. The wheels are started once. The lights change inside forever.
The wheels are started once. The lights change inside forever.

What happens

The car drives forward while its headlights flash red and blue.

The idea. The driving block is in on start because the car remembers it. Only the lights need repeating.

Now change it

  • Change both pauses to 300 for a slower flash.
  • Make the car drive for 3 seconds and stop while the lights carry on flashing.
  • Design the lights for an ambulance or a recovery truck.

ELECFREAKS Case 07

8Parking at a set pointThe car drives forward and parks itself on a black line.

You need

  • TPBot
  • micro:bit
  • A strip of black tape across the car's path

Build it

  1. In on start: show an icon and Go Forward at speed 30 %.
  2. In forever, add if … then with Line sensor state is ● ●. That pattern means black under both sensors.
  3. Inside the if, put Stop the car immediately.
  4. Start the car about a metre back from the tape, pointing straight at it.

The blocks

MakeCode blocks. Drive until both sensors see black.
Drive until both sensors see black.
Block showsThe sensors see
● ●black under both sensors
● ◌black on the left, white on the right
◌ ●white on the left, black on the right
◌ ◌white under both sensors

What happens

The car drives forward and stops on the tape.

The idea. Drive until again, this time with the line sensors.

Now change it

  • Raise the speed to 60 and measure how far past the tape the car rolls.
  • Turn the headlights green while driving and red once parked.
  • Make the car wait 2 seconds on the tape and then drive on to a second strip.

ELECFREAKS Case 08

9Seeking lightThe car spins until it finds a bright light, then drives towards it.

You need

  • TPBot
  • micro:bit
  • A torch, or a phone torch

Build it

  1. In on start, set the headlights white.
  2. In forever, add if … then … else with the condition light level > 40.
  3. In the then gap put Go Forward at speed 50 %. In the else gap put Go Left at speed 30 %, which spins the car on the spot.
  4. Put the car on the floor and shine the torch at the micro:bit's screen.

The blocks

MakeCode blocks. Bright: drive forward. Dim: spin and look for the light.
Bright: drive forward. Dim: spin and look for the light.

What happens

The car spins on the spot. When the torch shines on the micro:bit it drives forward, and it spins again when the light moves away.

Now change it

  • Show the light level on the micro:bit to find the number that suits your room.
  • Change 40 so that only the torch sets the car driving.
  • Lead the car round a chair with the torch.

ELECFREAKS Case 09

10Fall-arrest TPBotThe car stays inside a border of black tape.

You need

  • TPBot
  • micro:bit
  • Black tape laid as a large box on a pale floor

Build it

  1. In on start: show an icon and set the headlights white.
  2. In forever, add if … then … else. Build the condition from not with Line sensor state is ◌ ◌ inside it. It reads: the sensors are not both on white.
  3. In the then gap: reverse at 30 % for 1 second, then spin left at 30 % for half a second.
  4. In the else gap: Go Forward at speed 30 %.
  5. Place the car inside the tape box and switch it on.

The blocks

MakeCode blocks. Any black under the car: back away and turn. All white: drive on.
Any black under the car: back away and turn. All white: drive on.
Block showsThe sensors see
● ●black under both sensors
● ◌black on the left, white on the right
◌ ●white on the left, black on the right
◌ ◌white under both sensors

What happens

The car wanders inside the box. Each time a sensor reaches the tape it reverses, turns and sets off in a new direction.

The idea. ELECFREAKS call this fall-arrest because tape along a table edge keeps the car on the table. A tape box on the floor shows the same idea.

Now change it

  • Change the half second of turning to a random time between 0.3 and 1 second.
  • Flash the headlights red each time the car reaches the tape.
  • Count the number of times the car reaches the tape and show the count on the micro:bit.

ELECFREAKS Case 10

11Following with a fixed distanceThe car keeps a steady gap behind whatever is in front of it.

You need

  • TPBot
  • micro:bit
  • A book to hold in front of the car, or a second car

Build it

  1. Make a variable called distance.
  2. In forever, set distance to Sonar distance unit cm.
  3. Add if … then … else and press its + to make an else if. First condition: distance > 10 and distance < 20. Second condition: distance < 10.
  4. Set both wheels to 0 in the first gap, to -30 in the second and to 30 in the else gap.
  5. Hold the book about 15 cm in front of the car, then move it slowly away and slowly back.

The blocks

MakeCode blocks. Between 10 and 20 cm: stay. Closer: back away. Further: catch up.
Between 10 and 20 cm: stay. Closer: back away. Further: catch up.

What happens

The car follows the book forwards and backs away when the book comes close, keeping a gap of 10 to 20 cm.

The idea. Three answers to one measurement: too close, just right, too far.

Now change it

  • Change the gap to between 20 and 30 cm.
  • Use a headlight colour for each of the three answers.
  • Line up two cars, one following the other, with a person leading the front car.

ELECFREAKS Case 11

These eleven lessons follow Cases 01 to 11 of the TPBot guide by ELECFREAKS, who make the car. Each lesson links to its original case. The programs here were rebuilt with the current TPBot blocks and the instructions written for this class. Block pictures are drawn by Microsoft MakeCode.