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

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

![](https://1854716099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LskXJjPT2bXMIodrtZq%2F-Lu5vvqTsvrn-m1i6D4g%2F-Lu5wZF9QYUTcbx4qwfx%2Fc-4.png?alt=media\&token=7a2d0456-b2a2-423b-9cb2-0d3f2f5c245c)

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

![](https://1854716099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LskXJjPT2bXMIodrtZq%2F-Lu5vvqTsvrn-m1i6D4g%2F-Lu5wol6hCBf1RujiDgZ%2Fc-5.png?alt=media\&token=76809894-ad70-4ffd-bddb-3744b7ef33a7)

Follow the same steps again to create variable "B"

![](https://1854716099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LskXJjPT2bXMIodrtZq%2F-Lu5vvqTsvrn-m1i6D4g%2F-Lu5x1OZcc81GAMXy9wt%2Fc-6.png?alt=media\&token=dc930763-af67-49c3-96b7-288aed002fd7)

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;

![](https://1854716099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LskXJjPT2bXMIodrtZq%2F-Lu5xHlQ-UH6-E-61EoW%2F-Lu5xdO7csKse_ip701e%2Fc-7.png?alt=media\&token=29410fc5-653c-45e5-aa6c-5542267fdb93)

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

![](https://1854716099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LskXJjPT2bXMIodrtZq%2F-Lu5yyGUMvDVLBqLVPcl%2F-Lu5z7fnhZGca7Dx8hVM%2Fc-8.png?alt=media\&token=614b6829-04b7-47ee-980f-1a6eba2f444c)

Repeat the same step for button B

![](https://1854716099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LskXJjPT2bXMIodrtZq%2F-Lu5zAvV37sraceRZ3Aw%2F-Lu6-CiiAqZ0ii4kbGuT%2Fc-10.png?alt=media\&token=400a89fd-a04c-4120-a1d1-6187c99dd62c)

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

![](https://1854716099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LskXJjPT2bXMIodrtZq%2F-Lu5zAvV37sraceRZ3Aw%2F-Lu6-vDiwSby9EuoONiP%2Fc-11.png?alt=media\&token=c9c5aea9-6732-4f82-87e8-681f851a6fe1)

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

![](https://1854716099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LskXJjPT2bXMIodrtZq%2F-Lu60JAfS9RQdr4DIW2k%2F-Lu60WAk3dQzHGXypyeL%2Fc-12.png?alt=media\&token=4937e938-c247-4526-bb66-20731bc7a7f5)

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

![](https://1854716099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LskXJjPT2bXMIodrtZq%2F-Lu60Yr7V_iOzqZ-JEnR%2F-Lu6130J3InO_OCpFrev%2Fc-13.png?alt=media\&token=16dfce11-b124-42d4-99f2-ebde21e47d21)

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

![](https://1854716099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LskXJjPT2bXMIodrtZq%2F-Lu619eFDEiIxlU5s0kS%2F-Lu61jdlAdObe-ZzGpQ9%2Fc-14.png?alt=media\&token=05e8f958-956a-4b9a-823f-c32b69208fde)

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