MICROBITS
Micro-bits are small computers that allows you to program simple computer programs with several inputs.
We use micro bits along with makecode to create programs that respond to our inputs.
If - Then Statements
Robotic language runs on very simple logic: action and response. This can be coded in an if-then statement, also known as a conditional statement!
Some real life action and response examples:
If you mow the lawn then i’ll give you 5 bucks
If you hit your little brother then he’ll get angry
If you close the lock the front door then the house is secure
Computers are no different, they work on the exact same principle. You can essentially code if-then statements.
If (true) ⟵ If the statement is considered ‘true’
{do this} ⟵ then execute the commands in this bracket
For example:
If (sky == raining) {
open umbrella;
}
You can even code computer if-then statements as have multiple outcomes using if-else statements
If (true) ⟵ If the statement is considered ‘true’
{do this} ⟵ then execute the commands in this bracket
else ⟵ (aka otherwise)
{do this} ⟵ then execute the commands in this bracket
For Example
If (sky == raining) {
open umbrella;
}
else {
do nothing;
}
Try it yourself
Start by coding ‘Forever’ into your microbit
Code an if-then statement to display an ‘X’ when button ‘a’ is pressed
Add an else statement to display ‘Y’ if button ‘b’ is pressed
VAriables
Sometimes we would like to remember certain values. For instance maybe we need to remember a number code to unlock a door. This is the use for variables!
Variables are used to store numbers/words:
First a variable must be created (aka named) and an initial value type declared (aka is the value type a number or word):
new variable Counter;
Counter = 10;
Now we have a variable named ‘Counter’ it is storing a number, and in this case it the number 10.
We can change variables such as add a number or minus a number:
Counter = (Counter - 1);
We started the ‘Counter’ variable set so 10, now the ‘Counter’ is set to the number 9…cool!
We can pair it with an if statement:
If (Counter > 10) {
Then display the ‘Counter’ value;
Counter = (Counter - 1);
}
Challenge 1
Code your microbit to count down from 10 to 0:
It should display the numbers 10 through 0 on the microbit display
Challenge 1.1
Code your microbit to count down from 100 to 0:
Challenge 1.2
Code your microbit to count down from 100 to 0:
Pressing button ‘a’ should pause the counter, the display should also pause the number being counted down on the display.
Challenge 1.3
Code your microbit to count down from 100 to 0:
Pressing button ‘a’ should pause the counter, the display should also pause the number being counted down on the display.
Pressing button ‘b’ should resets the counter back to 10