This project will run you through how to create animations on your micro:bit and trigger them using the new capabilities of micro:bit version 2; the microphone input. You’ll also learn how to use functions and integrate them into your code. When you’re done you’ll have a flashy Christmas decoration which is triggered by any sound, whistling, clapping, laughter…
You will be using Makecode to programme the micro:bit. Below is an overview of what you’ll build, don’t worry, we will break it down into manageable steps.
By default, when you start a new Makecode project, you’ll get on start and forever blocks already on the canvas.
- In the Variable menu, click on Make a Variable… and name it random.
- Create a second variable and name it delay.
- Drag 2 instances of set x to 0 from the Variables menu to on start.
- In the set x to 0 blocks, use the dropdown to set one to delay and one to random.
- In the number fields, set random to 0 and delay to 35.
- From Basic, insert a show leds block as shown below.
- Drag an if block from Logic to forever.
- Drag a Comparison block from Logic and place it on its allocated space within the if block.
- Set the operator on the Comparison block to greater than or equal to.
- In the right-hand field enter 40.
- From the Input menu, drag a sound level block to the left-hand field of the Comparison block.
So far your code should look like this:
- From Variables, place set random inside the if block.
- From Math, place pick random in the right-hand field of the set variable block.
- Enter 1 and 2 in the pick random fields.
- Complete the forever loop as shown below.
Now you’ll create the functions that create the animations that will run on the decoration.
- From the Advanced menu, go to Functions and click Make a Function and name it animation1.
- From Loops, place a repeat x times block inside the function block you just created. Set the looping times to 30.
- From Advanced > Images, place a scroll image block inside the repeat block. Set offset to 0 and place a delay variable in the interval field.
- From Advanced > Images, place a create image block in the first field of the scroll image block.
- Fill in the relevant squares on the create image block so that it looks like this:
- Right-click on the scroll image block and select duplicate 3 times to then create the following:
- Add a show leds and set random to the function as illustrated below. Ensure to set random to 0.
- Duplicate the function and name it animation2.
- Edit it so that it looks like this:
- Go back to the forever loop and add 2 function calls as illustrated below:
You should now have complete and working code. You can test it in the Makecode editor by sliding the microphone volume up and down.
You’re now ready to upload the code to the micro:bit.
- Connect your micro:bit to the computer using the micro USB cable.
- Click the Download button.
You should now have a micro:bit displaying a star which runs a random animation when sound is detected.
Once you have the basic project up and running you could:
- Adjust the microphone’s sensitivity to your liking and environment. You can do this by editing the sound level figure in the forever if statement. The number can range from 0 to 253.
- Create more or different animations. You can easily tweak the existing ones or even add more. If you do this you’ll have to adjust the pick random range to accommodate them and add the relevant if random = blocks.