Code to Note
Let’s take a look at the code and what to expect.
On [Accelerometer]
The micro:bit has a built in accelerometer that measures the gravitational forces that are acting upon it. With other
microcontrollers this is sometimes hard to use and make sense of the data you receive from the sensor. But,
MakeCode has simplified that for you in an event block under the Input drawer (you will see it as
On Shake
) that
allows you to pick a number of different force levels, orientations and even patterns (like shaking it). We use this to
detect a number of orientations that you can put your robot in. For example the motors will only be
ON
in the
screen up orientation.
If you tap the robot hard enough it will run / drive away from you. If you flip it over the motors will turn off. Same
thing holds true if you place it on its tail with the micro:bit pointing up.
Try playing around with the different events that you can trigger with the accelerometer. Can you get it to detect
bumping into an object like the wall and have it respond???
What You Should See
Once your code is loaded to your micro:bit and your motor switch is set to "RUN MOTORS." Your robot should not
do anything at first. Give it a good tap / smack on top of the micro:bit. If you apply enough force to it your robot
should run away. To stop your robot either flip the robot over to its back the motors should stop, or pick it up and
onGesture(Gesture.LogoUp, () => {
tobit.enable(MotorPower.Off)