> 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/calculator.md).

# 1 + 1 = 2

{% hint style="info" %}

## Teachers Notes

In this lesson, the kids will learn how to assign variable to each button and perform an addition function.&#x20;

* Assign a variable to button A
* Assign a variable to button B
* Value for each variable will be assigned based on number of taps on the particular button
* When A+B is pressed, the LED's will show the sum of values of variables A and B
* Shaking the micro:bit will reset the variables
  {% endhint %}

## Create your new program

Click on the ***micro:bit*** icon on your toolbar or go to <https://makecode.microbit.org/> on your browser.

![](/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 **"A"**

![](/files/-Lu5wZF9QYUTcbx4qwfx)

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

![](/files/-Lu5wol6hCBf1RujiDgZ)

Follow the same steps again to create variable "B"

![](/files/-Lu5x1OZcc81GAMXy9wt)

Now you will have to initialize variables on start. Drag **set variable to 0 option** to the **on start** block. Select the dropdown and select variable A. Similarly pefrorm the same step for variable B.&#x20;

![](/files/-Lu5xdO7csKse_ip701e)

Now we have initialized the variable. We will have to now make code for the variable to assign values based on number of taps we perform on a button. If we press button A the variable A should increase value by 1

To perform this function, drag **on button A pressed** option from **Input** toolbar. From the **Variables** toolbar, drag **change A by** option and change the value to **1**.

![](/files/-Lu5z7fnhZGca7Dx8hVM)

Repeat the same step for button B

![](/files/-Lu6-CiiAqZ0ii4kbGuT)

Now we must perform the addition function when button A and B are pressed together. Drag the **on button A+B pressed** option from the **Input** toolbar

Drag show string option from the Basic Toolbar

![](/files/-Lu6-vDiwSby9EuoONiP)

Now we will have to perform a mathematical calculation of the variables. Open the **Math** toolbar and drag **0 + 0 option** to the code editor

![](/files/-Lu60WAk3dQzHGXypyeL)

Now replace the zeros with variables A and B by dragging **A** and **B** from the **Variables** toolbar

![](/files/-Lu6130J3InO_OCpFrev)

Now you must be able to reset the calculator after addition function is completed. We can reset the variables as we shake the micro:bit. To perform this function drag **on shake** option from **Input** toolbar. Add **set A to 0** and **set B to 0** options to on shake from **Variables** toolbar

![](/files/-Lu61jdlAdObe-ZzGpQ9)

## Save, download and run your code

Save you code as **Calculator** 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 once, then Button B once then both button A and button B at the same time. Your micro:bit will add 1 + 1 and display value 2


---

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