For my last Code Club of the term we embraced Christmas and had a bash at coding “The 12 Days of Christmas” on our Micro:bits. Using the MakeCode blocks, the code can gets a bit busy, but stick with it and you’ll have it as an annoying ear worm in no time!
♪ On the 1st Day of Christmas my true love gave to me ♪
This is the opening line of the song, so let’s start with that. Go to makecode.microbit.org and create a new project called “12 days” (or something similar).
To make the code easier to manage we be using the Functions blocks to break it down into smaller chunks. Go to the Functions draw under Advanced and click Make a Function. A dialog will appear, change the name “doSomething” to “intro” and click Done. A new blue function block called intro will appear.
Now to add the notes to play the melody
Finally, add a call intro block from the Functions draw to a on button A press block from Inputs
Press the A button on the onscreen virtual Micro:bit and/or download to your physical one and test. Note; if you are using a physical Micro:bit you’ll need to attach headphones or a speaker to pin 0 (and ground). This can be done with crocodile clips like it shows you on screen, or via an add-on board like the Music Box from 4tronix, or the Noise:bit from Pimoroni to name two.
♪ A partridge in a pear tree ♪
Next comes the Partridge. Same as before, create a new function, but this time call it “partridge”. Add some notes to the function, add the call partridge block to the on button A pressed block, and see how it sounds
If at anytime it doesn’t sound quite right, check the notes and beat values are correct.
♪ 2 Turtle Doves / 3 French Hens / 4 Calling Birds ♪
By now, you’ve hopefully spotted the pattern? That’s right, a new function, this one we’ll call “item234” as the notes/melody for the 2nd, 3rd and 4th verses are the same. This also means we can reuse and repeat them depending on how many days we’re on.
Speaking of days, this is where the code starts to get a little more involved. We need a new Variable which we’ll call “days”, and will be used to work out how many times this function needs to be run/played.
You can now see how the variable is being used. The item234 function only needs to be run for days 2, 3, and 4 (the first day is the partridge). By taking one away from days, on the first day the repeat block is told to repeat zero times (1 – 1 = 0) , so it doesn’t run.
There is also an additional Middle A played, this is because for 2, 3, 4 the song has an extra “and” before the partridge section.
♪ 5 GOLDEN RINGS!!!! ♪
After “5 golden rings” is sung, the melody for 4, 3, 2 changes. So a new function is needed
Now to change the code to include these new functions
Now when button A is pressed, if days is greater than 4 (so 5 and up) it plays “5 Golden Rings” and so on.
To test it, change the set days block in on start to 5.
♪ 6 Geese a Laying, etc ♪
Nearly there!! from 12 Drummers Drumming, down to 6 Geese a Laying the melody is the same, so it can be repeated just like we have done for 2, 3, and 4.
One final update to the main code in on Button A
This time the repeat block repeats days minus 5, this means that when days = 6, it repeats once (6 – 5 = 1), but if days is 5 or less then the repeat block will not run as it is asked to repeat zero (or less, which is treated as zero) times.
Change the set days block in on start to 6 or more (up to 12 of course) and see if it works.
And that’s it…… but having to change the number of days each time and send it to the Micro:bit isn’t very helpful, so……
Changing the Days
Adding this code will allow you to change the number of days by pressing the B button. When it gets to 12, it will reset to 1 again. Make sure you change the set days block in on start to 1.
A challenge for anyone who wants to take this further; change the code so it plays each verse from the first day through to the twelfth day one after the other……. Good Luck!