Micro:Bit Workshop Series

Cymraeg

Aberystwyth University's Micro:Bit Workshop Series

We have adapted the material created for our Robotics Club for anyone to work through at home or in school.

Each of the below sessions include a video lesson from one of our staff and a set of exercises to assess your understanding. As this page is for independent learners, answers are included.

For teachers and group leaders, we hope to soon make these resources into packs freely available for schools and STEM clubs to use in our TES Shop.

Please select a session heading to get started.

Video Lesson:

Exercises:

We have created three levels (bronze, silver, and gold) of challenge for learners to start investigating and creating programs in Microsoft Makecode.

All learners should start with the bronze level and work their way up as far as they can.

Click on each challenge heading to expand.


Create a program (in Makecode) that does the following:

  1. When the program starts, have the LEDs show an image of your choice.

    Tip: To delete blocks press delete on your keyboard or drag them back into the menu.


    • You can use an existing image with the 'show icon' block or create your own image using the 'show leds' block.
    • If you are using the 'show leds' block, you can click on the squares to select if they are lit or not.
    • As you only want this to show up when the program starts, you should put it inside the 'on start' block.

    If you are still stuck, use the answer button below.

    Using the 'show leds' block:

    Inside the 'on start' block is a 'show leds' block with lights selected in the shape of a sun. The 'forever' block remains empty.

    Using the 'show icon' block:

    Inside the 'on start' block is a 'show icon' block with the icon of a ghost selected. The 'forever' block remains empty.

  2. After the start-up is complete, have your program play an animation over and over until you stop the program.

    Tip: An animation is a series of images that makes an object look like it is moving.


    • You will need to use the 'forever' block for this.
    • A basic animation could be produced using only two images.
    • As an example, try using the show icon block for the large heart followed by the small heart. This will create an animation that looks like a beating heart.
    • A series of images making an animation do not need pauses unless you wish to slow the motion down.
    • You can use the 'show leds', 'show icon', or a mix of the two.

    If you are still stuck, use the answer button below.


    The 'forever' block now has three 'show leds' blocks. The first is a single light on the far left, the second shows the same light plus the one in the centre, and the third shows the same as the second plus a light on the far right.

    This shows that you don't need to use that many LEDs to produce an animation.


  3. Have a different image appear on your Micro:Bit when button A is pressed.

    Tip: Button A is an input so look for the menu that will have the block you need.

    Tip: To test the program, button A does not mean the A button on your keyboard. It means the A button on the Micro:Bit inside the simulator which can be clicked on with your mouse cursor.


    • The block you will need is in the Input block menu.
    • This block does not go inside the 'on start' or 'forever' blocks. Instead, it starts a new section of code blocks.

    If you are still stuck, use the answer button below.


    There is now a pink/purple 'when button A pressed' block in the program containing a 'show icon' block with a giraffe image selected.

  4. Add sound or music to your start-up sequence.

    Tip: Check your computer volume is low before adding sound. You can adjust it during testing to a level of your choosing.

    Tip: If using music, keep the tune short and only play once, or you'll have a long wait for this to complete during each test.

    Tip: If you don't want to create your own tune, scroll down the Music menu to find ready-made melodies and sounds.


    • If using the 'play melody' block from the top of the Music menu, you can create your own tune by clicking on the musical note symbol in the block, then highlight the boxes to produce different notes.
    • If using the 'play melody' block with preset tunes, change the 'in background' option to 'until done' using the drop-down arrow.

    If you are still stuck, use the answer button below.


    Using your own tune:

    There is now a 'play melody at tempo 120 (bpm) until done' block under the 'show icon' block in the 'on start' section.

    Using preset melody:

    There is now a 'play melody funk until done' block under the 'show icon' block in the 'on start' section.

    Using sound effect:

    There is now a 'play happy until done' block under the 'show icon' block in the 'on start' section.

  5. Have the Micro:Bit react when it hears a loud sound.

    Tip: If you remember from the video, the microphone acts as a sound sensor and is an input device.

    Tip: You could use an image, text, music, or sound as the reaction.

    Tip: A loud sound in this program is any value over 170.


    • The block you need is in the Input blocks menu.
    • Look under micro:bit (V2). It is in this section because there are now two versions of the BBC Micro:Bit and only version 2 has a microphone.
    • You will need to re-watch the video if you are unsure how to create a loud sound in the simulator.

    If you are still stuck, use the answer button below.

    This solution has the Micro:Bit show a message when it hears a loud sound.

    A new 'on loud sound' block has been added containing a 'show string 'SHHH!'' block.



Inside the same program, create the following:

  1. At the end of your start-up sequence clear the screen and add a welcome message.

    Tip: Keep the message short as you'll have to wait for it to complete every time you test your program.

    Tip: Both blocks needed for this are in the Basic menu.


    • In programming we use the word string to describe any series of symbols.
    • You will need to find the 'show string' block and then you can change the word "hello" to a word/message of your choosing.

    If you are still stuck, use the answer button below.


    A 'clear screen' block has been added at the end of the 'on start' section along with a 'show string Hi' block below that.

  2. Add a message into your forever loop that says 'Need Input'.

    Tip: Think of this as a waiting screen.


    • You will use the same block as we used for the welcome message above.

    If you are still stuck, use the answer button below.


    Added a 'show string' block with the message 'Need Input' to the bottom of the 'forever' block.

  3. Have the Micro:Bit display different images for when the B button is pressed and when both buttons are pressed.

    Tip: The 'on button A pressed' block has a drop-down arrow on it that allows you to change the A to B or A+B.


    • Remember that the button blocks can be found in the Input menu.
    • You will need three of these blocks in your program, one for each button combination.
    • To test this, the simulator should now have a new button labelled A+B, this allows you to mimic the effect of pressing both buttons using the mouse.

    If you are still stuck, use the answer button below.


    New 'on button B pressed' and 'on button A+B pressed' blocks have been added. Both contain a different 'show icon' block.

  4. Add different sound effects depending on which button is pressed.

    Tip: You may start to see some unwanted behaviours in your tests as we add more blocks to the button programs. We shall cover why and how to avoid this later in the series.


    • If uncertain, refer to step 4 in the bronze challenge.

    If you are still stuck, use the answer button below.


    Different 'play sound until done' blocks have been added to each of the button pressed sections.

  5. Add a reaction for when the Micro:Bit detects it is quiet.

    Tip: A lot of blocks include drop down menus that allow you to change key words.

    Tip: For the Micro:Bit, quiet is any noise level below 90.


    • Remember that sound detection is an input.
    • The 'on loud sound' block can be changed.
    • A reaction could be a sound, image, icon, or message.

    If you are still stuck, use the answer button below.


    An 'on quiet sound' section has been added to the program containing a 'show leds' block with lights illuminated to show a smiley face.



