AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Show it and count it

Week 20 · Unit 8: Be an innovator · 3 lessons
3

Count and reset

Computing 5: Unit 8, pp. 104–118
GoalTrace a micro:bit program that counts button presses with a variable and resets the count to 0 when the equal-to test is true.
New words
counter · a variable that goes up by 1 each time something happensreset · to set a variable back to its starting value, often 0change by · a block that adds a number to a variableon start · a block whose blocks run once when the program begins
Explanation

A counter is a variable that counts how many times something happens. It must start at a known number, usually 0. The ‘set variable to 0’ block does this when the program starts. Each press runs ‘change variable by 1’, which adds 1 to the counter. An equal-to test then checks whether the counter has reached the limit. If it has, the program shows a message and resets the counter to 0. If the reset is missing, the counter goes past the limit and the equal-to test is never true again.

BlockWhat it doesCounter before → after
set [count] to 0replaces the value with 05 → 0
change [count] by 1adds 15 → 6
change [count] by -1takes 1 away5 → 4
(count) = (5)only asks a question5 → 5
Worked examples
Bellsetringsto0on startBellchangeringsby1show stringBreaksetringsto0show numberringsifrings=4thenelseon buttonApressed
A bell counter starts at 0. Each press of button A adds 1. When it reaches 4 the board shows the word Break and resets. Until then it shows the count.
Pressrings after changerings = 4?Display
11false1
22false2
33false3
44trueBreak (rings reset to 0)
51false1
62false2
Trace six presses. After press 4 the reset makes the count start again.
Pressringsrings = 4?Display
33false3
44trueBreak
55false5
66false6
Suppose the reset block is missing. The count passes 4 and the test is never true again, so Break shows only once.
Class activity

Counting claps (pairs). Player A claps and Player B holds a card showing the counter. Start the card at 0 and turn it up by 1 for each clap. When the card shows 4, B says ‘Break!’ and turns the card back to 0. Do 10 claps. What does the card show at the end? Try again with a limit of 3, and write down what changes.

Practice
1
The goal counter starts at 0. Button B is pressed 5 times. What does the board show on each press, and what is goals at the end?
Goalssetgoalsto0on startGoalschangegoalsby1show iconhappysetgoalsto0show numbergoalsifgoals=3thenelseon buttonBpressed
2
A counter starts at 0 and goes up by 2 on each press. The test is ‘counter = 5’. Will the test ever be true? Why?
3
The program should count presses up to 3, but it always shows 1. Find the mistake and say how to fix it.
Lapssetlapsto0changelapsby1show stringDoneshow numberlapsiflaps=3thenelseon buttonApressed
4
Write an algorithm for a drink counter. Button A adds 1. When the count is 3 it shows the word Full and resets.