You'll begin by getting familiar with MakeCode, in case you haven't used it before, by following this guide. Then, return here to program your Circuit Playground Express with the NYE Clock code.

Clock and Noisemaker

We'll give the Countdown Clock a few different capabilities:

  • Turn it on to hear a short intro to Auld Lang Syne, the lights turn on purple and white
  • Press the B button (the one on the right) to hear the intro again at any time
  • Blow on the microphone to create a party horn noisemaker sound as long as you blow
  • Press the A button at ten seconds until midnight -- it will count down the seconds with the NeoPixel LEDs, and then play a festive animation, and the full rendition of Auld Lang Syne
  • Flip the switch to the left to disable the noisemaker 

This is the embedded code window for MakeCode. If you like, you can download it and then drag it onto you Circuit Playground Express in CPLAYBOOT mode to start playing with the clock and noisemaker immediately. If you'd like to take a closer look at how it works, follow on below.

On Start

Follow the image below to create the on start block. The code contained within it will run once at start-up. This is a good place to set parameters, such as:

  • set brightness to control NeoPixel brightness
  • set volume to set the base volume level for notes and songs
  • set tempo to set the meter for songs

You'll also add a show ring block to turn on the NeoPixels in an alternating white/purple pattern by clicking a color wedge and then clicking each pixel circle in the wheel.

Functions

Sometimes, there are pieces of code that are complex or long, and need to be used in multiple places throughout your program. In this program, the songs that we'll play qualify. Both the startup and button presses cause the songs to play, so we don't want to have to write the songs, note by note, in multiple places. This is where functions come in!

A function is a user-defined block you can create and name, which you can then refer to elsewhere with a call function block. Create one by clicking ADVANCED category dropdown, and then the FUNCTIONS category, and then clicking Make a Function. Name the function play_Auld_Lang_Syne_Intro.

You'll see the blue function play_Auld_Lang_Syne_Intro block appear on the canvas. We'll add code to it later. For now, drag the call function play_Auld_Lang_Syne block to your on start block as shown.

Go ahead and make a second one the same way, named play_Auld_Lang_Syne.

 

Switch It

We'll use the switch to turn on and off the loud sound detection that will be used to control the noisemaker function.

Create two on switch moved blocks. Set one to left and one to right. Create a variable called noise_maker to use in a couple of set item to 0 blocks as seen below. Place one in the on switch moved left block and leave the value at 0, then place the other in the on switch moved right block, and change the value to 1. This variable can then be read inside another block to change functionality based on the switch position.

You'll also use another pair of show ring blocks to change the colors. Green means "safe" and red means "it's gonna get noisy in here!"

Loud Sound

This is a pretty neat trick -- the microphone can be tested for loud sounds, and blowing on the mic happens to trigger it. So, we can blow on the mic just like a real party horn!

Create an on loud sound input block, and then add an if true then block as shown below. Drag in the noise_maker variable and change the True to a 1. Now, the switch position will dictate if the code contained with in gets run or not.

When the switch is flipped right, and there's a loud sound, we can do the following:

  • set all pixels to blue
  • while sound level > 100
    • play tone at sound level x 10 for 1/16 beat
  • show ring

This will create a really fun, noisy, squealy sound, because the pitch of the sound will vary based on the volume of the noise. Blow softly for a lower tone, and blow hard to raise the pitch!

Write a Song Function

Earlier, we created a couple of functions for our songs. Now, we can populate them with play tone at _ for _ beat blocks. Follow the image to create the intro to Auld Lang Syne and then a second time for the full song.

Countdown

Make an on button A click block, and an on button B click block.

The B button is simple -- we'll just use this to play the intro to the song. Add a call function play_Auld_Lang_Syne_Intro block.

Button A has more going on -- first, set all pixels to medium white.

Then, we'll count down the ten seconds with a for index from 0 to 9 block. Inside this:

  • set brightness to 20
  • play tone at 1020 Hz for 1/8 beat
  • pause for 1 second
  • set pixel color at index to blue

Since the index variable changes with each iteration of the loop, this is what changes the color of each sequential NeoPixel.

Once the ten seconds have counted down, the play sound magic wand block plays, then show animation sparkle for 3000 ms.

Then, we'll set brightness down to 15, and show ring to alternating blue and green. 

Finally, we get to play the full song with call function play_Auld_Lang_Syne.

Happy New Year!

This guide was first published on Dec 27, 2017. It was last updated on Dec 27, 2017.

This page (Program It With MakeCode) was last updated on Nov 06, 2020.