4tronix Bit:bot Makecode Starter Guide
4tronix Bit:bot Makecode Starter Guide
(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)
❏ 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)
❏ 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)
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