Lessons 4 and 5 – Python Challenges

Learning Objectives

  • Design programs using the Python programming language
  • Develop your problem solving abilities

Learning Outcomes

All must develop simple programs using the instructions on the MicroPython website.  Give a brief description of how each program works. (Level 3)

Most should develop a mixture of simple and intermediate programs using the instructions on the Micropython website.  Be able to describe how the programs work and suggest possible improvements to each.  (Level 4)

Some could develop a mixture of intermediate and complex programs using the instructions on the Micropython website.  Be able to describe how the programs work and suggest possible improvements to each. (Level 5)

Keywords

Words to learn: program, Microbit, storage, network

Click here to open the MicroPython editor

Starter

Up until now we have used Blockly, which is a visual programming language.  This means you can create computer programs by slotting together blocks.  This is OK, especially when you are learning to program, but in real life programmers use text based languages to create computer programs.  This is because text based languages are much quicker to create programs in.

We can use a programming language called Python with the Microbit, and you will use Python in more depth in Year 8.  For these lessons though we will introduce Python to you and how you can use it with the Microbit.

Open the MicroPython editor by clicking the link above.  This will load a demo program which looks like this:

micropython1

The code above is the same as this in Blockly:

Capture

So what does all of the code written in Python mean mean?  Let’s go through it line by line:

from microbit import *

This line tells Python that it has to use the microbit library.  A library is a set of add-in instructions that allows Python to communicate properly with the microbit.  The programmer doesn’t need to do anything complicated.  You must always have this line at the top of any Python code you want to run on the Microbit.

while True:

This is the Python equivalent of the forever block in Blockly.  This means that whatever comes after it (and is indented) will repeat forever.

display.scroll('Hello, World!')

This tells the Microbit to scroll the message enclosed in apostrophes.  In this case it will scroll the message Hello, World! across your screen.

display.show(Image.HEART)

This tells the Microbit to display a built-in image.  Python has lots of built-in images – the list is below:

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.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

sleep(2000)

This tells the Microbit to wait for 2000 milliseconds.  2000 milliseconds are 2 seconds.  What would happen if this line wasn’t in the program?

In the MicroPython editor, click the Download button and transfer the .hex file to your Microbit to run the program.  See if it works.  Try changing the code – for example try changing the image it displays or the text that is displayed.

Caution!

Programming languages are precise and don’t like errors.  Imagine a programming language as being a really strict English teacher.  Python is strict about three things:

  • Spelling
  • Syntax
  • Indentation

If you mis-spell any programming words (or keywords as they are known) then Python will give you an error.

Syntax is essentially the grammar of a programming language.  If you get a syntax error, make sure you’ve put any punctuation symbols in the correct place.

Indentation is where you move text in a few spaces.  Python takes meaning from indentation – for example in the demo program, all of the instructions inside the loop are indented.  This helps Python work out which instructions should be repeated.

The most important thing is DON’T PANIC!  Look at any error messages and see if you can find the error.  Professional programmers make hundreds of mistakes a day – it’s fine to make a mistake as long as you can find the problem!

Main – DIY Images

Of course, you want to make your own image to display on the micro:bit, right?

That’s easy.

Each LED pixel on the physical display can be set to one of ten values. If a pixel is set to 0 (zero) then it’s off. It literally has zero brightness. However, if it is set to 9 then it is at its brightest level. The values 1 to 8 represent the brightness levels between off (0) and full on (9).

Armed with this information, it’s possible to create a new image like this:

from microbit import *

boat = Image("05050:"
             "05050:"
             "05050:"
             "99999:"
             "09990")

display.show(boat)

(When run, the device should display an old-fashioned “Blue Peter” sailing ship with the masts dimmer than the boat’s hull.)

Have you figured out how to draw a picture? Have you noticed that each line of the physical display is represented by a line of numbers ending in : and enclosed between " double quotes? Each number specifies a brightness. There are five lines of five numbers so it’s possible to specify the individual brightness for each of the five pixels on each of the five lines on the physical display. That’s how to create a new image.

Simple!

In fact, you don’t need to write this over several lines. If you think you can keep track of each line, you can rewrite it like this:

boat = Image("05050:05050:05050:99999:09990")

Main – Animation

We can tell MicroPython to animate a list of images. Luckily we have a couple of lists of images already built in. They’re called Image.ALL_CLOCKS and Image.ALL_ARROWS:

from microbit import *

display.show(Image.ALL_CLOCKS, loop=True, delay=100)

As with a single image, we use display.show to show it on the device’s display. However, we tell MicroPython to use Image.ALL_CLOCKS and it understands that it needs to show each image in the list, one after the other. We also tell MicroPython to keep looping over the list of images (so the animation lasts forever) by saying loop=True. Furthermore, we tell it that we want the delay between each image to be only 100 milliseconds (a tenth of a second) with the argument delay=100.

Can you work out how to animate over the Image.ALL_ARROWS list? How do you avoid looping forever (hint: the opposite of True is False although the default value for loop is False)? Can you change the speed of the animation?

Finally, here’s how to create your own animation. In my example I’m going to make my boat sink into the bottom of the display:

from microbit import *

boat1 = Image("05050:"
              "05050:"
              "05050:"
              "99999:"
              "09990")

boat2 = Image("00000:"
              "05050:"
              "05050:"
              "05050:"
              "99999")

boat3 = Image("00000:"
              "00000:"
              "05050:"
              "05050:"
              "05050")

boat4 = Image("00000:"
              "00000:"
              "00000:"
              "05050:"
              "05050")

boat5 = Image("00000:"
              "00000:"
              "00000:"
              "00000:"
              "05050")

boat6 = Image("00000:"
              "00000:"
              "00000:"
              "00000:"
              "00000")

all_boats = [boat1, boat2, boat3, boat4, boat5, boat6]
display.show(all_boats, delay=200)

Here’s how the code works:

  • I create six boat images in exactly the same way I described above.
  • Then, I put them all into a list that I call all_boats.
  • Finally, I ask display.show to animate the list with a delay of 200 milliseconds.
  • Since I’ve not set loop=True the boat will only sink once (thus making my animation scientifically accurate). 🙂

What would you animate? Can you animate special effects? How would you make an image fade out and then fade in again?

Main – More Challenges

Click the links below to try out some more challenges:

Using buttons in Python
Detecting movement in Python
Recognising gestures in Python