Week 28 · Unit 7.6: Sequencing and pattern recognition · 3 lessons
3
Pictures that move
Computing 7: Unit 7.6, pp. 182–224
GoalPlan, write and test a program that shows two images one after the other.
New words
frame · one picture in a sequence of picturessleep · a command that pauses the program; sleep(500) waits half a secondclear screen · switching all the lights off, with display.clear()
Explanation
A moving picture is made of more than one image, shown one after the other. First plan the sequence in a flowchart. Then make a grid for each image and write its rows of 0s and 9s. In the program, write the code for image 1, then a sleep, then the code for image 2, then another sleep. The delay is important: it gives your eyes time to see each image. When the program ends, the last image stays on the lights. To switch the lights off at the end, add display.clear(). The word vertical means from top to bottom and horizontal means from side to side.
flowchart→grid for image 1→grid for image 2→code: image 1→sleep→code: image 2→sleep→clear
Worked examples
Plan a blinking face. Image 1 has open eyes (two lights tall) and image 2 has closed eyes (one light tall). The flowchart shows both images, a wait of half a second after each, and a clear screen at the end.
This is the code for the plan. The second image repeats the pattern of the first, with one row changed. Each image is followed by sleep(500), which is half a second.
Without display.clear(), the last image stays lit after the program ends. This is the board when the blinking program has no clear line at the end.
Class activity
Flip-book (no computer). Draw two 5 × 5 grids on two cards, for example a small star and a big star. Hold the cards in a pile and flip from the first card to the second while you count one-and-two for half a second. Then write the code for both images on paper, with sleep(500) between them, and decide what you would add to switch the lights off at the end.
Practice
1
Look at the flowchart. Diagonal A runs from the top-left light to the bottom-right light, and diagonal B runs from the top-right light to the bottom-left light. Write the two grids as rows of 0 and 9, and write the MicroPython program.
Image 1 (top left to bottom right): 90000, 09000, 00900, 00090, 00009. Image 2 (top right to bottom left): 00009, 00090, 00900, 09000, 90000. Program: from microbit import *, then display.show(Image("90000:09000:00900:00090:00009")), sleep(500), display.show(Image("00009:00090:00900:09000:90000")), sleep(500), display.clear().
2
Predict what the lights do. Which light is on at the end, and for how long was the first light on?
First, the light in the second row and second column is on for 0.4 second. Then it goes off and the light in the fourth row and fourth column comes on. The program stops and that light stays on, because there is no clear line. The first light was on for 0.4 second.
3
Zoe’s two-image program shows an error. Find the mistake and write the corrected line.
Line 4 is missing its last bracket. The correct line is display.show(Image("09090:00000:00000:00000:09090")). Every ( must have a matching ).
4
Marek changes both lines sleep(500) to sleep(5). What will he see, and why? How would he fix it?
5 milliseconds is far too short. Each image is gone almost at once, so he sees only a flicker, and the first image cannot be recognised. To fix it he changes the numbers back to sleep(500), or another delay that is long enough to see.