Continue building your program by adding the following:

  1. Replace the image on start-up with an animation.

    Tip: You could use repeating images, or you may want to use a block from the Loops menu to help with this.


    • There is a 'repeat 4 times' block in the Loops menu that can be set to any number of repeats. This means you only need to use each image once.

    If you are still stuck, use the answer button below.


    The image in the 'on start' block has been replaced with a 'repeat 4 times' block containing two different icons (large and small heart)

    This program's start-up animation is a beating heart.


  2. Change your forever loop animation to show a person or animal moving onto the screen.

    Tip: Choose or create a full image of your person/animal first. Then you can create the animation more easily.


    • Have your animal/person appear from the left of your screen one column of lights at a time and then move them all the way across the screen.

    If you are still stuck, use the answer button below.


    The forever loop animation now contains a series of 'show leds' blocks which produce the animation of a snake coming in from the left until it's fully visible.

  3. Have the Micro:Bit react differently when tilted left and when tilted right.

    Tip: Some blocks have drop down menus to change a key word.

    Tip: The Micro:Bit is using its accelerometer for this which is a movement sensor. Is this an input or an output?


    • You will need to use a block from the Input menu.
    • Is there a movement-based block you can use? What options does it have for the key word?

    If you are still stuck, use the answer button below.


    An 'on tilt left' and a 'on tilt right' sections have been added, each containing a 'show leds' block which shows an arrow pointing in the same direction as the tilt.

  4. Add sound effects to being tilted left and right.


    • This is the same as when we added sound effects to the button inputs.

    If you are still stuck, use the answer button below.


    Different 'play sound until done' blocks have been added to both the tilt input sections.

  5. At the end of your forever loop replace the 'show string Need Input' block with a clear screen block and then show the value of the sound level instead.

    Tip: The sound level on a Micro:Bit is measured as a number.

    Tip: You will need to merge two blocks together to make this instruction.


    • There is a 'show number' block in the Basic menu.
    • The sound sensor is an input device, so you will need to look in the Input menu for the block that will fit inside your 'show number' block.

    If you are still stuck, use the answer button below.


    The 'show string Need Input' in the forever loop has been replaced with a 'clear screen' block followed by a block made up of 'show number' and 'sound level'.

Video Lesson:

Exercises:

For this session we shall be creating a program to turn your BBC Micro:Bit into a virtual pet.

As with all our exercises, they are designed for learners to complete a level before moving to the next. Otherwise, you may miss information or skip a crucial step that stops your program from working.

Click on each challenge heading to expand.


Start a new Micro:Bit program on Makecode and work your way through the following stages.

  1. Have a message and/or image appear on start to help identify the program.

    Tip: Remember that the clear screen block can be used to create a break between an image and a message.


    The 'on start' loop containing a 'show string 'My Pet'' block followed by 'clear screen' and then 'show icon (snake)' blocks.

  2. Create a little animation of your pet waiting inside the forever loop.

    Tip: You may be able to use one you created in Session One.


    • Remember, you only need two images to make an animation.
    • You can use the pre-made icons to get you started.
    • The more images you use, the longer the forever loop will take to complete - this could cause problems as we build the program.

    If you are still stuck, use the answer button below.


    Now we have a forever loop containing a 'show icon(snake)' block followed by a 'show leds' block with the same snake but with its tail in a different position.

  3. Use one of the inputs (like button A) to feed your pet - have it show an image of a food item on the screen.


    • You may need to clear the screen before showing your food image.
    • Remember, the button A control block is in the Input menu.

    If you are still stuck, use the answer button below.

    This answer includes my attempt at drawing a burger for my pet.

    A 'on button A pressed' control block has been added containing a 'show leds' block with an attempt at a drawing of a burger.

  4. Have an animation to show the pet eating.

    Important: You may start to see some unwanted behaviours with your animations clashing. By using our new if statement blocks, we can correct the issue and have the whole program stored inside a single forever loop. The below image shows how to include button A as a condition in an if statement inside the forever loop.

    A forever loop containing an 'if button A is pressed then' block that holds a 'show leds' block.

    Please note: The longer your forever loop, the more time it will take for the program to react to an input. You may need to hold down the inputs for longer to register.

    Remember: You should only have one forever loop in your program.

    Tip: Use the else section on this if statement block as a new home for your pet waiting animation.


    • You can animate either the pet or the food for this effect.
    • A repeat block from the Loops menu will help with the animation.

    If you are still stuck, use the answer button below.


    The forever loop has been completely re-written. It now reads: forever, if button A is pressed then {repeat 3 times {show leds (burger), show led (partial burger)}}, else {show icon (snake), show leds (snake with different tail position)}.

  5. Have your pet react badly (you can use an image, animation, and/or sound) when it hears a loud noise.

    Tip: Instead of using the 'on loud sound' block, use an else if statement inside your forever loop for when the sound level is > 175.


    • You can expand an if statement by clicking on the plus sign. You can do this for as many else ifs as you need.
    • You can find the greater than comparison block in the Logic menu.
    • To test your program, use the new microphone volume bar on your simulated Micro:Bit to adjust the value. When you go above 175 you should see the program - there might be a slight delay.

    If you are still stuck, use the answer button below.


    The forever loop now has an 'else if sound level > 175 then' statement which contains a 'clear screen' and 'show string 'SHH!'' block.



Continuing with the same program, continue with the below additions.

  1. Have your pet fall asleep when it gets dark (use less than or equal to 70 for this value).

    Tip: You can have as many else if statements as you need in a Micro:Bit program.


    • Use the plus button at the bottom of your if statement to add another else if statement to your program.
    • Check which symbol you are using for the comparison. It should look like ≤.
    • You could just use a string of 'zzz' to indicate your pet has fallen to sleep.
    • Don't forget to clear the screen first.

    If you are still stuck, use the answer button below.


    A new 'else if light level ≤ 70 then' section containing a 'clear screen' and a 'show string 'zzz'' block.

  2. Create a variable for 'fullness' which goes up by one every time your pet is fed.

    Tip: It is always good practise to set a new variable to zero on start.


    • There is a change variable by 1 block which does what we need.
    • Which section of the code does it need to go into?
    • Make sure you do not put it inside an animation's repeat loop.

    If you are still stuck, use the answer button below.


    There is now a 'set fullness to 0' block inside the on start code and a 'change fullness by 1' block in the 'if button A is pressed then' statement after the repeat block for the eating animation.

  3. Use an input (perhaps button B) which will show the user the value of 'fullness'.


    • You can put a variable inside a show string block.

    If you are still stuck, use the answer button below.


    An 'else if button B is pressed then' statement containing a 'show string fullness' block.

  4. Have your pet react happily to bright light (use greater than 200 for this value).


    • If you've chosen to do an animation, don't forget about the repeat block in the Loops menu.
    • Check your comparison symbol is >.

    If you are still stuck, use the answer button below.

    I chose to have my snake celebrate bright light with a somersault animation.

    An 'else if light level > 200 then' statement containing a 'repeat 3 times' loop containing a 'show icon (snake)' and three 'show leds' blocks that create my somersaulting snake animation.

  5. Have your pet make a noise to get attention if the sound level is less than or equal to 50.

    Tip: Have a pause block (at least 500 milliseconds) after your sound has played - trust me.


    • Make sure that whatever noise you've chosen to use in the program only plays until done or once.

    If you are still stuck, use the answer button below.


    An 'else if sound level ≤ 50 then' statement containing a 'play hello until done' block followed by one that says 'pause (ms) 500'.



