Countdown

{Introduction }

πŸŽ‡3β€¦πŸŽ‡2β€¦πŸŽ‡1…
πŸŽ†GO!πŸŽ†

Let’s create a musical countdown using the new micro:bit with sound!

Countdown banner message

{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: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]|| 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:1/4 beat||.

for (let index = 0; index <= 2; index++) {
    // @highlight
    music.playTone(262, music.beat(BeatFraction.Quarter))
}

{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:for [index] from 0 to [2]|| loop condition, click and drag out the red ||variables:index|| variable.
β–Ί Use the ||variables:index|| that you dragged out to replace the 0 in ||basic:show number [0]||.

for (let index = 0; index <= 2; index++) {
    music.playTone(262, music.beat(BeatFraction.Quarter))
    // @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:index|| in your ||basic: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:index|| variable and snap it in to the right side of your ||math:[0] - [0]|| operator.
πŸ’‘ Can’t find ||variables:index||? Try moving your ||basic:on start|| container to see if ||variables: index|| is hiding behind it!
β–Ί Set the left side of your ||math:[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.playTone(262, music.beat(BeatFraction.Quarter))
    // @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:on start|| container.
β–Ί Replace Hello! with the word GO!

for (let index = 0; index <= 2; index++) {
    music.playTone(262, music.beat(BeatFraction.Quarter))
    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]|| and place it above your ||basic:show string ["GO!"]|| block and below your ||loops:for|| loop.
πŸ’‘ This will let your micro:bit play the sound and show GO! at the same time.
β–Ί Set the ||music:tone|| to be Middle G.
πŸ’‘ Middle G is also tone 392.

for (let index = 0; index <= 2; index++) {
    music.playTone(262, music.beat(BeatFraction.Quarter))
    basic.showNumber(3 - index)
}
// @highlight
music.playTone(392, music.beat(BeatFraction.Whole))
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.playTone(262, music.beat(BeatFraction.Quarter))
    basic.showNumber(3 - index)
}
music.playTone(392, music.beat(BeatFraction.Whole))
basic.showString("GO!")
# BlocksExistValidator
//