MakeCode is a drag-and-drop code editor that makes it easy for anyone to make the Circuit Playground Express and Crickit do things, without having to delve too deeply into code.
I've included my completed project for folks who like to skip to the end and work backwards.
If you want to build it yourself and add your own custom options, start by going to MakeCode.com and choose the Circuit Playground Express. Then choose New Project.
Go to Advanced -> Extensions and choose the Crickit extension. It will install more blocks into your MakeCode window so you can program your Crickit.
Adding the Lights
Choose the LIGHTS block and select set brightness
. Drag it into your forever
loop, and set the brightness to whatever you want. I want my snakes to be screaming bright so I chose 255.
Then, add some color. I started by set all pixels to green
. Then I set pixel 7 to red
, since I wanted one red snake.
I also wanted some variation in the green snakes -- I had light and dark green colored snakes, so I wanted a light green and dark green glow. MakeCode only offers one green in its drag-and-drop editor, but luckily it's pretty easy to change in the Javascript code.
Find the hexidecimal code for the color you want. I have a desktop widget that lets me do this quickly, or you can search online for "hex code finder" and there are plenty of tools to help.
Back in your MakeCode project, drag an instance of set pixel color
into the forever
loop for each pixel you want to change.
Then, pop over to the Javascript tab at the top of the page. Change each pixel's hex code to match the color you want.
When you click back over to the Blocks tab, you'll see the new colors appear on the Circuit Playground image on the left. Hooray!
Adding the Motion
Crickit has four slots for servos, but for a costume project with a portable battery, it works best with just one or two. Servos take a lot of power, and while you may be tempted to use all four, the performance of your project will definitely suffer. Two servos is enough to add lots of motion. I put two snakes on each servo and so have four moving snakes on my headdress.
Click on the Crickit block and drag two instances of crickit set servo (1) angle to (90)
onto your workspace inside the forever
loop.
The simplest way to get the servos to move is to give them a value (0-180), then pause, then give them another value. Try adding the blocks shown in this image.
Download it to your project and see what happens. The servos move through their full rotation, pause one second, then move back again.
This works, but doesn't give us the slippery slithery motion we want from our snakes. It's fast and jerky instead.. useful for cat ear motion or the like, but for snakes we want to slow it down a bit. To do this, we'll use a for
loop and some variables.
We already have a pre-made variable called index
(if there is not one, use Make a Variable and call it index
).
Let's also add a variable called rightsnakes
and one called leftsnakes
. We'll also add one called index2
.
Drag two instances of set leftsnakes to 0
into your forever
loop up at the top. Change one to rightsnakes
and change the rightsnakes
integer to 180
. Now the two servos will start at opposite ends of their rotation so they can move in opposition to each other.
Also add a set index to 0
and set index2 to 0
. This will make sure these variables reset at the end of each loop.
Now, we will add a for
loop. This will enable us to move the servo just a little bit, over and over, with little pauses in between each step. This will effectively slow down the servo's motion to give us a more slithery feel.
Drag a for
loop (in the LOOPS bin) into your forever
loop after the color stuff. Change the default "4" to "180", so the loop repeats 180 times.
Then, place your servo commands inside the loop. Add a change
instance of 1 for leftsnakes
and -1 for rightsnakes
. Remember, leftsnakes
was set to 0 so we want to count up, and rightsnakes
was set to 180 so we'll want to count down.
Change the pause
from 1000ms to 40ms -- we want just a fraction of a second pause between each degree the servo turns.
Download this and give it a try. Success! The servos move slowly from one end of their rotation to the other end. Each time the for
loop is played (180 times total) the servos will move one degree.
You can adjust the speed by changing the pause
amount. Try different numbers, or maybe try adding a random number for variable speed snakes.
Now we need to add some code so they swing back the other way in a continuous back-and-forth.
We'll use our index2
variable, and set it up the same way as the last for
loop, except we'll switch leftsnakes
to -1 (so it counts down) and rightsnakes
to 1 (so it counts up).
Click Download and load the code onto your computer then from there onto your Circuit Playground Express. Test and see if you like it!
Note: make extra sure the servos can spin unobstructed. It's easy to get them tangled up in hair, crin, and light pipe. The easier they can swivel the more successful your project will be.
Page last edited March 08, 2024
Text editor powered by tinymce.