Now, let us build up the program further with the following tasks.

  1. If your pet is asleep when it hears a loud sound, it must react more angrily/scared/surprised than when it is awake.

    Tip: You can put if statements inside each other, making a stacked if statement.

    Important: Your program will work its way down the list of if and else if statements in the forever loop, it will run the first bit of code where the conditions are met, and skip the rest.

    Think of it like a check list where once something has been checked you return to the top of the list and start again.

    This means that you need to check which of the conditions (loud sound or darkness) comes first in your program and insert the new if statement into that one.



    • Use an if and else statement for this to determine which loud sound reaction is shown.
    • As we want to clear the screen before both options, we can just use this command once before the new if/else statement.
    • Alternatively, you could look at using advanced logic blocks that allow multiple conditions in an if/else if statement.

    If you are still stuck, use the answer button below.

    In our answer program, the loud sound condition is above the dark condition, so the new if statement goes there.

    Inside the 'else if sound level > 175 then' we now have a 'clear screen' block followed by an 'if light level ≤ 70 then' which contains 'show string '!?!'', followed by an 'else' containing 'show string 'SHH!'' block.

  2. Your 'fullness' variable needs to have a maximum value of 5.

    Tip: This means you only want to add one to the fullness value when it is less than 5 (or less than and equal to 4).


    • You need an if statement for when the fullness is less than 5 to only add to the value when true.
    • Currently, this does not require an else statement as there is no action for when the if statement is not true.
    • Remember to keep this if statement outside of any repeat loops used for the feeding animation.

    If you are still stuck, use the answer button below.


    Inside our 'if button A is pressed then' statement we have moved the 'change fullness by 1' block inside a new 'if fullness < 5 then' statement.

  3. Instead of showing the user the value of fullness using the input chosen in the silver challenges, have it show a meter level.

    Tip: You can also put else if statements inside another if / else if / else statement.


    • This will require an image for each fullness level to create the effect of a meter/gauge.
    • In this sort of statement list, you want to include an else statement to capture the 0 and/or any unexpected values. This then can act as an error screen for us.

    If you are still stuck, use the answer button below.

    We went for a round meter shape that gets more complete the higher the fullness level.

    Inside our 'else if button B is pressed then' statement we now have an 'if fullness = 1 then' containing a 'show leds' block. This is then followed by else if statements for fullness values 2 through to, and including, 5 each containing a new 'show leds' block with related meter level image. This then ends with an else statement containing a 'show leds' block with the image of a sad face.

  4. The pet should refuse to eat if it is full (fullness = 5).

    Tip: It might help to move the eating animation into the same if statement as when the fullness variable increases, then you can have a different animation for when full in an else statement.


    • Remember that repeat loops are required inside these statements for repeating animations.
    • When building up a program we often have to re-shuffle the locations of code sections to include new features.
    • This requires an else attached to our if fullness < 5 statement.

    If you are still stuck, use the answer button below.


    The feeding animation has now been moved inside of the if fullness < 5 statement (which is inside the if button A is pressed condition). Then an else statement has been added which shows a new animation (a 'show icon' and a 'show leds' block inside a 'repeat 3 times' loop) of the pet refusing to eat.

  5. The fullness value should slowly drop down over time as your pet starts to get hungry again.

    Tip: As this is a background process that will not suffer interference from the forever loop, you can use a separate timer loop to do this.


    • You can find a timer block in the Loops menu.
    • The default time is 500 ms - this is every half a second - you will need to change this (we found a minute - or 60000 ms - to work well for this).
    • We want to take one away from the fullness so we can use the change fullness by block with a -1 instead of 1.
    • We don't want negative values, so we only want to take away from the fullness value if it's over 0 (or greater than and equal to 1).

    If you are still stuck, use the answer button below.


    A 'every 60000 ms' loop containing an 'if fullness ≥ 1 then' statement with a 'change fullness by -1' block inside.





You now have a virtual pet. Here are some ideas of further additions and features you may want to have a go at adding. These are for you to investigate and explore - there are no answers provided.

  • Having the pet show when it is very hungry (fullness ≤ 1).

    Tip: This can be done by adding another else if statement to your forever loop.


  • Adding a happiness level and play inputs into the program. The more you play with the pet, the happier it is.

    Tip: You may want to move the hunger meter to show automatically after feeding to free up button B for this.


  • Setting a maximum value and a cool-down time for the happiness value. How will your pet react when the happiness value drops too low?

    Tip: This will use the same framework as our fullness meter.


  • Have the pet grow over time if it is looked after correctly.

    Tip: You could create a score variable for this and have the pet improve as the score reaches certain goals.



Video Lesson:

Exercises:

For this session we shall be creating a two-player dice duelling game using two BBC Micro:Bits. We recommend using the multi-editor for these exercises. Name the program on the left Player One and the other Player Two.

As with all our exercises, they are designed for learners to complete a level before moving to the next. Otherwise, you may miss information or skip a crucial step that stops your program from working.

