Week 29 · Unit 7.6: Sequencing and pattern recognition · 3 lessons
1
Longer light sequences
Computing 7: Unit 7.6, pp. 182–224
GoalExtend a light sequence by planning each new image on a grid and putting its code in the right place.
New words
extend · to make a sequence longer by adding more images to itinsert · to put a new section of code between two sections that are already thereorder · the position of each image in the sequence; it must be the same in the flowchart and in the code
Explanation
You can make a light sequence longer by adding images to it. First change the flowchart. Then draw a grid for each new image and write its rows of 0s and 9s. Each image needs its own show line and its own sleep line. The code must follow the order of the flowchart. If the new image belongs between two old images, insert its code between their code. A quick way is to copy the code for an earlier image, paste it in the right place and edit the rows. Finally run the whole sequence and check that the order is right. If the order of the code is wrong, the lights will move in the wrong order.
Worked examples
A lift indicator first shows a light at the bottom and then at the top, which is a sequence of two images. To make it show every floor, the plan is extended: a new image with the light in the middle is inserted between the two old images.
Row
Image 1
Image 2
Image 3
1
00000
00000
00900
2
00000
00000
00000
3
00000
00900
00000
4
00000
00000
00000
5
00900
00000
00000
Each image has a grid. The light is always in the middle column, and it moves up the board: row 5, then row 3, then row 1. The new image needs only one new grid, because the other grids stay the same.
This is the finished program. The code for image 2 is copied from image 1 and inserted between image 1 and image 3, and the rows are edited. Every image is followed by sleep(400). The last line clears the screen.
Class activity
Insert a frame (no computer). Write each image of the lift indicator on its own card, with its show line and its sleep line. Put the cards in order and read the program aloud. Your partner then gives you a new card for the light between row 5 and row 3 and you insert it in the right place. Swap two cards on purpose and read the program again: what does the lift do now?
Practice
1
A water tank fills up from the bottom. Image 1: only the bottom row is lit. Image 2: the bottom two rows are lit. Image 3: the bottom three rows are lit. Draw the flowchart, write the three grids as rows of 0 and 9, and write the program. Use waits of half a second and clear the screen at the end.
Image 1: 00000, 00000, 00000, 00000, 99999. Image 2: 00000, 00000, 00000, 99999, 99999. Image 3: 00000, 00000, 99999, 99999, 99999. The program starts with from microbit import *, then for each image a display.show(Image("...:...:...:...:...")) line and sleep(500), and the last line is display.clear(). The flowchart has three display boxes, each followed by a wait of 0.5 second, and then a clear screen.
2
This program should make a lift go down: the top light, then the middle, then the bottom, each for 0.4 second, then clear. Find the two mistakes and write the corrected program.
Mistake 1: there is no sleep after the top image, so it disappears at once. Mistake 2: the bottom light comes before the middle light, so the order is wrong. Corrected: show top, sleep(400), show middle, sleep(400), show bottom, sleep(400), display.clear().
3
Look at the program. How long is each image on the lights? What stays lit when the program ends?
Image 1 is on for 0.5 second, image 2 for 1 second (1000 milliseconds) and image 3 for 0.25 second. After that, image 3 stays lit, because there is no display.clear().
4
Explain why the code for a new middle image must be inserted between the code for image 1 and the code for image 3, and not at the end of the program.
The lights follow the order of the code, from top to bottom. If the middle image is added at the end, the lights would show image 1, image 3 and then the middle image, which is the wrong sequence.