1 + 1 = 2
Last updated
Last updated
In this lesson, the kids will learn how to assign variable to each button and perform an addition function.
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
Click on the micro:bit icon on your toolbar or go to https://makecode.microbit.org/ on your browser.
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
Click Make a Variable and give it a name "A"
Click Ok. The variable A will now be created
Follow the same steps again to create variable "B"
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.
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.
Repeat the same step for button B
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
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
Now replace the zeros with variables A and B by dragging A and B from the Variables toolbar
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
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