Week 29 · Unit 7.6: Sequencing and pattern recognition · 3 lessons
3
Project: model harbour lighthouse
Computing 7: Unit 7.6, pp. 182–224
GoalPlan, write and test a small lighthouse program on paper, and then evaluate it.
New words
project plan · a plan that says what the program must do, which input and output it uses, and how it will be testedevaluate · to judge how well something works and to say how it could be betterrecommendation · a suggestion for an improvement, based on what the tests and the users showed
Explanation
Problem: a model harbour in the school hall needs a lighthouse. When a visitor presses A, the lamp sweeps its beam from the left, down and to the right, and then goes off. Start with a project plan. Write what the program must do, the input, the output, and how it will be tested. Draw a flowchart in which the sweep is a sub-routine. Plan each image on a grid, with brightness numbers. Write the program. Then apply a test plan, find and fix any bug, and evaluate. Do the flowchart and the lights agree? Did pressing A start the lamp? Can each image be seen clearly? What would make the lighthouse better?
Row
Image 1: beam left
Image 2: beam down
Image 3: beam right
1
00900
00900
00900
2
05000
00500
00050
3
50000
00500
00005
4
00000
00500
00000
5
00000
00000
00000
Worked examples
Part of the plan
Answer
What it must do
sweep a beam left, down and right, when a visitor presses A
Input
button A
Output
three images; the lamp (the top light) is at brightness 9 and the beam is at brightness 5
Sequence
image 1, image 2, image 3, each for half a second, then clear the screen
How to test
a test plan with four tests
The project plan for the lighthouse. Each row answers one planning question.
The main flowchart. The sweep is a sub-routine, drawn here as a box that says run sub-routine. When A is not pressed, the lamp stays off. The sub-routine shows the three images with a wait of 0.5 second after each, and then clears the screen.
when A is pressed: beam left, down, right; then lights off
The program. The grids from the plan are copied row by row. The program checks button A again and again. Each time A is pressed, the lines under the if (the three images, each followed by its wait, and then the clear line) do the job of the Sweep sub-routine in the flowchart.
Class activity
Make it on paper (no computer). Plan your own sweep for a toy lighthouse or a pretend ship: draw the project plan table, the flowchart and three grids with brightness numbers, and write the program. Then give the program and the test plan to a partner. The partner plays the micro:bit: they read your code line by line and draw what the lights show, and they mark each test pass or fail. If you have a computer or tablet, flash the program to a micro:bit with an adult.
Practice
1
Copy the test plan for the lighthouse and write the expected outcomes by hand, before testing.
Test
What is tested
Test data
Expected outcome
Pass/fail
1
A starts the sweep
A pressed
?
2
The sweep order
A pressed
?
3
The sweep ends
A pressed
?
4
Idle state
no button pressed
?
Test 1: the first image appears, with the lamp bright and a beam to the left. Test 2: the three images appear in order: left, down, right, each for half a second. Test 3: after the third image the screen is clear. Test 4: the screen stays clear.
2
Ben copied the code for image 1 to make image 3, but he forgot to edit it. Test 2 fails. Say what the lights do and write the correct third block.
The beam goes left, down, and then left again, so it never reaches the right. The third block must show the right beam: display.show(Image("00900:00050:00005:00000:00000")).
3
Three visitors tried the lighthouse. Write three recommendations to improve it, using their comments.
Visitor
Comment
Visitor 1
The beam moves too fast for me to follow.
Visitor 2
It jumps from left to down. It is not smooth.
Visitor 3
The beam is too dim to see from the back of the hall.
Model answer: make each image stay on longer, for example sleep(800), so that the beam is easier to follow; add a fourth image between left and down so the sweep is smoother; make the beam brighter, for example 7 instead of 5, but still dimmer than the lamp at 9. Each recommendation should match a comment.
4
Choose your own problem: a crossing light for a model railway, or a signal for a toy bus stop. Write a short project plan with the same five parts as the lighthouse plan.
Any plan with the five parts is correct. For example, a railway crossing: it must flash a warning when button B is pressed; input: button B; output: two images, a cross at brightness 9 and a cross at brightness 3, fading in turns; sequence: image 1, image 2, repeated; how to test: a test plan with tests for starting, sequence and ending.