Click on each challenge heading to expand.


  1. Have both Micro:Bits programmed to show a random number between 1 and 6 every time it is shaken - like a dice roll.

    Tip: To avoid interference between loops, all outputs need to be inside the forever loop.


    • The video for Session Two included a walk-through on creating a basic dice program.
    • Use a different dice roll variable name for each player - this will reduce confusion later.
    • You only want the dice roll value to start showing after the first roll (so when the variable is no longer 0)


    Player One

    The 'on start' loop contains a 'show leds' block with p1 displayed and a 'set player1Roll to 0' block. The forever loop contains an 'if player1Roll > 0 then' statement with a 'show number player1Roll' inside. There is also an 'on shake' loop which holds a 'set player1Roll to pick random 1 to 6' block.

    Player Two

    The 'on start' loop contains a 'show leds' block with p2 displayed and a 'set player2Roll to 0' block. The forever loop contains an 'if player1Roll > 0 then' statement with a 'show number player2Roll' inside. There is also an 'on shake' loop which holds a 'set player2Roll to pick random 1 to 6' block.

  2. For Player One, have button A show your last roll whilst button B shows the other player's last roll. Do the same for Player Two.

    Tip: Don't forget to set both Micro:Bits into the same radio group so they can talk to each other - this should be done during the start-up sequence.


    • Use if statements inside the forever loop for button pressing, not the separate 'on button A pressed' or ' on button B pressed' blocks.
    • Remember, you can stack if statements inside each other.
    • You can have an else statement that contains nothing, or you can use it to report an error.


    Player One

    New additions to the program: The 'on start' loop has had a 'set player2Roll to 0' and a 'radio set group 138' added. Inside the forever loop, the 'if player1Roll > 0 then' and its contents are now inside an 'if button A is pressed then' statement. An 'else if button B is pressed then' statement has been added containing an 'if player2Roll > 0 then' statement holding a 'show number player2Roll' block. The else statement has been left empty. The 'on shake' loop now has a 'radio send number player1Roll' block added to the end. Finally, there is now an 'on radio received receivedNumber' loop housing a 'set player2Roll to receivedNumber' block.

    Player Two

    New additions to the program: The 'on start' loop has had a 'set player1Roll to 0' and a 'radio set group 138' added. Inside the forever loop, the 'if player2Roll > 0 then' and its contents are now inside an 'if button A is pressed then' statement. An 'else if button B is pressed then' statement has been added containing an 'if player1Roll > 0 then' statement holding a 'show number player1Roll' block. The else statement has been left empty. The 'on shake' loop now has a 'radio send number player2Roll' block added to the end. Finally, there is now an 'on radio received receivedNumber' loop housing a 'set player1Roll to receivedNumber' block.

  3. When either player presses the touch logo, have the screen show the signal strength.


    • The touch logo is only available on Micro:Bit V2 - so check the V2 section of the input menu.
    • This one is hard to test in the simulator because you can only alter the signal strength when it is sending information (in this case when we shake it).


    Player One

    New addition of an 'else if logo is pressed then' statement inside the forever loop containing a 'show number received packet signal strength' block.

    Player Two

    New addition of an 'else if logo is pressed then' statement inside the forever loop containing a 'show number received packet signal strength' block.



  1. Pressing button A and B at the same time should give players the total of both players dice rolls.

    Tip: You will need to have something to check both players have rolled.

    Important: To check against two conditions (in this case, that both players have rolled), you could stack the if statements or you could use an 'and' block.

    Stacked Conditions

    An 'if player1Roll > 0 then' statement holding a 'if player2Roll > 0 then' statement which in turn holds a 'show string' block that reads: This checks both conditions are true for this block

    The 'and' Boolean Operator

    An 'if player1Roll >0 and player2Roll > 0 then' statement holding a 'show string' block that reads: This checks both conditions are true for this block.

    Note: A Boolean value is true of false. A Boolean Operator compares two inputs to produce a single true or false output.

    Tip: The if button A and B pressed statement needs to be put in the forever loop above the if statements for button A and button B.

    Important: When using else if statements, the program goes down the list of conditions and stops at the first one that is true. It will then skip the rest. When using the if button A and B pressed, any conditions checking if either of those buttons is pressed becomes true. So, 'if button A is pressed' will be true, 'if button B is pressed' will be true and 'if button A+B is pressed' will be true. This highlights the importance of the ordering of conditions.



    • There are blocks that do the calculations for us in the Maths menu.
    • You may want to add an else statement to this to show that not all players have rolled yet.


    Player One

    The main if statement in the forever loop has now been changed to read 'if button A+B is pressed then' which contains an 'if player1Roll > 0 and player2Roll > 0 then' statement holding a 'show number player1Roll + player2Roll' block. The button A program has been relocated to an 'else if button A is pressed then' statement.

    Player Two

    The main if statement in the forever loop has now been changed to read 'if button A+B is pressed then' which contains an 'if player2Roll > 0 and player1Roll > 0 then' statement holding a 'show number player2Roll + player1Roll' block. The button A program has been relocated to an 'else if button A is pressed then' statement.

  2. Move the program for button A so as the Micro:Bit will show the roll on the screen automatically, include an else statement for when the dice roll value is zero. This could be a waiting animation, image, or message/instruction.

    Tip: Now you will need pauses to keep values other than the player's own roll on the screen longer.


    • You can move the show player's own roll program into the else statement for this.
    • Use 1 second (1000ms) pauses where needed.
    • You may need to hold down buttons for slightly longer when testing. Especially if you are using a string to show a message as it will not listen for buttons until the message has finished scrolling.
    • Leave the 'else if button A is pressed then' statement empty for now.


    Player One

    'pause (ms) 1000' blocks have been added to the on start loop and after every 'show number' block in the forever loop. The 'if player1Roll > 0 then' statement has had an else statement added containing 'show string Shake' and this has then been relocated from the 'else if button A pressed then' statement to the main else statement inside the forever loop.

    Player Two

    'pause (ms) 1000' blocks have been added to the on start loop and after every 'show number' block in the forever loop. The 'if player2Roll > 0 then' statement has had an else statement added containing 'show string Shake' and this has then been relocated from the 'else if button A pressed then' statement to the main else statement inside the forever loop.

  3. Now use Button A to reset the game.

    Tip: You will need to also send the reset command to the other player's Micro:Bit.


    • You can send a string, such as a word, over the radio group.
    • If using a string for this, you need to use the string comparison block - this is the one with quotation marks inside the value boxes.
    • Alternatively, you could send a different number which triggers the reset.
    • Check you are using the correct receiving blocks for the type of information you are sending.


    Player One

    The 'else if button A is pressed then' statement within the forever loop, now contains 'set player1Roll to 0', 'set player2Roll to 0', and 'radio send string reset' blocks. A new 'on radio received receivedString' loop has been added containing an 'if receivedString = reset then' statement holding 'set player1Roll to 0' and 'set player2Roll to 0' blocks.

    Player Two

    The 'else if button A is pressed then' statement within the forever loop, now contains 'set player2Roll to 0', 'set player1Roll to 0', and 'radio send string reset' blocks. A new 'on radio received receivedString' loop has been added containing an 'if receivedString = reset then' statement holding 'set player2Roll to 0' and 'set player1Roll to 0' blocks.



  1. Let us now have the players duelling. Whichever player rolls the highest number wins. Have the Micro:Bits show whether their player won or lost.

    Tip: You can use the 'and' Boolean operator here to make sure both players have rolled before checking to see who won.


    • You need to work out where to put this in your program.
    • Compare the two players dice rolls to work out which is higher.
    • What happens when the players both roll the same number?
    • You can use images, strings, and/or animations for these.
    • Add pauses where you think they are needed.


    Player One

    Where the program shows the player1Roll value (inside the 'if player1Roll > 0' statement which is in turn inside the main else statement for the forever loop), a 'pause (ms) 1000' block has been added beneath the existing 'show number player1Roll'. Then under this a new if statement has been added: 'if player1Roll > 0 and player2Roll > 0 then'. This then contains a new if, else if and else statement. The if: 'if player1Roll > player2Roll then' holds a 'show icon (happy)'. The else if: 'else if player2Roll > player1Roll then' holds a 'show icon (sad)' block. The else holds a 'show icon (asleep)' block.

    Player Two

    Where the program shows the player2Roll value (inside the 'if player2Roll > 0' statement which is in turn inside the main else statement for the forever loop), a 'pause (ms) 1000' block has been added beneath the existing 'show number player2Roll'. Then under this a new if statement has been added: 'if player2Roll > 0 and player1Roll > 0 then'. This then contains a new if, else if and else statement. The if: 'if player2Roll > player1Roll then' holds a 'show icon (happy)'. The else if: 'else if player1Roll > player2Roll then' holds a 'show icon (sad)' block. The else holds a 'show icon (asleep)' block.

  2. Upgrade the program so both players must roll twice before comparing their totals to determine the winner. Players should not be able to roll more than twice reset.

    Tip: You will need a new variable for the number of dice rolls.


    • Your new counter variable will need to be used in place of all conditions that currently check if the player's own roll value is over 0.
    • Have a message or image come up if a player tries to roll more than twice - this makes it easier to test.
    • The use and changing of the counter variable can be done in the 'on shake' loop.
    • You will need to set this new variable to zero both on start and during the reset process.
    • Changing the random number to be between 1 and 12 or multiplying the initial roll by two will not match the same probabilities as using two separate dice rolls - therefore, you will need to use a roll counting variable. For more information regarding probabilities of dice rolls, you can check out our Dice Duelling activity.


    Player One

    A new 'set rollCounter to 0' block has been added to the 'on start' loop. The 'on shake' loop is now made up of an if, else if and else statement. The if statement: 'if rollCounter = 0 then' contains a 'set player1Roll to pick random 1 to 6' and a 'change rollCounter by 1, block. The else if statement: 'else if rollCounter = 1 then' contains a 'change player1Roll by pick random 1 to 6', a 'radio send number player1Roll', and a 'change rollCounter by 1' block. The else statement contains a 'show icon (cross)' block. Inside the forever loop, all conditions that were originally set to 'player1Roll > 0' are now changed to 'rollCounter = 2'. 'set rollCounter to 0' blocks have also been added to both reset programs (the one activated on button A and the one activated when the radio receives the reset string.)

    Player Two

    A new 'set rollCounter to 0' block has been added to the 'on start' loop. The 'on shake' loop is now made up of an if, else if and else statement. The if statement: 'if rollCounter = 0 then' contains a 'set player2Roll to pick random 1 to 6' and a 'change rollCounter by 1, block. The else if statement: 'else if rollCounter = 1 then' contains a 'change player2Roll by pick random 1 to 6', a 'radio send number player2Roll', and a 'change rollCounter by 1' block. The else statement contains a 'show icon (cross)' block. Inside the forever loop, all conditions that were originally set to 'player2Roll > 0' are now changed to 'rollCounter = 2'. 'set rollCounter to 0' blocks have also been added to both reset programs (the one activated on button A and the one activated when the radio receives the reset string.)



