Week 29 · Unit 12: Be a problem solver · 3 lessons
2
Counting button presses
Computing 5: Unit 12, pp. 159–170
GoalCompare counting presses with an event block and with a forever loop, and say which count is right.
New words
event · something that happens, such as a button being pressed, that starts a scriptcounter · a variable that counts how many times something happenspause · a block that makes the program wait for a number of millisecondsmillisecond · one thousandth of a second, written ms
Explanation
A counter is a variable that goes up by 1 each time something happens. There are two ways to count button presses. The block ‘on button A pressed’ is an event block. It runs once for each press, so the count is right. A forever loop with an if block checks the button again and again. If the button is held down, the condition is true at every check, and the counter goes up at each one. A pause block slows the checks down, but a long press is still counted several times. To count presses, use an event block.
Worked examples
Press
1
2
3
4
5
6
lengths after the press
1
2
3
4
5
6
A swim coach presses A each time a swimmer finishes a length of the pool. The event block counts 1 for each press, however long the press lasts. After 6 presses the board shows 6.
The forever version checks every 500 ms. A press that is held down for two seconds is true at four checks, so the counter goes up by 4 for one press.
Check (every 500 ms)
1
2
3
4
5
A is down?
yes
yes
yes
yes
no
lengths
1
2
3
4
4
Here is one long press counted by the forever version. The button is down at the first four checks.
Class activity
Press counter (3 players, a pencil and paper). Player A taps the table as the button, sometimes quickly and sometimes holding the hand down for a few beats. Player B is the event block and makes one mark for each tap. Player C is the forever block and makes a mark on every beat (a clap every second) when A’s hand is down. After 20 beats, compare the marks. Which player has the right count of taps? Why does C get a bigger number?
Practice
1
A child skips with a rope, and a friend presses A once for each skip. Button A is pressed 12 times. What does the board show after press 9, after press 10 and after press 12?
After press 9 it shows 9. After press 10 the test skips = 10 is true, so it shows the happy face. After press 12 it shows 12. The event block counts each press once.
2
The forever version checks every 1000 ms. Button A is held down for the first three checks and then released. What does skips hold at the end?
It holds 3, because the condition is true at three checks.
3
Which version should count the lengths of a swimmer, and why?
The event version. It counts each press once. The forever version can count one press many times if it is held down.
4
Write the blocks for a board at a museum gate. It counts visitors when button B is pressed and shows the number of visitors.
on start: set visitors to 0. on button B pressed: change visitors by 1; show number visitors.