> For the complete documentation index, see [llms.txt](https://docs.kidscode.co.za/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.kidscode.co.za/microbit-beginners/lesson-4/step-counter.md).

# Step Counter

{% hint style="info" %}

## Teachers Notes

In this lesson, kids will learn how to assign a variable to micro:bit and make code for performing step calculation while the kids walk

* Assign a variable on the micro:bit
* On walking, micro:bit recognizes a shake and add steps
* When button A or B is pressed, micro:bit will display the number of steps recorded
* When A+B is pressed, the variable will be reset to 0
  {% endhint %}

## Create your new program

![](https://1854716099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LskXJjPT2bXMIodrtZq%2F-LssAbbgeYK7xwGZEYmg%2F-LssDwpr2bFRtkDFI9ol%2Fl2-1-2.png?alt=media\&token=ad9f2c70-87c3-41b9-b25e-42c3cfdfde34)

Click on "***New Project***". The MakeCode editor will appear

Delete the **forever** blocks by dragging it to the toolbar. Click on **Variables** option from the toolbar

![](https://1854716099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LskXJjPT2bXMIodrtZq%2F-Lu5vvqTsvrn-m1i6D4g%2F-Lu5wDpv2qSP2BS_OVpB%2Fc-2.png?alt=media\&token=10293ab2-ac0c-4167-aa23-f899fc739ac0)

Click **Make a Variable** and give it a name **"step"**

![](https://1854716099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LskXJjPT2bXMIodrtZq%2F-LuhzP8Anm9mT6c22H3L%2F-LuhzrBhPviuNCHf52gd%2Fstep-create-variable.png?alt=media\&token=2e027002-4b77-428d-b28c-30d62fd1c150)

Click **Ok**. The variable **step** will now be created

![](https://1854716099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LskXJjPT2bXMIodrtZq%2F-LuhzztGFIEpAu1gDpSb%2F-Lui5fH8oBsiQArHEJps%2Fstep-create-variable-complete.png?alt=media\&token=d64e37aa-9b2d-4792-90fc-6fbca0f14073)

Now you will have to initialize variables on start. Drag **set variable to 0 option** to the **on start** block

![](https://1854716099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LskXJjPT2bXMIodrtZq%2F-LuhzztGFIEpAu1gDpSb%2F-Lui5j2aqrXauaeQJLVN%2Fstep-initialize-variable.png?alt=media\&token=61e64f3e-05aa-464f-92dc-c9888f5f4f3f)

Now we will have to calculate number of steps based on walking pattern. When we walk with the micro:bit it shakes. We will use the **on shake** option. Drag **on shake** option from **Input** section

![](https://1854716099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LskXJjPT2bXMIodrtZq%2F-Lui5mI4tsNhBHbyio8l%2F-Lui6qTgV1bz_lMq3UH3%2Fstep-on-shake-drag.png?alt=media\&token=6dde1332-105a-4d4f-8b9f-4b8cbd57779f)

Now open **Variables** section and drag **change step by 1** option to the **on shake** block

![](https://1854716099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LskXJjPT2bXMIodrtZq%2F-Lui7FCVYNFGsZ6_B9HO%2F-Lui7O0UX-kWxjRFp0f_%2Fstep-onshake-change.png?alt=media\&token=a408743f-7671-4378-917a-aab420e006c7)

Now micro:bit will start calculating number of steps when you walk. We will have to display the number of steps calculated when you press button **A** or **B**. Drag **on button A is pressed** block from the **Input** section.&#x20;

![](https://1854716099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LskXJjPT2bXMIodrtZq%2F-Lui7FCVYNFGsZ6_B9HO%2F-Lui7yeDGuDgN7JhG7xY%2Fstep-drag-abutton.png?alt=media\&token=5049e0ad-148d-4121-9450-63f5e60fbd6e)

Now we will have to display the number of steps calculated. For this drag **show string** option from **Basic** section to the block. Drag the **step** from **Variables** section to the block. Perform the same action for **button B**

![](https://1854716099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LskXJjPT2bXMIodrtZq%2F-Lui85QlMEgdZ7mvT99M%2F-Lui8t1R9B9Pv27gNay3%2Fstep-display-buttona.png?alt=media\&token=5eda3066-0505-4e8d-b815-de4b0dcd37b1)

We now need to be able to reset the steps counted. To perform this action drag **on button A+B pressed** option from **Input** section

![](https://1854716099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LskXJjPT2bXMIodrtZq%2F-LuiCG7Rk4_QUNyFzwWy%2F-LuiCVVv2E5MEI4pACap%2Fstep-on-ab-press.png?alt=media\&token=2eb0e7ea-cabe-49c8-8c19-f6640935e2ce)

Drag the **set steps to 0** option from **Variables** section

![](https://1854716099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LskXJjPT2bXMIodrtZq%2F-LuiCG7Rk4_QUNyFzwWy%2F-LuiEPHr1PsztzWDdlo7%2Fstep-reset.png?alt=media\&token=b657014e-aacd-4679-adb2-91951059ad07)

## Save, download and run your code

Save you code as **Step\_Counter** and press save to download.

Drag your code file named microbit-1-2-3.hex to the MICROBIT(D:) folder to upload your code to your micro:bit.

Test your micro:bit by pressing button A or B. micro:bit will display zero. Walk around for a while and then press press Button A or B once, see if micro:bit displays number of steps that you have walked. Now press A+B together and then press button B to check if the steps have been initialized again.