Here are some extra features you can add to your dice duelling game. These are for you to investigate and explore - there are no answers provided.

  1. Adding instructions to player screens.

    Tip: Try to keep instructions short or in picture form.


  2. Have a score system that tracks how many times each player has won or lost.

    Tip: You can replace the programs for a button or use the touch sensor.


  3. Let the players know if they are shaking the Micro:Bit too hard.

    Tip: This requires measuring the acceleration of the Micro:Bit, anything over a strength value of 1700mg is too much.


  4. Add animations to the dice rolls.

  5. Can you design a version of this game for two players using only one BBC Micro:Bit?

    Tip: This will not need any radio communications.



Video Lesson:

Exercises:

For this session we shall be creating a traffic light program using a Neopixel strip.

As with all our exercises, they are designed for learners to complete a level before moving to the next. Otherwise you may miss information or skip a crucial step that stops your program from working.

Click on each challenge heading to expand.


  1. Use the blocks provided by the Neopixel extension to connect a 24 LED strip to pin 1.

    Tip: Don't forget to create your own start-up for this new program to identify it.


    • This is part of the set-up for your program, so should be done in the 'on start' block.


    An 'on start' block holding a 'show string TL' followed by a 'set strip to NeoPixel at pin P1 with 24 leds as RGB (GRB format)' block.

  2. Set up 3 equal size ranges to recreate a traffic light with red at the top, yellow in the middle, and green at the bottom.

    Tip: The LED strip numbering starts at 0.


    • 24 is dividable by 3, meaning you'll need to have the ranges cover 8 LEDs each.


    The 'on start' now has a 'set range to strip range from 0 with 8 leds', a 'set range2 to strip range from 8 with 8 leds', and a 'set range3 to strip range from 16 with 8 leds' block. There is now a forever loop containing a 'range show color red' followed by 'range2 show color yellow' and 'range3 show color green' blocks.

  3. Have the traffic light repeatedly cycle through a crossing sequence - that is red for 10 seconds, red and yellow for 5 seconds, green for 10 seconds, and then yellow for 5 seconds.


    • Putting this sequence in the 'forever' loop will ensure it repeats continuously.

    • Remember to turn off the un-needed colours each time (set them to black).


    The forever loop's contents have been changed to read; 'range show color red', 'range2 show color black', 'range3 show color black', 'pause (ms) 10000', 'range show color red', 'range2 show color yellow', 'range3 show color black', 'pause (ms) 5000', 'range show color black', 'range2 show color black', 'range3 show color green', 'pause (ms) 10000', 'range show color black', 'range2 show color yellow', 'range3 show color black', 'pause (ms) 5000',



  1. Move each light change into their own functions, remember to then call them in the forever loop.


    • Give each function a suitable name.


    The traffic light colour blocks have been moved to four different functions, one for red, one for red and yellow (called redYellow), one for green, and one for yellow. The forever loop contents have been changed to read; 'call red', 'pause (ms) 10000', 'call redYellow', 'pause (ms) 5000', 'call green', 'pause (ms) 10000', 'call yellow', 'pause (ms) 5000'.

  2. Add animations to the Micro:Bit to show when it is and isn't safe for pedestrians to cross the road. Do this using three new functions - one for walk, one for wait, and one for warning that the lights are changing to green for the traffic.

    Tip: You will need to time your animation to replace the use of pause blocks currently for timing the lights.

    Tip: A variable will mean you only need one function for wait that can be used for when the light is green and when it's yellow.


    • This is a good opportunity to practice working with the 'repeat' blocks inside the functions.

    • You can call a function from inside another function.

    • If using two images for your animations, you'll need to repeat them 10 times with half seconds pauses after each for the red and green. Only 5 repeats when the yellow light is active.


    The pause blocks have all been deleted from the forever loop. The 'on start' block now holds a 'set repeats to 0' block. There is now a function called walk which contains a 'repeat 10 times' loop holding two images, each followed by a 500ms pause. Another function called warn which holds a 'repeat 5 times' loop holding four images each followed by a 250ms pause. A third new function called wait contains a 'repeat repeats times' loop holding two image blocks each followed by a 500ms pause. The red function has a 'call walk' block added to the end, whilst the redYellow function now has a 'call warn' block added. The yellow function has a 'set repeats to 5' and 'call wait' block added to the end. Finally, the green function has had a 'set repeats to 10' and a 'call wait' block added on the end.




  1. Create a traffic light colour variable for use to identify which light is currently active and use this in if/else if statements for setting the lights.

    Tip: Remember to set the variable in the 'on start' block.


    • You will need to use numbers to represent the different light states.

    • Use an else statement to capture any errors regarding the variable value.


    There is a new 'set light to 0' added to the 'on start' block. The forever loop now reads; 'if light = 0 then', 'call red', 'change light by 1', 'else if light = 1 then', 'call redYellow', 'change light by 1', 'else if light = 2 then', 'call green', 'change light by 1', 'else if light = 3 then', 'call yellow', 'set light to 0', 'else', 'show string Error'.

  2. Have the A button on the Micro:Bit cause the traffic to come to a stop.

    Tip: You only need this over-ride to work whilst the traffic lights are green.

    Important: You can stop a loop early using the 'break' block found in the Loops menu.



    • You need to tell the program to change to yellow when button A is pressed.

    • To over-ride the program in this way, you will need to check if button A is pressed inside the relevant animation's repeat block.

    • You may also need to check against another variable, this can use a stacked if statement or an 'and' Boolean Operator.


    An 'if button A is pressed and repeats = 10 then' statement containing a 'break' block has been added to the end of the wait function's repeat loop.

  3. Add sound to indicate when it is safe to cross for those who cannot see the visual instructions.


    • This just requires a single note to be played on each image change in the animation for when it is safe to start crossing (when the traffic light is red).


    There is now a 'play tone High D for 1 beat until done' block after each of the images in the walk function.



