Getting Started with MakeCode
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.
Code Flow
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.
On Start - Light
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 120
and set all pixels to
color block set to yellow
from the LIGHT category.
On Shake - Flash
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 white
.
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!
Add Sound Effects
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 1/4 beat
.
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.
Tempo in Beats per Minute
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 60
bpm.
Back to Yellow
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!
Page last edited March 08, 2024
Text editor powered by tinymce.