5. Setting pixels


5.1. Set pixel

../_images/microbit_numbering.png ../_images/microbit-led-coords.png
Each pixel on the 5 by 5 grid can be controlled individually.
The column numbers are 0 to 4 from left to right.
The row numbers are 0 to 4 from top to bottom.
display.set_pixel(x, y, value)

Set the brightness of the LED at column x and row y to value, which has to be an integer between 0 and 9, where 0 is off and 9 is full brightness.

The code below turns on the pixel in the top left with full brightness.
../_images/pixel00.png
from microbit import *

display.set_pixel(0, 0, 9)
The code below turns on the pixel in column 2, row 1 with full brightness.
../_images/pixel21.png
from microbit import *

display.set_pixel(2, 1, 9)

Tasks

  1. Write code to turn on the pixel, 3 columns across and 4 rows down.

  2. Write code to turn on the pixel, 4 columns across and 2 rows down.

  3. Write code to turn on the pixel in the top right.

  4. Write code to turn on the pixel in the bottom right.

  5. Write code to turn on the pixel in the bottom left.

  6. Write code to turn on the 4 corner pixels.

  7. Write code to turn on the top 5 pixels.

  8. Write code to turn on the top 5 pixels at brightnesses of 1, 3, 5, 7, 9 from left to right.

Write code to turn on the pixel, 3 columns across and 4 rows down.

from microbit import *

display.set_pixel(2, 3, 9)

Write code to turn on the pixel, 4 columns across and 2 rows down.

from microbit import *

display.set_pixel(3, 1, 9)

Write code to turn on the pixel in the top right.

from microbit import *

display.set_pixel(4, 0, 9)

Write code to turn on the pixel in the bottom right.

from microbit import *

display.set_pixel(4, 4, 9)

Write code to turn on the pixel in the bottom left.

from microbit import *

display.set_pixel(0, 4, 9)

Write code to turn on the 4 corner pixels.

from microbit import *

display.set_pixel(0, 0, 9)
display.set_pixel(0, 4, 9)
display.set_pixel(4, 0, 9)
display.set_pixel(4, 4, 9)

Write code to turn on the top 5 pixels.

from microbit import *

display.set_pixel(0, 0, 9)
display.set_pixel(1, 0, 9)
display.set_pixel(2, 0, 9)
display.set_pixel(3, 0, 9)
display.set_pixel(4, 0, 9)

Write code to turn on the top 5 pixels at brightnesses of 1, 3, 5, 7, 9 from left to right.

from microbit import *

display.set_pixel(0, 0, 1)
display.set_pixel(1, 0, 3)
display.set_pixel(2, 0, 5)
display.set_pixel(3, 0, 7)
display.set_pixel(4, 0, 9)

5.2. Pixel rows and columns

for-loops can be used to turn on all the pixels in a row or colum.
The code below sets the brightness to 9 for the first column, column 0.
../_images/col0.png
from microbit import *

x = 0
for y in range(0, 5):
    display.set_pixel(x, y, 9)

The code below sets the brightness to 9 for the first row, row 0.
../_images/row0.png
from microbit import *

y = 0
for x in range(0, 5):
    display.set_pixel(x, y, 9)

Tasks

  1. Write code to turn on the pixels in column 3.

  2. Write code to turn on the pixels in row 2.

Write code to turn on the pixels in column 3.

from microbit import *

x = 3
for y in range(0, 5):
    display.set_pixel(x, y, 9)

Write code to turn on the pixels in row 2.

from microbit import *

y = 2
for x in range(0, 5):
    display.set_pixel(x, y, 9)

5.3. Pixel rows and columns lists

for-loops can be used to turn on pixels based on values in lists.
Each row will have the same patern of pixels.
Each column will have the same patern of pixels.
A variable, xlist, can store the columns numbers.
A variable, ylist, can store the row numbers.
The code below produces an image of a six on a die.
../_images/dice6.png
from microbit import *

xlist = [0, 4]
ylist = [0, 2, 4]
for x in xlist:
    for y in ylist:
        display.set_pixel(x, y, 9)

Tasks

  1. Adjust the code above to turn on pixels that are in both columns 1 to 3 and rows 0 and 4.

  2. Adjust the code above to turn on pixels that are in both columns 0 and 4 and rows 1 to 3.

  3. Combine the two answers to produce a square shape without the corners.

Adjust the code above to turn on pixels that are in both columns 1 to 3 and rows 0 and 4.

from microbit import *

xlist = [1, 2, 3]
ylist = [0, 4]
for x in xlist:
    for y in ylist:
        display.set_pixel(x, y, 9)

Adjust the code above to turn on pixels that are in both columns 0 and 4 and rows 1 to 3.

from microbit import *

xlist = [0, 4]
ylist = [1, 2, 3]
for x in xlist:
    for y in ylist:
        display.set_pixel(x, y, 9)

Combine the two answers to produce a square shape without the corners.

from microbit import *

xlist = [1, 2, 3]
ylist = [0, 4]
for x in xlist:
    for y in ylist:
        display.set_pixel(x, y, 9)

xlist = [0, 4]
ylist = [1, 2, 3]
for x in xlist:
    for y in ylist:
        display.set_pixel(x, y, 9)