In this section, we are doing math with our micro:bit. Math is a key element in any human endeavour and after this lesson, you will be coding your own calculations in no time.
Let's start our Math and micro:bit exploration by counting. First, we will create a counter from 1 to 5 using a basic approach, and then we'll find a better way to do it, with more control.
Before attempting each of the following parts, try to create code that counts, while considering the following points:
What to display on the micro:bit?
When to change the count? What action on the micro:bit will trigger the count?
By how much are we counting? Are we counting up (adding, multiplying) or down (subtracting, dividing)
What blocks do we need to display our count?
See more information about these powerful concepts in the Teacher's Corner - Counting, below
Count from 1 to 5 when buttons A and B are pressed at the same time (A+B)
This initial method counts all the way from 1 to 5 as soon as we press A+B. As you can see, we are introducing a new block, show number. This block works just like show string, but it is used to display numbers and do mathematical operations.
As we said, this works, but we have no control over when to count, it just does it and it will never go past 5!
Don't forget to test your progress using the micro:bit emulator!
Controlled count - PART A - introducing variables
Our code is about to get more powerful as we will introduce a variable, called counter that will keep track of the number of times we press A+B.
Variables are places in the micro:bit or computer where we can store information.
The first step is to create the variable
Give the variable a name, in our case Counter
Now we have access to new blocks!
Counter stores and returns the value. In this case, the count
set defines what is the value of the variable
change, lets us change the value of the variable
Controlled count - PART B - coding the counter
All that's left to do is to use our new blocks to count by 1, each time we press A+B
Reading the code.
When the application starts, the Counter variable is set to be zero (0), then when the user presses the A and B buttons at the same time, the variable Counter increases by 1 and then the new value of Counter is displayed.
Make sure you test it in the emulator to better understand the previous sentence!
Great work in getting the counter working! time for a mini-challenge.
❓Count by 2, or by 5!
How did you do? I'm sure you did great!
Solving this problem opens many possibilities as you can count by any number you want. This is our solution to counting by 2.
Hint: we don't always need to count by adding. Changing our variable by -2 will result in subtracting
We have made lots of progress and in the next section If Hot!, we will explore some really exciting concepts in coding and the micro:bit!
Asking your students to think about the challenge, to count, helps create deeper connections beyond the micro:bit and coding. It will help introduce Computational Thinking and how we use it in our daily lives.
Computational Thinking
Decomposition: breaking down something complex into smaller, simpler pieces.
Pattern recognition: matching new incoming information with information retrieved from memory.
Abstraction: identifying essential information while ignoring irrelevant details.
Algorithms:followingsimple steps to solve problems or tackle a challenge.
🍎Teacher's Corner - Counting - Variables
A variable is a container for a value, like a number we might use in a sum, or a string that we might use as part of a sentence. MDN