pbaumgarten.com

Microbit lesson 5: Lists with music

Video

Lesson overview

This lesson:

Lists

Lists are an important and very useful tool in programming. They are a variable that can store more than one value, and are used when we have lots of values that we want to treat in a similar manner.

A simple example on the Microbit is a list of different items to show on the display:

from microbit import *

items = ["H", "E", "L", "L", "O", Image.HAPPY, " ", "B", "Y", "E", Image.SAD]
for letter in items:
    display.show(letter)
    sleep(500)

Using lists for calculations

Rather than writing lots of display.show() lines, we were able to just use a single list of items. The magic happens with the for command. This will look at the first item inside items, put it in letter and run the indented lines. Once reaching the end, it will look at the items list and put the value of the second item into the variable letter and re-run the indentded lines. It will keep repeating this process until it reaches the end of the items list.

Another example would be to use a list to act as a calculator and total a set of numbers.

from microbit import *

numbers = [5, 7, 10, 15, 42]
total = 0
for number in numbers:
    display.scroll("+")
    display.scroll(number)
    total = total + number
display.scroll("=")
display.scroll(total)

Using lists to play music

We will practice lists by using them to play music on the microbit!

To do so, we will create a list of musical notes we want the microbit to play.

To wire the Microbit directly to a set of earphones, you want to use clips connected to PIN0 and GND, where the other end looks like this…

For instance, the tune for “Happy Birthday” looks like…

from microbit import *

music.set_tempo(bpm=240)
song = [
    "C", "C", "D", "C", "F", "E:8", "r", 
    "C", "C", "D", "C", "G", "F:8", "r", 
    "C", "C", "C5", "A", "F", "E", "D", "r", 
    "A#", "A#", "A", "F", "G", "F:8"
    ]
for note in song:
    display.show(note[0], wait=False, delay=0)
    music.play(note)

Meme song

For the musically inclined amongst you who wish to experiment with this, allow me to explain the method of detailing individual notes…

For a more complete example, here is a meme song for you…

from microbit import *
import music

music.set_tempo(bpm=168)
take_on_me = [
    "f#:2", "f#:4", "d", "b3", "r:2", "b3:4", "r:2", "e4:2", "r:2", "e:2", "r:2",
    "e:2", "r:2", "g#:2", "g#:4", "a", "b", "a:2", "a:2", "a:2", "e:4", "d", 
    "f#:2", "r:2", "f#:2", "r:2", "f#:2", "e:4", "e", "f#", "e",
    # Repeat the same 3 lines (just do a copy-and-paste)
    "f#:2", "f#:4", "d", "b3", "r:2", "b3:4", "r:2", "e4:2", "r:2", "e:2", "r:2",
    "e:2", "r:2", "g#:2", "g#:4", "a", "b", "a:2", "a:2", "a:2", "e:4", "d", 
    "f#:2", "r:2", "f#:2", "r:2", "f#:2", "e:4", "e", "f#", "e",
]

display.scroll("Take on me!", delay=70)
for note in take_on_me:
    display.show(note[0], wait=False, delay=0)
    music.play(note)

Activity

Find a song you want your microbit to play, and have a go at making it work.

If you are musically inclined, I’m happy for you to pick your own song and convert the notes as required.

For those who are not musically, here are a couple of websites with song notes written out you can use.

Please note these websites have not written their music for the purposes of programming microbits. You will have to convert their system into what the microbit requires.

For easy-letter-notes.com: That the colour of the note on the website indicates the octave:

For Pianoletternotes.blogspot.com: The codes work as follows: