0% found this document useful (0 votes)
83 views5 pages

4tronix Bit:bot Makecode Starter Guide

This document is a tutorial for running a program on a Bitbot robot that uses an ultrasonic sensor. It teaches how to: 1. Read the ultrasonic sensor values into a variable called "distance". 2. Use an if/else block to check if an object is detected within 15cm, and spin the robot if so. 3. Otherwise, keep the robot moving forward if no close objects are detected. Loading this code onto the Bitbot causes it to drive forward and turn when objects are too close, avoiding obstacles using the ultrasonic sensor.

Uploaded by

Bobby
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views5 pages

4tronix Bit:bot Makecode Starter Guide

This document is a tutorial for running a program on a Bitbot robot that uses an ultrasonic sensor. It teaches how to: 1. Read the ultrasonic sensor values into a variable called "distance". 2. Use an if/else block to check if an object is detected within 15cm, and spin the robot if so. 3. Otherwise, keep the robot moving forward if no close objects are detected. Loading this code onto the Bitbot causes it to drive forward and turn when objects are too close, avoiding obstacles using the ultrasonic sensor.

Uploaded by

Bobby
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

4tronix makecode starter guide: Part 2 - Running a program on your Bitbot

(Ultrasonic sensor)

4tronix bit:bot makecode starter


guide
This is the second part of our tutorial, and assumes that you’ve already completed Part 1 - which shows
how to add the Bitbot extension to the makecode PXT editor, and load your code onto your micro:bit in
your Bitbot. This new code will run with the code from Part 1 already loaded. Instead of adding code to
the on start block, we’ll be adding to the forever block, so that it repeats endlessly. If you deleted the
forever block, you can find it by searching at the top of the command categories.

In this tutorial, you’ll learn how to:


1. Read the Ultrasonic sensor on the Bitbot
2. Control the motors to drive forward and turn
3. Avoid objects detected within a set distance of the Ultrasonic sensor

You can find the finished code for this project at ​https://makecode.microbit.org/_f85DqbX634wM

Page 1 of 5
4tronix makecode starter guide: Part 2 - Running a program on your Bitbot
(Ultrasonic sensor)

Read the Ultrasonic sensor into a variable


❏ From the command categories, find the ​Make a Variable​ button under ​Variables​.

❏ We’ll create a variable named ‘distance’ to read our Ultrasonic sensor values into

❏ Add the new ​set distance​ to block from Variables, to the forever block.
From the ​Bitbot > Sensors​ category, drag the ​read sonar as​ block into the ​set distance​ block.

Now the Ultrasonic sensor’s data will be repeatedly loaded into the ​distance​ variable.

Page 2 of 5
4tronix makecode starter guide: Part 2 - Running a program on your Bitbot
(Ultrasonic sensor)

Do something if the sensor detects an object


within a set distance
❏ Every time we read the sensor data into the distance variable, we will use an​ if..then..else​ block
to check whether an object has been detected within 15cm of the sensor. Drag this into the
forever block, from the ​Logic​ category.

❏ We’ll need to create a condition that tells our code when our robot is too close to an object.
Drag the comparison block from the ​Logic​ category, then drag the ​distance​ variable onto the left
circle. Edit the values so it looks like this.

❏ So if an object is too close, we need the robot to spin around, otherwise it can keep moving
forward. Once we drag those blocks from the ​Bitbot​ category, and add our above condition,
your code should look like this.

Page 3 of 5
4tronix makecode starter guide: Part 2 - Running a program on your Bitbot
(Ultrasonic sensor)

Load your code onto your Bitbot, and run it as you did in Part1 of this tutorial. The robot should
drive forward, and then turn when it detects an object in front of it.

Page 4 of 5
4tronix makecode starter guide: Part 2 - Running a program on your Bitbot
(Ultrasonic sensor)

The finished code


Combining the completed code with Part 1, looks like this:

Loading and running this code on your Bitbot will make it:
1. Turn the buzzer on for 100 milliseconds
2. Set all the LEDs to red
3. Move the Bitbot forwards, and avoid obstacles that are closer than 15cm

Page 5 of 5

You might also like