C2CMC 2017 – Day 24

Phew, I made it! It’s day 24 of my ‘Countdown to Christmas Makes Challenge 2017’. So, with today being Christmas Eve I thought it would be rather apt to end my challenge with the wonderful festive tune, ‘We Wish You a Merry Christmas’, programmed in MicroPython for the BBC micro:bit. Now, on that note and along with the lyrics, I just want to say “Merry Christmas and a Happy New Year everyone!”.

“We Wish You a Merry Christmas” Tune

The tune was quite easy to transpose from sheet music into MicroPython. The version I chose, has a tempo of 120, covers 2 octaves (4 & 5) and includes a couple of sharp (#) notes.

Instructions

Firstly, you just need to copy (cut & paste) the code below into your favorite BBC micro:bit MicroPython (IDE) editor (I personally use Mu, or you can use the Official Online Editor).

Once you’ve copied the code into the editor you need flash it to your BBC micro:bit. If using the Official Online Editor, you need to click the Download button to save the ‘.hex’ file to your computer then copy that file to your BBC micro:bit. Or, if using Mu just click the Flash button.

To start the music, press the A button on your BBC micro:bit and it will play once. To play again, press the button once more. If you would like the music to play continuously just remove or comment out, line 24; if button_a.is_pressed():

For details on how to connect headphones to your BBC micro:bit, check out Day 1 of my ‘Countdown to Christmas Makes Challenge 2017’.

MicroPython Code

# Project: "We Wish You A Merry Christmas" tune for the BBC micro:bit
#  Author: @KariLawler (Twitter)
# Created: 24/Dec/2017
#  Edited: 27/Dec/2017
# Version: 1.1
from microbit import *
import music

tempo = 120
we_wish_you_a_merry_christmas = [
    'd4:4',
    'g:4', 'g:2', 'a:2', 'g:2', 'f#:2',
    'e:4', 'e:4', 'e:4',
    'a:4', 'a:2', 'b:2', 'a:2', 'g:2',
    'f#:4', 'd:4', 'd:4',
    'b:4', 'b:2', 'c5:2', 'b4:2', 'a:2',
    'g:4', 'e:4', 'd:2', 'd:2',
    'e:4', 'a:4', 'f#:4',
    'g:8'
]

music.set_tempo(bpm=tempo)
while True:
    if button_a.is_pressed():
        for note in we_wish_you_a_merry_christmas:
            if note[0] != "r":  # Display letter notes, but not rest notes.
                delay_sum = int((int(note.split(':')[1]) * (60000 / (tempo * 4))) - 100)
                display.show(note[0].upper(), delay=delay_sum, wait=False, clear=True)
            music.play(note)

Leave a Reply