> 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
