Get in the festive mood by creating a micro:bit Christmas tree bauble.
Welcome to the first of the twelve blogs of Christmas!
On the first day of Christmas my true love sent to me
a micro:bit in a christmas tree
Over the Christmas season I’m going to be posting twelve festive blogs at random intervals, and by the end we’ll have a complete ‘twelve days of Christmas’ song. Let’s get started!
What you’ll need:
- A Micro:bit
- A Micro:bit battery pack
- Two elastic bands
- A piece of string
- A laptop or tablet with an internet connection
Step one: Programming
Go to microbit.org and open up the Python editor. It doesn’t matter if you haven’t used it before, as we’re going to be making quite simple code. Here’s the code that should come up:
# Add your Python code here. E.g. from microbit import * while True: display.scroll('Hello, World!') display.show(Image.HEART) sleep(2000)
We’re going to create new code, so get rid of it all.
from microbit import * #import everything from 'microbit' star1 = Image("00900:" #Create an image called 'star1' "00900:" "99999:" "00900:" "00900") star2 = Image("90009:" #Create an image called 'star2' "09090:" "00900:" "09090:" "90009")
Firstly, we have to import everything from ‘microbit’, so that python knows how to control the micro:bit and what the different commands do. Everything behind a tag (#) is just a comment, and isn’t part of the code. Next we create two images, one called star1 and the other called star2. The numbers are the brightness of the LEDs on the micro:bit. 0 if off and 9 is full brightness. As you can see, there is a number for every LED on the display.
while True: #forever loop display.show(star1) #show the image we created earlier called 'star1' sleep(1000) #sleep for 1 second display.show(star2) #show the image 'star2' sleep(1000) #sleep for 1 second
Next we create a forever loop (‘while True’). Everything that has a gap (four spaces big) after ‘while True’ will repeat forever. Then we display the image we created called ‘star1’, sleep for one second (1000 miliseconds), display our ‘star2’ image and sleep for another second. This keeps repeating forever. Here’s the code all together:
from microbit import * star1 = Image("00900:" "00900:" "99999:" "00900:" "00900") star2 = Image("90009:" "09090:" "00900:" "09090:" "90009") while True: display.show(star1) sleep(1000) display.show(star2) sleep(1000)
Download your code and copy the .hex file from your downloads folder to you Micro:bit.
Part 2: Turning your micro:bit into a Christmas bauble
Now that you’ve put your code onto your micro:bit, it’s time to connect the string and battery pack so that you can hang it on your tree. Firstly, wrap the two elastic bands around your micro:bit on either side of the display. Then slot your battery pack inside the elastic bands on the back of the micro:bit to attach it. Then thread the string through the top of the elastic bands and tie a knot. The back should look like this:
Now all you have to do is plug in the battery pack and use the string to hang it on your Christmas tree. Enjoy!
Note: Don’t leave the micro:bit on all the time, or the battery will run out!