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
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 codeWhat the light does
0off
9on, at full brightness
09090a row: the second and fourth lights are on and the others are off
Worked examples
ABplan for the letter L
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.
Python
1from microbit import *
2display.show(Image(
3    "90000:"
4    "90000:"
5    "90000:"
6    "90000:"
7    "99999"))
Output
the letter L is shown and stays lit
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 writtenWhat goes wrong
a row with four digits or six digitseach row must have exactly five digits
only four rowsthe grid must have five rows
no colon between two rowsthe program cannot tell where one row ends
the last bracket missingthe brackets must be closed, or the program will not run
0 and 9 mixed upthe 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).
AB
2
Look at the code. Draw the picture it makes on a 5 × 5 grid, and count how many lights are on.
Python
1from microbit import *
2display.show(Image(
3    "99999:"
4    "90009:"
5    "90009:"
6    "90009:"
7    "99999"))
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.
Python
1from microbit import *
2display.show(Image(
3    "09090:"
4    "9999:"
5    "99999:"
6    "09990:"
7    "00900"))
4
Write the code rows that switch on only the four corner lights.