To make the mask light up when you talk, you'll be programming the Circuit Playground Express using Microsoft MakeCode. It's really easy to learn and fun to play around with! You can use it online at the Adafruit MakeCode site, or offline with the Windows 10 app on your computer. If you've never used MakeCode before, there's a guide to getting started here.
First, you'll figure out how to program the ring of lights on the CPX to look like a mouth opening, closing, and smiling. Then you'll add code to trigger the lights using the sound sensor/microphone built into the board.
The instructions below show two ways to trigger the lights respond to your voice. One uses a pre-built code block called
on loud. You can check out the completed code of that version by clicking the button here.
If you need to fine-tune your code to make your talking mask work better, go on to the steps that show you how to adjust the sound sensor to respond to the volume that you set using the
sound level block. This version was adapted from the Sound Activated Shark Mask learning guide. The button below takes you to sample code using that method:
The first step is to make the lights flash in a pattern that looks like a mouth talking. To do this, imagine turning the Circuit Playground Express board simulation on the screen sideways, so that the lights go across the top and the bottom.
When all the lights are lit, the "mouth" is open.
With just the bottom row lit up, the lights form a little smile.
The pattern of lights used in the example shown here is:
- The open mouth flashes on and off twice.
- The smile flashes on for a moment, then turns off.
You'll be building this part of the code using the
show ring block from the LIGHT category of blocks in the menu column. Change the colors of the ring of lights by clicking on a color in the center of the diagram and then on the lights you want to change.
To test your program, insert the code inside the
forever block that's already in the workspace whenever you open a new project. Here's how:
Program the Open Mouth
First, drag a
show ring block into the workspace and insert it into the forever block. Change all the lights to white.
Then right-click on the
show ring block to duplicate it, or just drag a new one over from the menu and place it under the first one. Click on the gray space in the middle of the ring, then click on every light to turn them all off.
The lights on the simulated board should start to flash on and off continually.
Program the "Smile"
Next, take another
show ring block, attach it underneath the first two blocks, and turn just the lights on one side to white. (Which side depends on where you put the opening for the battery pack wire on your mask -- the JST cable plugs into the port shown at the bottom of the simulated board.)
Duplicate the block that turns all the lights off and put it at the end of the stack.
Add Repeats and Pauses
To create a pattern of flashes where the whole ring flashes twice, drag a
repeat block from LOOPS and place it around the first two
show ring blocks. Change the number of repeats to 2.
Then, to adjust the timing, go back to LOOPS and drag a
pause block into the stack after the repeat. Change the timing to whatever you like -- in this example, 300 milliseconds (about a third of a second). Add another
pause block after the third
show ring block. This one is set to 700 ms in the example.
Now it's time to add the sound sensor to trigger the pattern of lights!
on loud block
From the INPUT category on the block menu, drag and drop an
on loud block in the workspace.
To move the stack of code from the
forever block, grab the top of the
repeat block. The entire stack will come with it. Drag it inside the
on loud block.
Look at the simulation of the board. Near the bottom left corner, a small round sound level indicator has appeared. It's at the midway point, which 128. (The highest it can go is 255.)
You can grab and move the indicator up and down, like a window shade. When that indicator hits about 191, the
on loud command triggers the lights to flash.
Follow the instructions on the MakeCode guide to download your program to your CPX board.
Then test to see whether this will work for your mask by putting the CPX board near your mouth. Say something in your normal speaking voice.
If the lights don't turn on, go on to the next section and try creating some code that lets you adjust the sound sensor.
sound level Block
From the LOOPS category on the block menu, drag and drop a
while block in the workspace. Insert it inside the
forever loop and around the rest of the stack of code, starting with the
Next, from the LOGIC category, drag and drop a pointy-tipped
0 = 0 comparison block. Change the "equal" sign to a "greater or equal to" sign. (It looks like ≤.) In the second oval, type 140 over the 0. Then drag the block over the pointy-tipped "true" space on the
while block and let it click into place.
Now go to the INPUT category and scroll down until you see some oval-shaped blocks. Drag the
sound level block into the workspace. Drop it over the first oval on the comparison block.
That's it! You've set the sound sensor to trigger your code when the sound level gets to 140 or above. Again, follow the instructions on the MakeCode guide to download your program to your CPX board.
Test out this setting the same way you did before and adjust it lower or higher to work with your mask.
When everything's working, go to the last page to see how to insert the electronics into your mask.