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

Debugging and light grids

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.

Worked examples
StartDisplay image 1: eyes openwait 0.5 secondDisplay image 2: eyes closedwait 0.5 secondDisplay a clear screenEnd
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.
Python
1from microbit import *
2display.show(Image("09090:09090:00000:90009:09990"))
3sleep(500)
4display.show(Image("00000:09090:00000:90009:09990"))
5sleep(500)
6display.clear()
Output
open eyes, closed eyes, then the lights go off
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.
ABthe last image stays on
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.
StartDisplay diagonal Await 0.5 secondDisplay diagonal Bwait 0.5 secondDisplay a clear screenEnd
2
Predict what the lights do. Which light is on at the end, and for how long was the first light on?
Python
1from microbit import *
2display.show(Image("00000:09000:00000:00000:00000"))
3sleep(400)
4display.show(Image("00000:00000:00000:00090:00000"))
5sleep(400)
3
Zoe’s two-image program shows an error. Find the mistake and write the corrected line.
Python
1from microbit import *
2display.show(Image("90009:00000:00000:00000:90009"))
3sleep(500)
4display.show(Image("09090:00000:00000:00000:09090")
5sleep(500)
6display.clear()
4
Marek changes both lines sleep(500) to sleep(5). What will he see, and why? How would he fix it?