MakeCode for Circuit Playground Express

MakeCode works with the Circuit Playground Express. If you have the original Circuit Playground Classic, check out the Arduino version of this code on the previous page

Lots of Coding Options

There are many options for programming the Circuit Playground Express! You can hand-code it with the traditional, text-based Arduino IDE, as seen in the previous page of this guid, you can try your hand at the exciting new CircuitPython methods, or you can harness the incredibly user-friendly, yet powerful block-based interface of Microsoft's MakeCode web app!! (You can even go full circle and hand type code inside the MakeCode interface with Javascript!)

By simply dragging and dropping elemental code blocks, you can create Circuit Playground Express software in no time, and even test it out with the virtual Circuit Playground Express simulator built into the page.

Be sure you've gone through the tutorial here on the basics of MakeCode for Circuit Playground Express. 

MakeCode

You're now ready to begin coding your own program with the MakeCode interface to use the Circuit Playground Express as a Freefall Deck.

Click the Projects button so you can start a new project, then click the New Project... button to create a fresh, clean project.

Input

Your Freefall Deck will give up its secret Morse code message only after its accelerometer has detected a free fall. To do this, you'll create an input that check the accelerometer for free fall, and then plays the appropriate tones.

The first block you'll add is is an input block that can read the accelerometer on the Circuit Playground Express. Click on the Input category and choose the on shake block, then drag it to the canvas. (All of the accelerometer-based inputs are contained within this one block.)

Click on the shake dropdown, and select the free fall option from the list.

Freefall Test

You can test the free fall by adding a play tone block to the on free fall input.

Click the music category and drag the play tone at Middle C for 1/2 beat block into the on free fall block.

Upload this code to your Circuit Playground Express by downloading the file, pressing the reset button on the Circuit Playground Express to enter bootloader mode, and dragging the file to the CPLAYBOOT drive.

Now, drop your Circuit Playground Express from one hand to the other and it will play a tone!

Morse Code Setup

Next, you'll make blocks to play Morse code dots and dashes. These will be represented in the play tone blocks as frequencies, instead of music notes, and as timings in milliseconds, instead of musical note durations.

Morse code follows conventions on timing for the lengths of dots and dashes, spaces between elements, spaces between letters, and spaces between words. 

You can set up all of the timing in variables in a setup loop, and then use these variables throughout the your on free fall code block.

From the loops category, get an on start block.

Variables

You can declare your variables, and assign to them values using the set item to 0 block. Go ahead and get one of these from the variables category, and place it in the on start block.

Since you'll be using the variable block on its own as well, drag a copy of the item variable to your work space as well.

Let's use the first variable to set the pitch of the tone you'll play. Audible Morse code is usually between 600 and 800 Hz. Click on the dropdown in the set item to 0 block, and choose the Rename variable... item.

In the rename popup window, type in the name PITCH, then press 'Ok'. This will rename all instances of the variable item used in your code.

Change the value to 680, which will be the frequency of the tone.

Now, you can drag the PITCH variable block into the play tone block to replace the Middle C that's currently there.

Repeat the same process as above to create a variable for the duration of a Morse code dot, which is 200 milliseconds. The steps are:

  • Drag a set item to 0 block from the variable category
  • Rename item to DOT
  • Change the DOT variable value to 200

Place the new variable declaration into the on start block.

Drag a copy of the DOT block out of the variables category and onto 1/2 beat value that's currently in your play tone block

Download the .uf2 code and upload it to your Circuit Playground Express to test it out. You'll now hear a single dot tone play upon freefall.

There isn't a way currently to trigger the 'free fall' input in the simulator, so you can switch the block to read 'shake' temporarily while testing (a 'shake' button appears over the board graphic). Just remember to switch it back to 'free fall' before moving the code to your Circuit Playground Express!

Repeat the same steps as above to define the rest of the Morse code timing variables as seen here.

Note how most of the values are defined as multiples of the dot value. This is how Morse code is defined, so you can adjust the pitch and duration of the DOT and everything else will update accordingly!

Using Morse Code

Morse code uses different length pauses between the dots (short tones) and dashes (long tones) to indicate separation between characters (short pause), letters (medium pause), and words (long pause). 

To create the letter A, which is .-  or "dot dash", we need to use a play tone at PITCH for DOT, followed by a short gap pause, and then a play tone at PITCH for DASH block.

For the gap pause, get a pause (ms) 100 block from the loops category.

Next, get a copy of the GAP variable from the variables category, and drag it onto the pause (ms) value.

To complete the .- you can duplicate the play tone block, place it under the pause block, and change the duration with the dropdown from DOT to DASH.

Creating Messages

You've now created all the blocks you need to make your Morse code messages! Here's an example of SOS, or ... --- ... created by duplicating blocks and adjusting their variables from the dropdown menus:

We've also added a half second delay before the tone plays, so the to give the listener a moment.

Now, you can make up your own messages, and they will be revealed when the Freefall Deck falls! You can use this handy chart as a reference for the letters and numbers of international Morse code.

This guide was first published on Jan 15, 2017. It was last updated on Jan 15, 2017. This page (MakeCode for Circuit Playground Express) was last updated on Aug 16, 2019.