If you're new to MakeCode, head to this guide to get started. Once you're familiar with MakeCode on your Circuit Playground Express, return here.
We'll enhance the Gauntlet with a light and sound effect that reacts to punching. To do this we'll do these three things:
- Set the NeoPixel ring of the Circuit Playground Express to moderately bright yellow.
- Detect quick changes to the position of the Circuit Playground Express using the built-in accelerometer
- Adjust the NeoPixels and play a sound effect when a punch motion is detected.
The first thing we'll do in our code is turn on the NeoPixels with a yellow color at moderately high brightness. To do this we'll use an
on start block from the LOOPS category, and then place inside of it a
set brightness block set to
set all pixels to color block set to
yellow from the LIGHT category.
Detecting a punching motion is very easy in MakeCode -- we'll use the
on shake block from the INPUT category.
When we detect the shake, we'll first have the NeoPixels turn bright white like a powerful flash! Add a
set brightness block set to a value of
255 as well as a
set all pixels block and change the color to
Here's a speed tip: Instead of getting these two block from the LIGHT category, you can copy the existing set of the same blocks from your
on start block by right-clicking them and selecting duplicate from the pop-up menu!
After the light flashes to bright white, we'll play a sound effect. You can use any of the pre-made
play sound __ until done block choices from the MUSIC category. Or make your own by using the
play tone at __ for __ blocks also from the MUSIC category as shown here. This allows us to craft our own unique sound effect!
I set a repeating, trill-like pattern with short tone durations by duplicating the
play tone block a few times, alternating between
3000 Hz and
2600 Hz, and setting the duration to
1/16 beat. I set the final note to play for a
Test out your sound while working on it by clicking the SHAKE button that appears on the top of the simulator Circuit Playground Express inside of MakeCode.
The sound effect plays back too quickly at first, so I decided to add a
set tempo to __ (bpm) block from the MUSIC category into the
on start block. After trying different values, I decided upon
Finally, we want to return the NeoPixels to the original moderately bright yellow color. Add a
set brightness 120 and
set all pixels to yellow block set to the bottom of the
on shake block.
You can take a look at the final code here:
Your code is complete! You can now download it and drag it onto your Circuit Playground Express and try it out on the real device! Power it up, give it a good shake, and watch and listen for the cool effects!