← Teacher guide

Count with a variable

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

micro:bit Foundations · Remember and sense · about 45 minutes · Open the student lesson

Classroom resources

Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.

Students will be able to

Timing

Predict and Run 8 min · Investigate 12 min · Modify 8 min · Mix it up 3 min · Make 10 min · Scoreboard 4 min

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.

In class

On the slides at these points. Students can do the whole lesson on their own; these are for the room. In the deck, the Run, Modify and Make slides have the live playground in them (the tutorial link is the backup), and the notes above are in each slide's speaker notes.

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

What you will seeA question to askThe fix
The number always shows 0.Where is your set count to 0 block?Move it into on start. In forever it resets the count all the time.
The count goes up but nothing shows.Which block shows the number, and when does it run?Add show number count inside the button event, after the change block.
Numbers above 9 seem to go missing.What happens when the screen is still scrolling?Wait for the number to finish scrolling, or press more slowly.

Answers to the checks

  1. Predict: You press A three times. What number is on the screen?
    3. count starts at 0, and each press of A changes it by 1 and shows it: 1, then 2, then 3.
  2. Trace the presses: This program counts with A and resets with B. Fill in count after each press.
    1, 2, 0, 1
    change count by 1 adds one to whatever count is now. set count to 0 throws the old value away.
  3. Mia's counter: Mia built this counter. However fast she presses A, it always shows 0. Why?
    forever sets count back to 0 again and again
    The press does add 1, but forever runs set count to 0 straight after, many times a second. Setting the start value belongs in on start, which runs once.
  4. Set, change, show: Match each block with what it does to count.
    set count to 0 → C. throws the old value away and puts 0 there; change count by 1 → B. adds 1 to whatever count is now; show number count → A. shows count, without changing it
  5. Mix it up: Ravi's signal board shows the surprised face once, when it starts, and never when he shakes it. Why?
    The surprised face is in on start, not inside on shake
    on start runs once, at the start. To react to every shake, the face goes inside an on shake block.

Solutions

One possible solution for each task. Other correct programs are fine.

Modify › Count in twos

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

Modify › Count down

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

Make

The program as blocks
Show as code
let steps = 0
input.onGesture(Gesture.Shake, function () {
    steps += 1
})
input.onButtonPressed(Button.A, function () {
    basic.showNumber(steps)
})
input.onButtonPressed(Button.B, function () {
    steps = 0
    basic.showIcon(IconNames.Yes)
})

Change a scoreboard

Open task: any program that does what the step asks.

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.

Exit ticket

What is the difference between set count to 0 and change count by 1?

set count to 0 throws the old value away and puts 0 there. change count by 1 adds 1 to whatever count already is.

What each step is for

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.