Use a variable to remember a number, and change it each time a button is pressed.
A step counter, a scoreboard, a ticket machine at the deli: each one remembers a number and changes it when something happens.
So far the micro:bit has shown pictures but forgotten everything straight away. To count, it needs to remember a number. A variable is a named place to keep a value, such as count. You find variable blocks in Variables.
How this lesson works: Predict, Run, Investigate, Modify, Make
Predict: Read the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.
Run: Run it and compare the result with your prediction.
Investigate: Trace how each line of code connects to what you see.
Modify: Change the code on purpose and check the result.
Make: Build something new using what you have learned. Expect a few tries; that is normal.
Make your prediction first. Help opens once you've locked it in.
PredictRead the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.
Predict
Show as code
lesson.ts
let count = 0basic.showNumber(count)
input.onButtonPressed(Button.A, function () {
count += 1basic.showNumber(count)
})
set count to 0 runs once, at the start. change count by 1 runs each time A is pressed.
RunRun it and compare the result with your prediction.
Run
Press A slowly three times. Wait for each number to show before you press again.
Show as code
lesson.ts
let count = 0basic.showNumber(count)
input.onButtonPressed(Button.A, function () {
count += 1basic.showNumber(count)
})
Try it yourselfYour edits are saved in this browser.
It takes a few seconds to load. Your work in this step is kept.
InvestigateTrace how each line of code connects to what you see.
Investigate
Here is a counter with a reset button.
Show as code
lesson.ts
let count = 0input.onButtonPressed(Button.A, function () {
count += 1basic.showNumber(count)
})
input.onButtonPressed(Button.B, function () {
count = 0basic.showNumber(count)
})
Work out the value of count after each press.
Mia's program:
Show as code
let count = 0input.onButtonPressed(Button.A, function () {
count += 1
})
basic.forever(function () {
count = 0basic.showNumber(count)
})
Say what each block does to the variable.
This program counts with A and resets with B. Fill in count after each press.
press
count after
(start)
0
A
A
B
A
Match each block with what it does to count.
set count to 0
change count by 1
show number count
ModifyChange the code on purpose and check the result.
I can change a variable when an event happens, and show it.
I can explain the difference between set and change.
Before class
A variable is a named box that holds a value, such as count. Make one in Variables › Make a Variable. Two blocks matter: set puts a new value in the box (throwing the old one away) and change adds to what is already there. Where the set block goes matters: in on start it runs once; in forever it runs again and again and wipes the count. Numbers above 9 scroll across the screen, so ask students to wait before pressing again.
Running the lesson
Predict: most students say 3. Some say 1 (change sets it to 1). Ask what count is before the second press. Ask the students who chose 1 what change count by 1 does to a count that is already 1.
Run: press slowly. Point out that the number is remembered between presses: that is the variable.
Investigate: the trace shows set (B) throwing the value away. Mia's counter is the most common mistake this lesson: set inside forever.
Modify: change by -1 counts down. Negative numbers show with a minus sign.
Mix it up: a look back at events, with no variables.
Make: three events and one variable. steps is already made for them.
The hard part (on purpose)
Telling set from change. Ask what count was before the press, and what it is after.
When students get stuck
The number always shows 0. Ask: Where is your set count to 0 block? Fix: Move it into on start. In forever it resets the count all the time.
The count goes up but nothing shows. Ask: Which block shows the number, and when does it run? Fix: Add show number count inside the button event, after the change block.
Numbers above 9 seem to go missing. Ask: What happens when the screen is still scrolling? Fix: Wait for the number to finish scrolling, or press more slowly.
Stretch and support
Stretch: Make the scoreboard show both scores on A+B, one after the other.
Support: Give students the step counter's three event blocks already in place, and ask them only to fill in set, change and show.
Where it leads
Next lesson: sensors give numbers too, but the micro:bit updates them by itself. Class ideas: a class tally of votes with A and B; count the steps between classrooms with a real micro:bit in a pocket.
What each step is for
Investigate: A variable keeps its value between events. change adds to it; set replaces it. Setting it again and again in forever wipes it out.
Modify: change can add any amount, including a negative one, which counts down.
Mix it up: Each event block holds the blocks for its own event, and runs only when that event happens.