The below exercise is to expand the program further. This is for you to investigate and explore - there are no answers provided.

  1. Add a second Neopixel to pin 2 and have it act as the traffic light for traffic flowing in the other direction.

  2. How much can you tidy up your code with functions, while loops, repeat blocks, and variables?


Video Lesson:

Exercises:

For this session we shall be creating a program to turn your BBC Micro:Bit into a sound meter using a servo.

As with all our exercises, they are designed for learners to complete a level before moving to the next. Otherwise you may miss information or skip a crucial step that stops your program from working.

Click on each challenge heading to expand.


  1. Write a program that plays an alarm when the sound level is over 200.

    Tip: Don't forget the start-up routine to identify the program.


    • This requires an if statement with a condition of sound level > 200.

    • There is a variable block for the sound level in the Input menu for the V2.

    • Add some pauses to reduce the rate of your chosen sound/note.


    The 'on start' contains a 'show string Loud' block. The forever loop holds 'if sound level > 200 then' statement holding a 'play tone Middle C for 1 beat until done' and a 'pause (ms) 200' block.

  2. Have the Micro:Bit display the sound level on its screen.

    Tip: This can be done using a 'show string' block.


    • This needs to be in the forever loop, outside of the current if statement.


    The forever loop now contains a 'show string sound level' block before the if statement.

  3. Notice how the show string block slows down our alarm sound. Let us resolve this by replacing the number with these images and conditions:

    Sound level is 0.


    An image of a Micro:Bit with a blank screen - taken from the Makecode simulator.

    Sound level is over 0
    and
    less than or equal to 51.

    An image of a Micro:Bit with the bottom row of LEDs lit - taken from the Makecode simulator.

    Sound level is over 51
    and
    less than or equal to 102.

    An image of a Micro:Bit with the bottom two rows of LEDs lit - taken from the Makecode simulator.

    Sound level is over 102
    and
    less than or equal to 153.

    An image of a Micro:Bit with the bottom three rows of LEDs lit - taken from the Makecode simulator.

    Sound level is over 153
    and
    less than or equal to 204.

    An image of a Micro:Bit with the bottom four rows of LEDs lit - taken from the Makecode simulator.

    Sound level is over 204.


    An image of a Micro:Bit with all LEDs lit - taken from the Makecode simulator.

    The alarm should now only sound when the meter is full.


    • This will require a new series of if and else if statements.

    • Remember you can use the 'and' Boolean operator instead of stacking if statements.

    • Use the else statement at the end for any errors in the program.


    The forever loop has been re-written to: 'if sound level = 0 then', 'show leds' (blank), 'else if sound level > 0 and sound level ≤ 51 then', 'show leds' (bottom row lit), 'else if sound level > 51 and sound level ≤ 102 then', 'show leds' (bottom two rows lit), 'else if sound level > 102 and sound level ≤ 153 then', 'show leds' (bottom three rows lit), 'else if sound level > 153 and sound level ≤ 204 then', 'show leds' (bottom four rows lit), 'else if sound level > 204', 'show leds' (all lit), 'else', 'show string error'.

  4. Add a 180° positional servo to pin 1. Program the servo to rest at 0° whilst the sound level is less than or equal to 102. Otherwise, it needs to move to 180°

    Tip: You will need to use the servo extension.


    • We can use all the default settings for the positional servo for this. However, it is still good practice to set the servo to a start position (0° in this case) in the 'on start' block.

    • You will need to set the servo angle in each if/else if statement.

    • When testing, make sure you give the servo time to complete its rotation - if you switch the condition too quickly it will not behave the way we expect it to.


    A 'set servo P1 angle to 0°' block has been added to the 'on start' sequence and both the if/else if statements for when the sound level is ≤ 102. The other else if statements have a 'set servo P1 angle to 180°' added to them.



  1. When the servo changes direction, can you have the servo move only 10° every 300 milliseconds? This reduces the stress on the servo, stopping it from breaking - something the simulator does not replicate.

    Tip: You will need to make sure the servo does not attempt to turn below 0° or further than 180° - this could break your servo.


    • You will need to create a variable for the angle - remember to set this in the 'on start' block.

    • This new variable can be used to set the servo angle - changing it up or down by 10°.

    • When setting up your if statement conditions consider the values to use - as we're moving 10° at a time, consider using 10 and 170.

    • These new if statements will need to replace the set servo blocks in each if/else if statement.

    • As there is already a 200 millisecond pause in the > 204 statement, we need to adjust the movement to total 300.


    A 'set angle to 0' block has been added to the 'on start' sequence. Inside the forever loop, each 'set servo P1 angle to 0°' blocks have been replaced with an 'if angle ≥ 10 then' statement containing 'change angle by -10', 'set servo P1 angle to angle°', and 'pause (ms) 300' blocks. The 'set servo P1 angle to 180°' blocks have been replaced with an 'if angle ≤ 170' statement containing 'change angle by 10', 'set servo P1 angle to angle°', and 'pause (ms) 300' blocks. The difference being in the section with the alarm, where we instead used a 'pause (ms) 100' block inside the new if statement.

  2. To tidy up our code, let's move the servo instructions into their own functions and call them when needed.

    Tip: You'll need one for lowering and one for increasing the angle.


    • Remember the Functions menu is in the Advanced section.

    • To only have two functions - remove the 200 millisecond second pause from the alarm program so it too can use the 300 milliseconds pause in the servo function.


    The 'if angle ≥ 10 then' statements have been removed and replaced with a 'call quiet' block whilst a copy of this statement and its contents is now in a function called quiet. The same has been done for the opposite movements using a new function called loud. The 'pause (ms) 200' block inside the alarm program has been removed.



Before you start this exercise, we advise that you duplicate your program as we will be deleting a lot of the previous code.

Step 1. Close your program and return to makecode.microbit.org's homepage.

Step 2. Click on the view all option next to 'My Projects'.

Step 3. Select the file you want to duplicate (a green tick should appear on the file).

Step 4. Click on the duplicate option in the top right of the screen. This will allow you to then select a name for your new copy.

Step 5. Open your new copy and continue with the next challenge.

  1. Instead of having a single value determining the movement of the servo, let us look at having the servo act like the arm on a sound meter (as shown below), traveling to the correct angle for the current sound level.

    An illustration of how a servo could be used as a pointer on a sound meter.


    Important: We have a range of 0° to 180° on the servo but the sound level has a range of 0 to 255. There are two ways to approach this:

    Do the maths ourselves

    1. Work out how far the meter needs to move for one unit of sound:

      (180 ÷ 255) = 0.7058823529411°


    2. Use the above value with a Maths block inside a 'set servo' block.

      The makecode block for this - 'set servo P1 angle to sound level x 0.7058823529411°'.

    Use the 'map' block

    1. The map block allows us to enter the range we're measuring (sound level) which is 0 to 255 and convert it to the servo's range of 0 to 180. The block then does all the calculations for us.

      The makecode block for this - 'set servo P1 angle to map sound level from low 0 high 255 to low 0 high 180°'.


    • We need to delete the functions we've created for the servo from our program. The 'call' blocks will automatically vanish when their function is removed.

    • Keep the screen meter animation in place.

    • A single 'set servo' block using the method chosen from above is all that's required for this.


    The functions have been removed along with the call blocks. Instead, there is a 'set servo P1 angle to map sound level from low 0 high 255 to low 0 high 180°' inside the forever loop before the if statements for the screen and alarm programs.



