Today you are coding with the micro:bit. You will build a wearable step counter you can walk with, watch the count on the LEDs, and check how accurate it is.
You already know how to open a MakeCode project and send code to a micro:bit. We will predict, build step by step, run it on the board, investigate the count, modify it, and test again.
What do you think the micro:bit can sense so it knows you took a step?
Keep this short. Devices ready, students logged in, MakeCode for micro:bit to hand. Optional predict before anyone runs code: ask what the micro:bit might sense to detect a step (shake or movement). Do not confirm yet; park answers for make sense. Model only the opening beat on the board, then pairs work. No typed student response here.
Go to the makecode.microbit.org website and create a new project.
Model opening makecode.microbit.org and starting a new project on the board. Watch for students editing an old project by mistake. Differentiation: stay with anyone stuck on login or browser tabs before the class moves on.
We will need to remember the amount of steps we've taken so we will create a variable called 'steps'.
Key question: why do we need a named place in memory for the step total? Common misconception: thinking the LEDs alone remember the number. Cue pairs to use a clear name such as steps. Support: write the variable name on the board.
When we start the 'steps' should be 0. Add the following code:
let steps = 0
Model setting the starting value to 0 on start. Bug to watch: forgetting this so the count begins at a random or blank value. Ask: what should the counter show before anyone walks?
Our Microbits have a sensor in them called an accelerometer and this can detect when the Microbit has been shaken. We're going to use this to detect the 'shake' of our steps and each time we detect it, we will add 1 to our 'steps' variable so we can record the number of steps we take.
Add the following code:
input.onGesture(Gesture.Shake, function () {
steps += 1
})
Briefly name the accelerometer as the sensor behind shake. Model the on-shake handler adding to the variable. Misconception: expecting a step on every foot without testing. Differentiation: if a pair cannot find the gesture block, point to Input rather than rewriting instructions.
You're previewing this lesson. Get full access to this lesson and hundreds more — each one ready to teach, with interactive activities, printable resources and pupil progress tracking built in.