Program It in MakeCode

Now that you've set up your box, you can program it with Microsoft MakeCode for Adafruit!

MakeCode is a web-based code editor designed for beginners that is amazingly simple to learn. You can use it online -- there's no need to download anything. You can build programs quickly with drag-and-drop blocks, similar to Scratch. And best of all, it displays a simulated Circuit Playground Express so you can get an idea of how your program is working before you download your code to the real CPX board.

If you're new to MakeCode and CPX, you can learn more by checking out the Adafruit MakeCode Guide.

What the Code Does

The MakeCode program for this project does several things:

  • Sets up the light sensor so it can trigger actions when the available light gets dimmer;
  • Makes the NeoPixel light ring on the CPX board flash in the shape of an orange face, like a jack-o-lantern;
  • Plays an eerie tune;
  • Makes the servo swing back and forth so the "mouth" of the monster box looks like it's jittering with laughter.

These instructions will take you through a basic version of the program. (You can open it online to look at or download by clicking the button below.)

To personalize your code, see the Trick Out Your Spooky Box page for ways to add other outputs!

Adjust the CPX Settings

To begin, use an on start block from the LOOPS menu to adjust the settings for the servo, speaker, and light sensor. For each one, you will mouse over the white ovals to open up sliders that let you set the values where you like.

Set Up the Servo

First, to find the servo blocks, click on ADVANCED in the menu column and scroll down to EXTENSIONS. When you click on it, it takes you to another page with several choices. Click on SERVOS and it will be added to the menu column. Add a set servo A1 to [90] block. Use the slider to change the 90 to a number that will bring the servo arm down to the bottom of the box. For the sample used here, 160 degrees was the best setting.

Set Up the Speaker

To make sure your eerie music can be heard above the noise of Halloween night, grab a set volume block from the MUSIC menu and raise the level as needed.

Set Up the Light Sensor

The light sensor on the CPX detects changes in the relative light level, so it helps to set it to the actual conditions around it.

From the INPUT group, add a set [dark] light threshold to block and a set [bright] light threshold to block. Since it will probably be dark where your box is, adjust the numbers to specify low levels of light and dark. (You can fine-tune them later when you test out the code on the CPX.)

With those set, you can use the on light dark block to tell the CPX to make the box do its thing.

Program the Actions

Once the CPX is set up, you can write one or more stacks of code that tell it what to do. In this case, you'll build a stack of code blocks inside an on light [dark] block from the INPUT menu to instruct the CPX to flash its lights, play music, and move the servo.

Coding Tip: It's easier to test your code on the screen (and with the actual box) if you use a simple input, like Button A. When you've got the code working, just make a duplicate of that stack and change the input on it to the light sensor.

Make the NeoPixels Flash in a Face Design

To make the pumpkin face in lights, drag a show ring block from the LIGHT menu. Click on all the lights on the left side for the smile. Then click on two lights on the right side for the eyes.

To make it flash, drag a set all pixels to [red] block underneath the show ring block, and change the red to black. This will turn all the lights off. Drag a pause block from the LOOPS menu and set it for the number of milliseconds you want the pause to last.

Then right-click on the first show ring block and choose Duplicate. Drag the second show ring block under the pause.

Don't forget to add another set all pixels to [black] block at the end of the entire stack to turn all the lights off, so it's ready for the next time it's triggered.

Play Some Music

The easiest way to add music is to choose a premade tune from the MUSIC menu. Drag a play sound [ba ding] until done block under the light block. Click on the small down arrow and pick a melody from the drop-down list. Power Down works well.

Program the Servo

To make the mouth open and close, you need to make the servo arm swing back and forth. You already set it to lay flat when the program starts. Use another set servo [A1] angle to [90] ° block to move the servo up until the lid is pushed open as far as you would like it to go. In the example project shown here, 90 degrees was the right setting for the "lid open" position.

A pause block from the LOOPS menu is inserted after the set servo block, to give the servo time to complete the movement before the program goes on.

To create a laughing effect, you want the mouth to open and close several times in a row. Once you've decided on your "open mouth" and "close mouth" settings, you can just repeat them (with pauses in-between) by placing them inside a repeat [4] times block.

However, varying the amount the mouth opens and closes each time makes the box a little more life-like. So the sample code shown here has a slightly different amount each time that is somewhere between the widest open setting and totally shut.

After you download your code and test it, play around with the number of degrees the servo arm pushes the lid open, as well as the timing of the pauses, to get your box to laugh just right!

Download MakeCode to the CPX

Before you download the program to your CPX, give your file a name! It will make it easier to find. You can also download the file to your hard drive or save it as a link to open online later or to share.

To copy the code to the CPX, connect it to your computer with the micro USB cable. Your computer will treat the CPX the same way it does a flash drive, as an extra piece of hardware for storing files. It will show up as a drive called CLAYBOOT. (You can ignore any pop-ups.)

To tell the CPX to get ready to receive code, you need to put it into bootloader mode. Press the tiny Reset button in the middle of the board until all the lights turn red for a second and then green. You may have to press the button a few times the first time you use the CPX, or whenever it doesn't go into bootloader mode the first time.

Then, click the pink Download button on the MakeCode page. It may copy the file to the Downloads folder, or whichever location you last saved a file to. If it's not already on the CPX, drag and drop (or copy and paste) the file into the CLAYBOOT drive.

If you get a drive named CIRCUITPY, no worries! Press the button twice quickly to change that to CPLAYBOOT. CIRCUITPY is the CircuitPython drive and your code will not work if dragged there.
This guide was first published on Oct 08, 2019. It was last updated on 2019-10-08 16:28:32 -0400. This page (Program It in MakeCode) was last updated on Oct 17, 2019.