There are two things that we want to use when dealing with the LED matrix. The display object allows us to interact with the matrix, show images, text or turn pixels on or off. The Image class has lots of useful functionality for defining the images you want to display on the matrix.
display.set_pixel(x, y, val)
You can set the brightness of individual pixels from 0 (off) to 9 (full brightness). The following example varies the brightness of the central pixel of the matrix.
from microbit import * while True: for i in range(0,10): display.set_pixel(2,2,i) sleep(100) for i in range(9,-1,-1): display.set_pixel(2,2,i) sleep(100)
You can use the display.get_pixel(x, y) method to find out the brightness of a pixel. You might need to do this if you are doing something where the brightness of pixels on the matrix varies with user or sensor input.
This method allows you to scroll text across the matrix at the speed of your choice. The delay argument is optional. The following example scrolls text slowwwwly.
from microbit import * while True: display.scroll("A message", delay=1000)
display.show(image, delay=0, wait=True, loop=False, clear=False)
We use this method to display an image on the matrix. A built-in image is used in this program.
from microbit import * display.show(Image.HAPPY)
It does what you expect.
from microbit import * while True: display.show(Image.HAPPY) sleep(1000) display.show(Image.SAD) sleep(1000) display.clear() sleep(1000)
The Image Class
The Image class is a way of working with patterns that you want to display on the screen. With the other code editors, you work directly with the screen and don't have an abstract model of a 5x5 pattern that you can work with.
Built-In ImagesMicroPython has a shedload of built-in images.
from microbit import * built_in_images = [Image.HEART, Image.HEART_SMALL, Image.HAPPY, Image.SMILE, Image.SAD, Image.CONFUSED, Image.ANGRY, Image.ASLEEP, Image.SURPRISED, Image.SILLY, Image.FABULOUS, Image.MEH, Image.YES, Image.NO, Image.CLOCK12, Image.CLOCK11, Image.CLOCK10, Image.CLOCK9, Image.CLOCK8, Image.CLOCK7, Image.CLOCK6, Image.CLOCK5, Image.CLOCK4, Image.CLOCK3, Image.CLOCK2, Image.CLOCK1, Image.ARROW_N, Image.ARROW_NE, Image.ARROW_E, Image.ARROW_SE, Image.ARROW_S, Image.ARROW_SW, Image.ARROW_W, Image.ARROW_NW, Image.TRIANGLE, Image.TRIANGLE_LEFT, Image.CHESSBOARD, Image.DIAMOND, Image.DIAMOND_SMALL, Image.SQUARE, Image.SQUARE_SMALL, Image.RABBIT, Image.COW, Image.MUSIC_CROTCHET, Image.MUSIC_QUAVER, Image.MUSIC_QUAVERS, Image.PITCHFORK, Image.XMAS, Image.PACMAN, Image.TARGET, Image.TSHIRT, Image.ROLLERSKATE, Image.DUCK, Image.HOUSE, Image.TORTOISE,Image.BUTTERFLY, Image.STICKFIGURE, Image.GHOST, Image.SWORD, Image.GIRAFFE, Image.SKULL, Image.UMBRELLA, Image.SNAKE] display.show(built_in_images, delay=1000,loop=True)
The above example uses an overloaded version of the display.show() method that allows you to pass a list of images as an argument. This creates a repeating animation/slideshow of all of the built-in image constants. Alternatively, you can use them just like this,
from microbit import * display.show(Image.HAPPY)
Make Your Own
You can define your own image quite easily with a string like this,
from microbit import * im = Image('99999:90009:90009:90009:99999:') display.show(im)
Remember that you can vary the brightness by choosing digits from 0 - 9.
from microbit import * im = Image('99999:97779:97379:97779:99999:') display.show(im)
Here a list of images is used to make a simple line animation.
from microbit import * imgs = [ Image('90000:00000:00000:00000:00000:'), Image('90000:09000:00000:00000:00000:'), Image('90000:09000:00900:00000:00000:'), Image('90000:09000:00900:00090:00000:'), Image('90000:09000:00900:00090:00009:') ] display.show(imgs, delay=500,loop=True)
You can set a pixel in an image and move it around quite nicely too. The advantage of being able to work with the image class is that you don't have the matrix updating until you want it to. In this example, we'll create a blank image. Then we'll set the central pixel to full brightness. Then we'll move the image around a little.
from microbit import * im = Image('00000:00000:00900:00000:00000:') display.show(im) sleep(500) while True: im = im.shift_up(1) display.show(im) sleep(500) im = im.shift_right(1) display.show(im) sleep(500) im = im.shift_down(1) display.show(im) sleep(500) im = im.shift_left(1) display.show(im) sleep(500)
You can make some variation to what happens to the dot in this program quite easily. Replace the sleep number with a variable. Find a way to vary that variable to alter the speed at which the dot moves.
You can also use the same technique that was used to display the built-in images to make some sort of animation without having to go through the clunkiness of clicking out your frames in the code editors.
Hopefully you have some ideas starting to form. There's going to be a lot more you can do with the matrix once you get a little bit more into the functionality of the micro:bit module. Make sure that you have had a go at all of the samples on this page. Mess around a little with the values and change the overall effect a little.