> 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

![](/files/-LssDwpr2bFRtkDFI9ol)

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

![](/files/-Lu5wDpv2qSP2BS_OVpB)

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

![](/files/-LuhzrBhPviuNCHf52gd)

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

![](/files/-Lui5fH8oBsiQArHEJps)

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

![](/files/-Lui5j2aqrXauaeQJLVN)

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

![](/files/-Lui6qTgV1bz_lMq3UH3)

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

![](/files/-Lui7O0UX-kWxjRFp0f_)

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;

![](/files/-Lui7yeDGuDgN7JhG7xY)

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**

![](/files/-Lui8t1R9B9Pv27gNay3)

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

![](/files/-LuiCVVv2E5MEI4pACap)

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

![](/files/-LuiEPHr1PsztzWDdlo7)

## 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.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.kidscode.co.za/microbit-beginners/lesson-4/step-counter.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
