MakeCode is a block based language that makes it easy to program the Circuit Playground Express. You can drag and drop code blocks to make things light up. To get started, we'll need to head over to the Adafruit MakeCode website and follow the steps below.
- Plug in your Circuit Playground Express with a USB Cable
- Press the RESET button. Green light means you're ready to MakeCode
- Download the UF2 file and drop it onto CPLAYBOOT.
When the lunchbox is opened, a strip of NeoPixel flash on/off and a Power Up sound effect is triggered. Then, the NeoPixels on the Circuit Playground Express sparkle after the NeoPixel strip flashes.
The accelerometer on the Circuit Playground Express can detect when it's facing up and down. In the code, we can tell the NeoPixel LEDs to flash on and off when the Circuit Playground is facing up. We can also add sound effects and animate the on-board NeoPixels.
The full program is available to download and modify. Click the Adafruit flower logo in the top left to open in a new window. There you can play with the Circuit Playground simulator, modify code blocks and upload code.
Below the code embed is a step by step breakdown of the program. Read through it to get an idea of how to build this program, block by block. If you get stuck, re-read the steps and reference the guide.
We need to setup the neopixel strip by assigning it to a pin on the Circuit Playground. We can do this by creating a variable, setting up a strip and configuring which pin it's on and how many pixels are in the strip.
- Drag and drop an On Start block from the Loops menu onto the canvas.
- Create a variable called "strip" using the "Make Variable" button in the Variables menu.
- Click on the "Light" menu and click the "NeoPixel" submenu.
- Drag the "Set [strip] to create strip" block into the On Start block.
- Click the plus(+) button and select A1
- Click the plus(+) button again and type in 30 (the number of pixels in our strip)
- Drag and drop a Strip Set Brightness block from the NeoPixel submenu to the bottom of the Set Strip block. Type in desired brightness (255 is max).
Now that we have our external neopixel strip setup, we can start programming events to trigger animations and sound effects. Let's think about how we can do this. In this project, we placed the Circuit Playground inside the lid of the lunchbox. So when the cover is open, the board is facing up and when it's closed, it's facing down.
- Drag and drop "On Shake" block from the Input menu onto the canvas.
- Click the "Shake" option to open the dropdown and choose "Face Up".
- Drag and drop "[Strip] set all pixels to [color]" into the "On Face Up" block.
- Choose a color by clicking on the color box and picking from the swatches.
- Drag and drop "play sound [power up]" into the "On face up" block.
- Choose a sound effect by clicking on the dropdown picker.
- Download code and test!
When the board is facing up, it should light up the strip of neopixels and play a sound. That's great, but how do we get the lights to flash on and off?
In order to flash the NeoPixels on and off, we'll need to create a repeat block and loop between turning the pixels black and white (or in this case, on and off).
- Drag and drop "repeat  times" block from the "Loops" menu onto the canvas.
- Insert a "[strip] set all pixels to color" block into the "repeat  times" block.
- Choose the color black
- Drag and drop "pause ms" block from the "Loops" menu below the "strip set all pixels" block.
- Type in or choose the value "100".
- Right click on the "[strip] set all pixels to color" block and choose duplicate.
- Place below the "pause" block
- Change the color to something other than black.
- Add another "pause" block to the bottom of the second "strip set all pixels" block.
And now we have a repeat block that we can insert into our "Face Up" block. You'll want to replace the first "strip set all pixels" block with our "repeat" block.
You easily change up the options in the various blocks to make new events and triggers! Why not change on face up to on shake? Now you can shake the launch box to make lights or sounds! Add an on face down block to make it do other things when the lunchbox is closed! The options and combinations are enormous so start experimenting!