We set up a variable at the beginning called
ledMode. We'll use this variable to let the Circuit Playground know which mode we want, then we'll set up the two onboard buttons to cycle through the modes.
From the LOGIC tab, drag an
if / then / else block into your
forever loop. Drag a
0=0 comparison block in to replace the true. Then replace the first
0 with the
Now head over to the ADVANCED > FUNCTIONS tab and find your
twinkle function. Drag an instance of
call function twinkle inside this conditional block.
Click the + button at the bottom of the conditional block to add more spaces. We have 5 modes, plus we want to add one mode where all the lights are off, for 6 modes total. Create four more spaces, copy/paste the
ledMode = 0 block into each, and fill each with a function as shown. Be sure to change the
0 to count all the different modes.
You can change the order if you'd like, but the order shown is designed to allow you to get a solid or gradient animation in any color, since it comes after the RainbowFade animation.
Now let's add a final mode that turns all the lights off. From the LIGHT > MORE tab drag an instance of
set strip all pixels to red into the
else block. (You may need to mess with the
- buttons to get it to read
else and not
else if). Change the color to black. Drag a
strip stop all animations block and a
strip clear block in there as well, and apply them to both
strip2. We want to make extra-sure that all the lights go off when we're in mode 6.
Now we'll set up the A and B buttons on the face of the Circuit Playground to change modes. We'll go forward through the modes with A, and backward through the modes with B.
There are all kinds of ways to trigger mode changes! If you'd like, you can use the onboard capacitive touch pads, or one of the other sensor triggers like tilt or shake to change modes, or even change modes if a loud sound is heard. Get creative! I like using the onboard buttons with this design because they're easy to find with my fingers without looking, and I can change modes without thinking about it too much.
From the INPUT tab, drag an instance of
on button A click into your workspace.
From VARIABLES, drag
change item by 1 and set it to
This will make button A count up through all the modes. We need to add some code so that when it passes mode 4, it will reset to 0 so it cycles through over and over. Grab another
if/then loop from the LOGIC tab and set it up as shown:
Since our modes are numbered 0-4, then as soon as the ledMode counts to 5 (or anything greater than 4) it will reset back to 0.
Copy/paste this entire block to set up button B. Change the values around as shown to make it count down instead of up.
That's it! We're done. Download the code to your Circuit Playground and click the buttons to make sure it works the way you expect.