The below exercises are designed to expand your understanding of servos. These are for you to investigate and explore - there are no answers provided.

  • Instead of using the meter to measure sound level, have it measure radio signal strength, with an alarm for if the signal gets too weak.

    Tip: You will need to find the range of values for the radio signal strength.


  • Remember the traffic light program we created last session? Now you can add a rail crossing with a servo acting as the barrier arm.

    Tip: You can only attach the servo to pins 0, 1 or 2. However, you can relocate the Neopixels to other pin numbers (up to 16).



Video Lesson:

Exercises:

For this session we shall be creating a program to control a railway crossing. This includes revision of radio communications, if-statements, variables, functions, Neopixels, and servos.

We recommend you attempt these challenges using the standard editor.

As with all our exercises, they are designed for learners to complete a level before moving to the next. Otherwise, you may miss information or skip a crucial step that stops your program from working.

Click on each challenge heading to expand.


For these exercises, imagine you have one Micro:Bit controlling the crossing whilst another is acting as a beacon on-board the train. The crossing will know a train is coming when the beacon's signal strength gets stronger.

  1. Use an image, animation and/or text to create a start-up screen that identifies your program.

    If only using an image, include a pause (500ms) so it remains on the screen long enough to see.


    • Try not to make the start-up take too long, otherwise this will get frustrating during testing as we progress.


    An 'on start' program containing a 'show leds' and a 'pause (ms) 500' block.

  2. Have the Micro:Bit send the number zero via radio from inside the forever loop.

    Tip: Don't forget to set a radio group for your Micro:Bit to use.


    • You can find the blocks you need in the Radio menu.

    • You need to set a radio group in the start-up sequence.


    A 'radio set group 33' block has been added to the 'on start' program. There is now a 'forever' loop containing a 'radio send number 0' block.

  3. Create a beacon strength variable and have it set to the signal strength being received.

    Tip: To measure the signal strength, the program needs to first receive the sent number.


    • Pick a suitable name for your new variable. Some programs use lots of variables, and it helps to be able to quickly identify what each one is for.

    • Remember to set your new variable to 0 when the program first starts - This is good practice for when you start learning text-based programming languages.

    • You will need to set your new variable to the received signal strength value inside an 'on radio received' block.


    A 'set variable to 0' block has been added to the 'on start' program. There is now a new loop using 'on radio received receivedNumber' which contains a 'set variable to received packet signal strength' block.

  4. We now need two functions, one for the program for when a train is coming, the other for when there isn't. Give each function a suitable name and have a different image in each.

    Tip: The images at this stage are for testing, so any will do.


    • The Functions menu can be found in the Advanced section.

    • These functions will not show in testing, as we have not called them in the program.


    There is now a function called trainComing which contains a 'show icon' block for a tick, and another called noTrain with a 'show icon' block for a cross.

  5. Call your function for when a train is coming when the signal strength is greater than or equal to -75. Otherwise, call the no train coming function.

    Tip: While testing in the simulator, treat the top Micro:Bit as the crossing controller and the bottom Micro:Bit as the train beacon.


    • You will need to create an if statement for this which compares the beacon strength value to -75.

    • The radio signal strength for the Micro:Bit in the simulator has a range of -128 (weakest signal) to -42 (strongest signal).

    • Remember we have several maths symbols to choose from: = equals, < less than, > greater than, ≤ less than or equal to, and ≥ greater than or equal to.


    The 'forever' loop has had an if/else statement added. The if statement holds the comparison of 'beaconStrength ≥ -75' and holds a 'call trainComing' block. The else statement contains a 'call noTrain' block.



  1. We now need to create our crossing lights. For this we need a Neopixel connected to pin 1 which is made up of 15 LEDs.

    Tip: You'll need to add the Neopixel extension to your blocks menu.


    • Re-visit Session Four if you need a reminder of how to get and use the Neopixel extension blocks.

    • You will need to change the default length from 24 to 15 LEDs.


    A 'set strip to NeoPixel at P1 with 15 leds as RGB (GRB format)' block as been added to the end of the program in the 'on start' section.

  2. Set up two ranges, with suitable names - one for the top 5 LEDs and another for the bottom 5 LEDs.

    Tip: Remember that counting in computing starts at zero.


    • The first range will start at 0, whilst the bottom range will start at 10.


    There are now 2 'set range' blocks in the on-start program. They are called topLight and bottomLight. The topLight range is 'set to strip range 0 with 5 leds' whilst the bottomLight range is 'set to strip range 10 with 5 leds'.

  3. Remove the image in your function for when a train is coming and replace it with a program that will turn the lights on and off (alternating between the top and the bottom) three times. Include a pause of 500ms after each light turns on.

    Tip: Remember that setting a light to black turns it off.


    • Each time a light turns on, the other needs to turn off.

    • You can use a 'repeat 3 times' block to shorten the program.


    The program now inside the trainComing function is contained in a 'repeat 3 times' block. The block order is: 'topLight show color red', 'bottomLight show color black', 'pause (ms) 500', 'topLight show color black', 'bottomLight show color red' and 'pause (ms) 500'.

  4. Replace the image in you function for when a train is not approaching with a program to make sure the lights are all turned off.


    • You will need to set both of our light ranges to black in the function for when there is no train coming.


    The function for when a train is not approaching now holds two blocks: 'topLight show color black' and 'bottomLight show color black'.

  5. Have your program play a middle C note when the top light is on and a middle E note when the bottom light is on. This is our alarm to warn drivers and pedestrians a train is coming.

    Tip: When testing, make sure the top Micro:Bit (our 'crossing') has a very low signal strength or the alarms on both Micro:Bits may merge and/or be out of sync.


    • You only want the notes to be 1 beat long.

    • You can change the note inside the 'play tone' block.


    There is now a 'play tone Middle C for 1 beat until done' block before the first pause in the function for when a train is coming. There is then a matching block with Middle E selected inserted above the second pause.



  1. For our railway crossing barrier we shall use a servo to control its movement. This needs to be connected to pin 2 and be in the open position (0°) at the start of the program.

    Tip: This will require the Servos extension.


    • If stuck, please look back at Session Five.


    There is now a 'set servo P2 angle to 0°' block inside the 'on start' program.

  2. When a train is approaching, we want the lights and alarm to sound three times and then for the barrier to close (move to 90°).


    • Be careful not to place this new instruction inside the repeat loop.

    • Remember that the servo is on pin 2.


    The function for when a train is coming now has a 'set servo P2 angle to 90°' block after the repeat loop.

  3. Once the barrier is closed, we need the alarm to stop while the lights continue to flash.

    Tip: You will need a way to check the angle of the servo.


    • You will need a servo angle variable.

    • This new variable will need to be updated each time the barrier opens or closes.

    • Move the program with the lights and alarm into an if statement for when the barrier is open.

    • For when the barrier is closed, have the lights continue to flash. As long as the barrier is down, this will keep repeating, so you don't need to use a repeat block here.


    There is now a 'set servoAngle to 0' variable block in the 'on start' program. The function for a train coming now has its previous program wrapped in an 'if servoAngle = 0 then' statement with an additional 'set servoAngle to 90' block at the end. Inside the else statement for this is the same light flashing program as created in part 3 of the silver exercises, without the repeat loop.

  4. Once the train has passed and the beacon signal fades to less than 75 again, we want the barrier to open (retuning to 0°).

    Tip: Remember to also reset your new angle variable at the same time.


    • You can use the angle variable to tell if the gate needs opening. This is the opposite to the check we've already done to see if it needs closing.

    • Don't forget that our servo is attached to pin 2.


    There is now an if statement inside the function for when there is not train coming. This if statement has the condition of if the angle variable is equal to 90. Inside this there are two blocks: 'set servo P2 angle to 0°' and 'set (angle variable) to 0'.

  5. At the moment, the servo is moving too fast. This could cause injury to a pedestrian or damage a passing vehicle. So we need the barrier to lower 5° every 200ms. When it is opening, it is safer to go faster - in this case 20° every 300ms.

    Tip: Turn both the red lights on fully during these movements as the flashing will stop during these movements.


    • Our function for when our train is coming will need an else if statement for when the barrier is only partially closed.

    • You will need to use the angle variable in the maths conditions.

    • Remember you can also use the 'and' Boolean block demonstrated previously if you want to check against two values.

    • Perhaps create new functions for the opening and closing movements. Yes, you can call a function inside another function.

    • When opening the barrier be aware that 90 cannot be divided by 20. Think about how you will make sure the barrier opens fully without going too far and breaking the servo.


    There is now a new function called closing which holds the following program: 'topLight show color red', 'bottomLight show color red', 'change servoAngle by 5', 'set servo P2 angle to servoAngle°', 'pause(ms) 200'. There is also a new function called opening which holds an if statement for when the servo angle is greater than or equal to 20. Inside this statement is the following: 'topLight show color red', 'bottomLight show color red', 'change servoAngle by 20', 'set servo P2 angle to servoAngle°', 'pause(ms) 300'. The else statement that follows holds a 'set servoAngle to 0' and a 'set servo P2 angle to servoAngle°' block. Inside the function for when a train is coming there are some changes. The if statement now has a 'call' block for the closing function instead of the servo movement blocks. There is now an else if statement following this with the condition that the servo angle is greater than 0 and less than 90. Inside this there is also a 'call' block for the closing function. The final change is in the function for when no train is approaching where the if statement condition has been changed to when the servo angle is greater than 0 and the blocks contained replaced with a single 'call' block for the opening function.


