Learning Outcomes

Objectives:

Must

Create python lists to represent the the LED Matrix on the micro:bit

Should

Make an image flash on the micro:bit using a simple loop

Could

Add a control variable to the loop to make the lights dim progressively

Keywords

Words to learn for this lesson are:

Loop, Iteration, LED Matrix, control variable

Starter

Get this code working in micropython and flashed onto your microbit:

```from microbit import *
im = Image('99999:90009:90009:90009:99999:')
display.show(im)```

im is an “Image” object, which controls the LED Matrix on the screen.

How does it work?  What do the numbers represent?

Main

So we can create an image on the micro:bit screen to manage the brightness, and state of each led.

Can you change the above code to fill the square in with dull pixels? (LEDs)

If we have full control over the screen, we can create a simple animation…

```from microbit import *
im = Image('99999:90009:90009:90009:99999:')
display.show(im)
sleep(500)
im = Image('99999:90009:90909:90009:99999:')
display.show(im)
sleep(500)```

This makes the centre pixel “flash” on and off…

Challenge:

Start with a small central pixel, and make it grow into a large square (the whole screen) then shrink back to one pixel….

At the moment, we are struggling with a lot of code to create our “animation”.  It is much easier to create a list, which holds various images…

```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)```

Make sure you type this in accurately!  All you have is a normal python list (inside [ ] and separated by commas).  The display.show()  function then controls the animation, or change from one image to another.  Experiment changing loop to False, and changing the duration of delay!

Challenge:

Create an animation showing a stick figure walk ONCE across the screen.  Easy on one micro:bit.  Then, once complete, coordinate your animation with your partner’s.  The stick figure walks across YOUR screen, then walks across the second micro:bit…
Hint – look at the a delay or sleep() before the animation starts…

This is all well and good if you want to move an entire image.  What if you want to change a pixel by a specific amount?

```from microbit import *

display.set_pixel(0,4,9)

```

Takes a specific LED (0,4)  and turns it to a brightness of 9 so:

```from microbit import *

display.set_pixel(0,4,0)

```

Turns it off again.  Remember loops?

for i in range(0,5) :

Creates a loop which repeats five times.  We can access the number of repetition in the variable i (first time 0, then 1, then 2, etc…)

so the code:

```from microbit import *

for x in range(0,5):
display.set_pixel(x,0,9)
sleep(100)

```

Will turn pixels on the top row progressively.

Plenary

Challenges:
1) Can you change the code so the first COLUMN (not row) lights up?
2) How about the first and last row? (maybe more than one loop)
3) Can you make the entire screen light up one pixel at a time using loop(s)
4) Make it flash?
5)The real toughy!! Make one pixel move around the edge of the screen…