TigerJython4Kids
micro:bit Robotik
display
Deutsch   English   Franšais

2. LED DISPLAY

 

 

YOU LEARN HERE...

 

how to display text messages and simple pictures on the LED display.

 

 

25 LEDs

 

The 25 red color LEDs are arranged in a 5x5 matrix and individually addressable via their x and y positions. With the command

display.set_pixel(x, y, n)

the LED lights up at the position x, y with the brightness n, where n is a number from 0 to 9. For n = 9 the LED is brightest, for n = 0 it is off.

display.clear() switches off all LEDs.
 

 

 

EXAMPLES

 

Switch LEDs on and off

Your program should switch the LEDs in the middle row on and off one after the other. The command sleep(400) stops the program for 400 milliseconds. The currently lit LED stays on during this time and is then switched off.

 




Program:

from microbit import *

for x in range(5):
    display.set_pixel(x, 2, 9)
    sleep(400)
    display.set_pixel(x, 2, 0)
► Copy to clipboard

In the next program a randomly selected LED should be switched on and off. To structure the program, you define a function randomLed(). In this function, all LEDs are first cleared and two random numbers x and y between 0 and 4 are requested. Then the LED with these coordinates is turned on at maximum brightness.

 

In the main program, the function randomLed() is called in an infinite loop every 200 milliseconds. So the program runs until you download a new program, press the reset button (button next to the USB port) or interrupt the power supply.

Program:

from microbit import *
from random import randint

def randomLed():
    display.clear()
    x = randint(0, 4)
    y = randint(0, 4)    
    display.set_pixel(x, y, 9)

while True:
    randomLed()
    sleep(200)  
► Copy to clipboard

 

Show Images

The module microbit stores several 5x5 pixel images, which can be displayed on the screen. Consult the documentation for the available pictures and their name (Menu option Help | APLU documentation -> micro:bit). 

Use the display.show() command to display an image. In this example, arrows are displayed successively, pointing north, east, south, and west.

 




Each image is displayed during 1000 milliseconds. With a for loop the process is repeated three times.

Program:

from microbit import *

for i in range(3):
    display.show(Image.ARROW_N)
    sleep(1000)
    display.show(Image.ARROW_E)
    sleep(1000)
    display.show(Image.ARROW_S)
    sleep(1000)
    display.show(Image.ARROW_W)
    sleep(1000)
► Copy to clipboard

 

Design your own images

You design your own images by creating an image object using the Image string parameter.  It consists of 5 blocks of 5 numbers 0 to 9 each. The blocks correspond to the individual lines. The numbers set the brightness in the range 0 (off) to 9 (very light).

The following program shows a square with pixels in full brightness.
 





Program:

from microbit import *

img = Image('99999:90009:90009:90009:99999:')
display.show(img)
► Copy to clipboard

 

 

MEMO

 

To control the LED display, use the display object with its functions set_pixel(), show(), scroll(), and clear(). More information about the allowed parameters can be found in the TigerJython menu under Help | APLU documentation | micro:bit -> MicroPython API.

 

 

EXERCISES

 

 

1.
Write a program that displays four diagonal arrows in succession:
Image.ARROW_NW
Image_ARROW_NE
Image.ARROW_SW
Image.ARROW_SE.
 

2.

Switch on all LEDs one by one by using the following double for loop:

for y in range(5):
    for x in range(5):        

Wait for 0.5 s after each LED is lit.



3.

Write a program that first turns on the top LED row, then the second, third, and so on. Let the process run in an endless loop.

 

2-1
Professional information:

Images defined in the Image class:

HEART, HEART_SMALL, HAPPY, SMILE, SAD, CONFUSED, ANGRY, ASLEEP, SURPRISED,
SILLY, FABULOUS, MEH, YES, NO, CLOCK12, CLOCK11, CLOCK10, CLOCK9, CLOCK8,
CLOCK7, CLOCK6, CLOCK5, CLOCK4, CLOCK3, CLOCK2, CLOCK1, ARROW_N,
ARROW_NE, ARROW_E, ARROW_SE, ARROW_S, ARROW_SW, ARROW_W, ARROW_NW,
TRIANGLE, TRIANGLE_LEFT, CHESSBOARD, DIAMOND, DIAMOND_SMALL, SQUARE,
SQUARE_SMALL, RABBIT, COW, MUSIC_CROTCHET, MUSIC_QUAVER, MUSIC_QUAVERS,
PITCHFORK, XMAS, PACMAN, TARGET, TSHIRT, ROLLERSKATE, DUCK, HOUSE,
TORTOISE, BUTTERFLY, STICKFIGURE, GHOST, SWORD, GIRAFFE, SKULL, SNAKE