Page 154 - KEC Khaitan C5 Flipbook
P. 154
USING VARIABLES FOR STEP COUNTING
A variable is a storage location for data that can change over time. In step counting
project, you will use a variable to store the number of steps taken. Each time movement is
detected, the variable will increase by one.
IMPLEMENTING STEP COUNTING
The steps to implement step counting using different variables are as follows:
Step 1 Click on New Project in MakeCode to
create a new Micro:bit project.
Step 2 Click on Input and select the on shake
block.
Step 3 Click on Variables, then Make a Variable,
name it stepCount, and set it to 0.
Step 4 Click on Variables and drag the change stepCount by 1 block inside on shake.
Step 5 Click on Basic, drag show number block, and place stepCount inside it.
let stepCount = 0
input.onGesture(Gesture.Shake,
function () {
stepCount += 1
basic.showNumber(stepCount)
})
DESIGNING LOGIC FOR STEP DETECTION AND DISPLAY
To improve accuracy, we need to refine our step detection logic. Instead of detecting simple
shakes, we can use the accelerometer’s acceleration function to determine whether a step
has been taken based on threshold values.
ADDING A STEP GOAL TRACKER
The steps to add step goal are as follows:
1. Create a variable called stepGoal and set it to a target value (e.g., 1000 steps).
2. Compare stepCount with stepGoal.
3. Display a congratulatory message when the goal is reached.
let stepCount = 0
152 Premium Edition-V

