If you haven't used MakeCode before, this guide is a good place to start.
This example demonstrates how to make Sparky's mouth move in response to noise. MakeCode doesn't currently have a way to play back sounds from wav files. So instead, we'll make the sounds and Sparky will 'talk' along!
MakeCode is used to turn the microphone on your CPX into a trigger for the servo motor.
...you will need to make sure you have the CRICKIT extension installed in MakeCode. Detailed instructions on how to do that can be found in this guide.
Once you've got that taken care of, get the Makecode below or via this link.
This block of code tells the Circuit Playground Express that sits on top of CRICKIT to use its microphone to listen for sounds above a certain loudness threshold.
If it hears a noise, it's told to move the servo motor back and forth between 100-80 degrees.
There are also a couple
pause 100ms placed in the code to help smooth out the motion of the motor.
You can change the value in the
if sound level > 10 then to change how sensitive Sparky is to noises.
Decrease it to 1 and he'll start chattering at even the slightest whisper.
Increase it to 100 and it'll take a loud clap to set Sparky off.
The default value of 10 is already quite sensitive, even quiet sounds like tapping on his box will make Sparky start chattering.
If you'd like to make Sparky portable, you can use 3 AA batteries in a battery pack as a power supply. This should provide power to Sparky for about 3 hours of continuous interaction.
Problem: My servo isn't moving!
Solution: Make sure you have 5V power connected and that the small slide switch on the CRICKIT is set to "ON".
Problem: My Circuit Playground Express doesn't show up as CPLAYBOOT!
Solution: Your Circuit Playground Express board comes ready to work with CircuitPython, and will show up on your computer as a flash drive named CIRCUITPY when the Circuit Playground Express is connected via USB. To switch over to work with MakeCode, connect the board to your computer with a micro USB cable and click the small reset button in the center of the board.
When the Circuit Playground Express is in Bootloader mode, all the LEDs will turn red briefly, then green. Verify your status LED is also pulsing red. Your computer should show a new removable drive called CPLAYBOOT. Now you can copy the MakeCode file to the CPLAYBOOT flash drive.
If you enjoy MakeCode and want to continue exploring you can check out lots more MakeCode projects on the Adafruit Learn System.