Click on the LOOPS tab and drag an instance of on start
onto the workspace. (It may already be there) Anything inside this bracket will happen just once, when the Circuit Playground is powered up. You'll also see a FOREVER loop. Anything in this bracket will happen over and over, forever.
First we'll create our variables. Click the VARIABLES tab and add a variable called strip
, and one called bright
.
Set Up the Lights
Click on the LIGHT tab. Any block inside this tab will control the lights on the face of the Circuit Playground. We don't need to do anything to get these lights set up, they're ready to go.
Click on the the MORE tab. Blocks in this tab will control the NeoPixel strip we added. We need to tell MakeCode we added lights, so drag an instance of set strip to create WS2812 on LED with 30 pixels
into your on start
loop.
I soldered my NeoPixel strip to pin A1, and my strip has 42 pixels in it, so I changed this line so it reads set strip to create WS2812 strip on A1 with 42 pixels
Brightness Setup
I want to be able to change the brightness slowly over time, so I've made brightness into a variable. This way we can use simple math to fade the lights up or down.
In the on start
block, set your global brightness. I've set mine to 150, which is medium-bright (your max here is 255).
Then in the forever
loop, I've set up the brightness of both the onboard lights (found in the LIGHT tab) and the NeoPixel strip (found in the MORE tab) so they use the bright
variable.
Flame Animation
MakeCode comes ready with a few premade LED animations, one of which is a flame-type effect. I like this well enough on the Circuit Playground face, but I don't think it looks quite as good on the LED strip; I want a little more control over the flicker for the body of the dragon. No worries, we can use both.
Find the FUNCTION tab under ADVANCED. Create a new function and call it flame
. Then, add FUNCTION --> call flame
to the forever
loop. We'll move it out of there later, but while we're building the animation it's nice to have it just run.
Now you can mess around with the different light controls available until you have something that looks like a flame. Here's what I ended up with.
The first two lines deal with the pixels on the Circuit Playground's face. I gave a background color of orange, then added the pre-made flame animation over the top.
The rest of the lines deal with the NeoPixel strip. I gave it a background gradient from orange to red, then used the photon function, which sends a colored pixel shooting up or down the strip. I used a black pixel, which turns the lights off (black) for just a moment, and played around with adding more photons until I had a flickering effect that I liked. Adding the random numbers (under the MATH tab) gives it a really nice randomized flicker effect that looks organic.
Testing
Test your code by clicking the Download button. You may get the option to pair your board to your computer -- this is helpful as you're testing because then download becomes just one step. If you don't want to pair the board, just drag the downloaded file onto the MAKECODE or CIRCUITPY drive that appears when you plug in your board and press reset.
Text editor powered by tinymce.