← micro:bit Foundations

Count with a variable

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.

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

The program as blocks
Show as code
lesson.ts
let count = 0
basic.showNumber(count)
input.onButtonPressed(Button.A, function () {
    count += 1
    basic.showNumber(count)
})

set count to 0 runs once, at the start. change count by 1 runs each time A is pressed.

You press A three times. What number is on the screen?

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.

The program as blocks
Show as code
lesson.ts
let count = 0
basic.showNumber(count)
input.onButtonPressed(Button.A, function () {
    count += 1
    basic.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.

The program as blocks
Show as code
lesson.ts
let count = 0
input.onButtonPressed(Button.A, function () {
    count += 1
    basic.showNumber(count)
})
input.onButtonPressed(Button.B, function () {
    count = 0
    basic.showNumber(count)
})

Work out the value of count after each press.

Mia's program:

The program as blocks
Show as code
let count = 0
input.onButtonPressed(Button.A, function () {
    count += 1
})
basic.forever(function () {
    count = 0
    basic.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.

presscount after
(start)0
A
A
B
A

Mia built this counter. However fast she presses A, it always shows 0. Why?

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.

Modify

Make A count up by 2: 2, 4, 6.

Need a hint?

Click the 1 in change count by 1 and type a new number.

Add an event for B that counts down by 1 and shows the number.

Need a hint?

Drag in on button B pressed from Input.

Another hint

change count by -1 counts down. Then show number count.

Try it yourselfYour edits are saved in this browser.
It takes a few seconds to load. Your work in this step is kept.

Make A count up by 2.

This ticks by itself when your blocks do what is asked.

Make B count down by 1 and show the number.

This ticks by itself when your blocks do what is asked.

InvestigateTrace how each line of code connects to what you see.

Mix it up

Ravi's signal board, with no variables at all.

The program as blocks
Show as code
lesson.ts
basic.showIcon(IconNames.Square)
basic.showIcon(IconNames.Surprised)
input.onButtonPressed(Button.A, function () {
    basic.showArrow(ArrowNames.West)
})
input.onButtonPressed(Button.B, function () {
    basic.showArrow(ArrowNames.East)
})
Ravi's signal board shows the surprised face once, when it starts, and never when he shakes it. Why?

MakeBuild something new using what you have learned. Expect a few tries; that is normal.

Make

Build a step counter. Each step shakes the micro:bit a little.

In the simulator, click SHAKE for each step.

Try it yourselfYour edits are saved in this browser.
It takes a few seconds to load. Your work in this step is kept.
Need a hint?

You need three events: shake, A and B. The variable steps is already made.

Another hint

Shake changes steps by 1. A shows it. B sets it back to 0.

A step counter: shake adds a step, A shows the count, B resets it.

This ticks by itself when your blocks do what is asked.

ModifyChange the code on purpose and check the result.

Change a scoreboard

This scoreboard keeps two variables, one for each team. Make each goal worth 2 points, then make a shake reset both scores to 0.

Try it yourselfYour edits are saved in this browser.
It takes a few seconds to load. Your work in this step is kept.
For teachers

Students use a variable to count button presses and shakes, and see the difference between setting a variable and changing it. About 45 minutes.

Open the teacher guide: answers, solutions, slides and a worksheet.

Students will be able to

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

  1. 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.
  2. Run: press slowly. Point out that the number is remembered between presses: that is the variable.
  3. Investigate: the trace shows set (B) throwing the value away. Mia's counter is the most common mistake this lesson: set inside forever.
  4. Modify: change by -1 counts down. Negative numbers show with a minus sign.
  5. Mix it up: a look back at events, with no variables.
  6. 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

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

Now you can
Next lesson5. Sensors are numbersRead light, temperature and tilt as numbers, and show them.