This section is an additional activity for you to try out - this will provide an opportunity for revision whilst introducing some new blocks and terms.

As with all our exercises, it is best to work through this is order.

Select a heading to expand.


  1. Create a basic dice rolling program on your Micro:Bit. This should show a random number between 1 and 6 on the screen when shaken.

    Tip: Don't forget your start up screen.


    • If stuck, revisit sessions two and three.

    • This does not require a forever loop.


    The 'on start' holds a 'show string Dice' block. There is no forever loop. Instead, we have an 'on shake' input with a 'show string pick random 1 to 6' block inside.

  2. Instead of showing a number, have the screen show the following images depending on the value rolled.

    One

    The screen has the centre LED lit.

    Two

    The screen has two LEDs lit in a diagonal.

    Three

    The screen has three LEDs lit in a diagonal.

    Four

    The screen has four LEDs lit in a square.

    Five

    The screen has five LEDs lit in a cross.

    Six

    The screen six LEDs lit in a rectangle.

    Tip: You will need to create a variable to hold the dice roll value.


    • You will need a different if/else if statement for each possible value. You can then use the else statement to catch any errors.


    There is now a 'set diceRoll to 0' block in the 'on start'. In the 'on shake' program, the 'show string' block has been replaced with a 'set diceRoll to pick random 1 to 6' block. Below this is an if statement with five else if statements and an else statement attached. There is a condition for each possible dice roll value with an instruction inside to show the correct image. The else statement holds a 'show string error' block.



A Magic-8 Ball is like a dice, put instead of numbers it produces answers when shaken.

  1. Instead of showing numbers in your program, have the Micro:Bit display the answers yes, no, maybe, go for it, good idea, bad idea, don't know, definitely, absolutely not, and no idea.

    Tip: There are now 10 options.


    • For the first six options you can simply replace the current images.

    • You will then need to add four more else if statements.

    • Don't forget to change the random number generator to have 10 options instead of 6.


    No changes to the 'on start' sequence. In the 'on shake' program, the 'pick random' block has been changed to the range of 1 to 10. Four additional 'else if' statements have been added for the values of 7, 8, 9, and 10. Each condition triggers one of the asked for responses. The 'else' statement remains the same.



There is a different way we could have approached this program. We could have used something called an array. Array blocks can be found in the Advanced menu.

An array is a special kind of variable. Instead of holding only a single piece of information like the variables we've used, it can hold a list of values/strings.

You can then call a value from the array by its location in the list. As with most coding, the numbering of list items starts at zero.

Example:

Let's say we wanted to write a program that randomly selected a string from a list of apple, orange, and banana when we press button A.

  1. As with any variable, we need to set the array in the 'on start' section:

    An 'on start' block containing a 'set text list to array of apple orange banana' block.

    We can add or take away items to the array using the plus and minus buttons on the block.


  2. To have the screen show a random item from the array we can use the 'show string' combined with a 'get random value from' array block.

    There is now an 'on button A pressed' program containing a 'show string get random value from fruit' block.

    We now have a complete program that randomly selects an item from the list and displays it on the screen when we press button A.

As the Magic-8 Ball program has a single variable which has a list of different strings (the answers) we could use an array and have the program randomly select from a list instead of using such a large if/else if/else statement.

Important: Arrays cannot be used to replace all if and else if statements. With Makecode, they can only be used for lists of strings or numbers. For example, you could not use this for the dice program with different images for each value.


  1. Change your Magic-8 Ball program to use an array for all possible answers and randomly select from it when shaken.

    Tip: You may want to duplicate your file first so you can compare them later.


    • Use the fruit list program in the example and replace the fruit with the Magic-8 Ball answers provided in the silver exercise.

    • Make sure you are using the text list block, the number version will error if you add words.

    • Remember, you can add more items to the array using the plus symbol on the set-up block.

    • Rename the list to something more suitable than fruit.

    • If you haven't already, re-design the start sequence for the Magic-8 Ball instead of the dice.


    The 'on start' program has a 'show string 8' block followed by a 'set text list to array of' block containing all ten possible answers. The 'on shake block now only contains one block - 'show string get random value from text list'.


Now that you've discovered random generators using arrays, there are number of different fun things you can create. Here are some ideas.