GoalPlan a micro:bit program for a real job with an input, process and output table, then write and trace its blocks.
New words
requirement · something that the program must doplan · a short description of the inputs, the process and the outputs before we write the programreset · to put a variable back to its start valuetrace table · a table that shows the value of the variables after each step
Explanation
A good programmer plans before writing blocks. First, say what job the program must do. Second, list the inputs, the process and the outputs in a table. Third, decide which variables are needed and what each event will do. Fourth, write the algorithm in short steps, then turn each step into blocks. At the end, trace the program with some test presses and check each answer by hand. A counter often needs a reset, so that a new count can start from 0. When the program has to choose between two outputs, add an if block, and test the values on both sides of the condition.
Part
Plan for a laps counter
Job
count the laps run, and show the distance
Input
button A (a lap is done), button B (show distance), A+B (start again)
Process
laps = laps + 1; distance = laps × 400
Output
number of laps, or distance in metres
Worked examples
This laps counter is for a running track where one lap is 400 m. A adds a lap, B shows the distance and A+B resets. Suppose that A is pressed three times, then B, then A+B.
Press
laps after
Board shows
A
1
1
A
2
2
A
3
3
B
3
1200
A+B
0
0
The trace of the laps counter. After B the board shows 3 × 400 = 1200.
A second job: help a runner to drink 8 cups of water in a day. Each press of A adds a cup. The board shows the number of cups, and when it reaches 8 it shows yes. Suppose that A is pressed 9 times. The board shows 1 to 7, and then it shows the yes icon at the 8th and at the 9th press, because the condition cups ≥ 8 is true both times.
Class activity
Plan a job (no computer): choose one job, such as a score keeper for a team game, a counter for the visitors at a school fair or a tool to count the pages read in a week. Draw a plan table with the job, the inputs, the process and the outputs. Write the algorithm in short numbered steps. Give your plan to a partner, who traces it with a list of six presses and checks the results. Did your partner find a missing step, such as a reset? Improve the plan. If you have a computer or tablet, build your program in a micro:bit simulator.
Practice
1
Complete the plan for a program that counts the visitors to a school fair and shows the total. What is the process?
Part
Plan
Job
count the visitors
Input
button A pressed
Process
?
Output
show number visitors
The process is to add 1 to visitors each time that A is pressed (visitors = visitors + 1).
2
A tray holds 30 eggs. A is pressed for each egg that is sold, and B shows how many are left. A is pressed 7 times and then B is pressed. What does the board show?
After 7 presses sold = 7. When B is pressed the board shows 30 − 7 = 23.
3
Write an algorithm in numbered steps for a score keeper. Button A adds 1 to the score and shows it. Button B starts again at 0.
1 Create the variable score. 2 When A is pressed, add 1 to score. 3 Show score. 4 When B is pressed, set score to 0. 5 Show score.
4
A plan says: button A adds 1 to cups, and the program shows yes if cups ≥ 8. Which values of cups would you test, and what do you expect?
Test the edge of the rule: 7 (it shows 7, not yes), 8 (yes) and 9 (yes). Also test a low value such as 2, which shows 2.