Project

Sound-triggered Xmas Decoration

LED animations to decorate your home or tree

Scroll down

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… 

 

 

 

1. Programme the micro:bit

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.

makecode sound reactive xmas decor

By default, when you start a new Makecode project, you’ll get on start and forever blocks already on the canvas.

makecode

  • 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.

makecode on start

  • 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:

makecode microbit

  • 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.

makecode

  • Complete the forever loop as shown below.

makecode

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:

makecode function

  • Right-click on the scroll image block and select duplicate 3 times to then create the following:

makecode function

  • Add a show leds and set random to the function as illustrated below. Ensure to set random to 0.

makecode function

  • Duplicate the function and name it animation2.
  • Edit it so that it looks like this:

makecode function

  • Go back to the forever loop and add 2 function calls as illustrated below:

makecode forever

You should now have complete and working code. You can test it in the Makecode editor by sliding the microphone volume up and down.

2. Download the code

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.

3. Do more.

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.

You can find more information and resources for your micro:bit here: https://microbit.org/

 

 

Like what you read? Why not show your appreciation by giving some love.

From a quick tap to smashing that love button and show how much you enjoyed this project.

Location

Please select an option to see content specific to your location and shop online.

Browse the US site

Privacy

Our website uses cookies and similar technologies to provide you with a better service while searching or placing an order, for analytical purposes and to personalise our advertising. You can change your cookie settings by reading our cookie policy. Otherwise, we’ll assume you’re OK with our use of cookies.