6. Setting pixels
6.1. Set pixel
- 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.
from microbit import *
display.set_pixel(0, 0, 9)
from microbit import *
display.set_pixel(2, 1, 9)
Tasks
Write code to turn on the pixel, 3 columns across and 4 rows down.
Write code to turn on the pixel, 4 columns across and 2 rows down.
Write code to turn on the pixel in the top right.
Write code to turn on the pixel in the bottom right.
Write code to turn on the pixel in the bottom left.
Write code to turn on the 4 corner pixels.
Write code to turn on the top 5 pixels.
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)
6.2. Pixel rows and columns
from microbit import *
x = 0
for y in range(0, 5):
display.set_pixel(x, y, 9)
from microbit import *
y = 0
for x in range(0, 5):
display.set_pixel(x, y, 9)
Tasks
Write code to turn on the pixels in column 3.
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)
6.3. Pixel rows and columns lists
x_list
, can store the columns numbers.y_list
, can store the row numbers.from microbit import *
x_list = [0, 4]
y_list = [0, 2, 4]
for x in x_list:
for y in y_list:
display.set_pixel(x, y, 9)
Tasks
Adjust the code above to turn on pixels that are in both columns 1 to 3 and rows 0 and 4.
Adjust the code above to turn on pixels that are in both columns 0 and 4 and rows 1 to 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 *
x_list = [1, 2, 3]
y_list = [0, 4]
for x in x_list:
for y in y_list:
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 *
x_list = [0, 4]
y_list = [1, 2, 3]
for x in x_list:
for y in y_list:
display.set_pixel(x, y, 9)
Combine the two answers to produce a square shape without the corners.
from microbit import *
x_list = [1, 2, 3]
y_list = [0, 4]
for x in x_list:
for y in y_list:
display.set_pixel(x, y, 9)
x_list = [0, 4]
y_list = [1, 2, 3]
for x in x_list:
for y in y_list:
display.set_pixel(x, y, 9)