Countdown
{Introduction }
π3β¦π2β¦π1β¦
πGO!π
Letβs create a musical countdown using the new micro:bit with sound!

{Setting up the loop}
Weβll begin by using a for loop to recreate the same sound 3 times.
β From the ||loops:Loops|| category in your toolbox, find the ||loops:for [index] from 0 to [4]|| loop and add it to your ||basic(noclick):on start|| container.
β Change your loop to count from 0 to 2.
π‘ This means the loop will count 0-1-2 instead of what we want, which is 3-2-1. We will worry about this later!
// @highlight
for (let index = 0; index <= 2; index++) {
}
{Play music}
β From ||music:Music||, grab ||music:play tone [Middle C] for [1 beat] [until done]|| and snap it into your empty for loop.
π‘ Your simulator might start playing music. You can mute it if distracting.
β 1 beat is a little long. Use the dropdown to set the tone to play for ||music(noclick):1/4 beat||.
for (let index = 0; index <= 2; index++) {
// @highlight
music.play(music.tonePlayable(262, music.beat(BeatFraction.Quarter)), music.PlaybackMode.UntilDone)
}
{Showing a number}
With every tone, we also want to display our countdown.
β From ||basic:Basic||, find ||basic:show number [0]|| and snap it in at the bottom of your for loop.
β From your ||loops(noclick):for [index] from 0 to [2]|| loop condition, click and drag out the red ||variables(noclick):index|| variable.
β Use the ||variables(noclick):index|| that you dragged out to replace the 0 in ||basic(noclick):show number [0]||.
for (let index = 0; index <= 2; index++) {
music.play(music.tonePlayable(262, music.beat(BeatFraction.Quarter)), music.PlaybackMode.UntilDone)
// @highlight
basic.showNumber(index)
}
{Inverting the number}
If you take a look at your simulator, youβll notice the micro:bit flashing 0-1-2. We want it to say 3-2-1! Letβs learn a trick to change that.
β From the ||math:Math|| category, snap ||math:[0] - [0]|| in to replace ||variables(noclick):index|| in your ||basic(noclick):show number [index]|| block.
π‘ You should now have a greyed out index variable in your workspace. Weβll use that in the next step.
β Pick up the greyed out ||variables(noclick):index|| variable and snap it in to the right side of your ||math:[0] - [0]|| operator.
π‘ Canβt find ||variables(noclick):index||? Try moving your ||basic(noclick):on start|| container to see if ||variables(noclick):index|| is hiding behind it!
β Set the left side of your ||math(noclick):[0]-[index]|| operator to 3.
π‘ Why does this work? Every time we loop, our index variable will grow by 1 and our micro:bit will output: 3-0 = 3 β‘οΈ 3-1 = 2 β‘οΈ 3-2 = 1!
for (let index = 0; index <= 2; index++) {
music.play(music.tonePlayable(262, music.beat(BeatFraction.Quarter)), music.PlaybackMode.UntilDone)
// @highlight
basic.showNumber(3 - index)
}
{Printing βGO!β}
β From ||basic:Basic||, grab ||basic:show string ["Hello!"]|| and snap it into the very bottom of your ||basic(noclick):on start|| container.
β Replace Hello! with the word GO!
for (let index = 0; index <= 2; index++) {
music.play(music.tonePlayable(262, music.beat(BeatFraction.Quarter)), music.PlaybackMode.UntilDone)
basic.showNumber(3 - index)
}
// @highlight
basic.showString("GO!")
{Adding a βGO!β noise}
β From the ||music:Music|| category, grab ||music:play tone [Middle C] for [1 beat] [until done]|| and place it above your ||basic(noclick):show string ["GO!"]|| block and below your ||loops(noclick):for|| loop.
π‘ This will let your micro:bit play the sound and show GO! at the same time.
β Set the ||music(noclick):tone|| to be Middle G.
π‘ Middle G is also tone 392.
for (let index = 0; index <= 2; index++) {
music.play(music.tonePlayable(262, music.beat(BeatFraction.Quarter)), music.PlaybackMode.UntilDone)
basic.showNumber(3 - index)
}
// @highlight
music.play(music.tonePlayable(392, music.beat(BeatFraction.Whole)), music.PlaybackMode.UntilDone)
basic.showString("GO!")
{Testing in the simulator}
Make sure your speakers are on and check out the simulator!
If you have a micro:bit with sound (the one with the shiny gold logo at the top), no need to plug in an external speaker - just download this code and try it out!
for (let index = 0; index <= 2; index++) {
music.play(music.tonePlayable(262, music.beat(BeatFraction.Quarter)), music.PlaybackMode.UntilDone)
basic.showNumber(3 - index)
}
music.play(music.tonePlayable(392, music.beat(BeatFraction.Whole)), music.PlaybackMode.UntilDone)
basic.showString("GO!")
# BlocksExistValidator
//