Week 28 · Unit 7.6: Sequencing and pattern recognition · 3 lessons
2
Lights on a grid
Computing 7: Unit 7.6, pp. 182–224
GoalPlan a picture on a 5 × 5 grid and write it as 0s and 9s in MicroPython code.
New words
LED · a light-emitting diode: a tiny light; the micro:bit has 25 of them in a 5 × 5 gridrow · a line of lights from left to right; the display has 5 rows, counted from the topcolumn · a line of lights from top to bottom; the display has 5 columns, counted from the left
Explanation
The micro:bit has 25 LEDs in a grid of 5 rows and 5 columns. In the code, each light has a number. The number 9 switches a light on at full brightness and 0 switches it off. To make a picture, first draw a 5 × 5 grid and mark the lights that are on. Then write the picture row by row, from the top. Each row is five digits inside quotation marks, and a colon comes after every row except the last. All the rows are placed inside Image( ... ), which is inside display.show( ... ). Close both brackets at the very end. The numbers in the code have the same shape as the lights on the board, so you can read the picture from the code.
Number in the code
What the light does
0
off
9
on, at full brightness
09090
a row: the second and fourth lights are on and the others are off
Worked examples
The plan. To make the letter L, the whole left column is on, and the whole bottom row is on. Everything else is off. The picture on the board shows the lights that will be on.
The code. Write 9 for each light that is on and 0 for each light that is off, row by row. Rows 1 to 4 each start with 9, and the last row is all 9s. Notice the colon after each row except the last, and the two brackets at the end.
What was written
What goes wrong
a row with four digits or six digits
each row must have exactly five digits
only four rows
the grid must have five rows
no colon between two rows
the program cannot tell where one row ends
the last bracket missing
the brackets must be closed, or the program will not run
0 and 9 mixed up
the picture is the opposite: lit lights become dark
These are typical mistakes. Each one gives an error or the wrong picture.
Class activity
Draw and decode (no computer). Draw a 5 × 5 grid on squared paper. Colour squares to make a letter or a simple shape. Write the five rows as strings of 0 and 9, with 9 where a square is coloured. Give only your five strings to a partner, who draws their own grid and colours it from your code. Compare the two drawings. Then swap roles. Count the lights that are on in each picture.
Practice
1
Write the five rows of code for the picture on the board (the letter H).
It is a square outline: the top row and the bottom row are all on (5 + 5), and the middle three rows each have one light at each end (3 × 2 = 6). Altogether 5 + 5 + 6 = 16 lights are on.
3
Cem wants to show a heart, but the picture on the micro:bit is wrong. Find the mistake. Then say what the picture shows when the mistake is fixed.
The second row has only four digits (9999), but every row needs five digits. It should be 99999. Then the picture is a heart: two bumps at the top, a wide middle and a point at the bottom.
4
Write the code rows that switch on only the four corner lights.
Row 1: 90009. Rows 2, 3 and 4: 00000. Row 5: 90009. The corners are the first and last light of the first and last rows.