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.
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!
To begin, use an
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
servo A1 to  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
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
light dark block to tell the CPX to make the box do its thing.
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.
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
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.
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  ° 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.
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  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!
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.