Let's start by getting our code uploaded, so we can test the lights immediately when we connect them later on.

MakeCode is an easy way to get up and running with the Circuit Playground.  No prior coding knowledge is needed, and it's an easy way to experiment and learn to think like a coder. You just drag and drop code blocks, like building with Lego. MakeCode makes coding fun!

Head over to this Intro to MakeCode guide for more info on getting started with MakeCode.

How To Upload the Code

  1. Open the project with the button above and click the blue DOWNLOAD button near the bottom of the project.
  2. Plug your Circuit Playground into your computer via its USB port and click the "reset" button.  All the lights will turn green and your Circuit Playground will appear as a drive on your computer called CPLAYBOOT
  3. Drag the code you just download onto this drive to program the Circuit Playground -- like putting files on a USB stick. That's all you need to do.

Note: If you plug in the board and you see a drive called CIRCUITPY appear, press the reset button again (double-click) to get to CPLAYBOOT.

How To Use It

We'll attach a wire to pin A3 and connect it to copper tape on the outside of the torch. This copper tape will act as our "switch" using the magic of capacitive touch. 

There are 3 modes: Fairy lights only, Fairy lights and Circuit Playground lights, and Off.

The torch will start up with the fairy light strand lit. Touch pin A3 and the Circuit Playground's lights come on, illuminating your torch with an orange flicker animation. Touch it once more and the torch turns off.

How to Customize Your Code

You can add your own modes, give yourself more speed variations, or set it to start up on your favorite mode. The possibilities are endless! Poke around and change some stuff, and if it stops working you can always come back here and start fresh. Make it yours.

Take a look at the green boxes on the left: on start and forever. These are found under the LOOPS tab at the left. Anything in the on start block runs just once, when the board first powers up. Anything in the forever loop runs over and over, forever. 

On Start Block

If you add or remove modes, change numModes to reflect the total number of modes (including mode 0). We have three modes: 0 (fairy lights only), 1 (fairy lights plus CPX), and 2 (off), so we've set numModes to 2. It's a bit confusing, but computers like to count starting with 0!

To select a different start-up mode, change set mode to 0 to the number of the mode you want to appear first. 

You can also change the Circuit Playground brightness by choosing a number between 0 and 255.

Forever Block

Here is where we set up the order of our modes, and turn the fairy light strand on and off. If you want to add more modes you can do it here by clicking the + button at the bottom. Be sure to increment the mode number in each block you make, and don't forget to change numModes in the previous block if you add or subtract modes.

The digitalWrite lines control pin A3, to which we've attached our fairy lights. We turn the light strand on by setting A3 to "high." Easy!

The digitalWrite code blocks are found under ADVANCED > PINS.

NeoPixel Control Function

The blue box in the middle contains our LED function. This is where we're telling the Circuit Playground what we want its onboard lights to do - in this case, flicker like a fiery torch.

The first purple block refers to which NeoPixel we want to light up. There are 10 NeoPixels on the face of the Circuit Playground, so using pick random 0 to 10 will get them all to flicker in a random pattern.

We're using the HSV method to control the lights. HSV stands for Hue, Saturation, and Value. Here's some more info about how HSV works to control lights.

We can choose any hue (color) between 0 and 255. I wanted a range of oranges and yellows so my values are between 5 and 30. If you want your torch to flicker in a different color, change the hue values around until you like what you see.

Saturation I left at 255 so the colors are vibrant and fully saturated. If you want a pastel or white light, you can pull the saturation numbers down to accomplish that.

Value equates to the brightness of the LED. I wanted them pretty bright so I chose a range of values from 225-255, 255 being maximum brightness.

Capacitive Touch Switch Control

This block is where we tell the Circuit Playground to scroll through the modes each time the pad labeled A3 is touched. When it reaches the last mode (defined by the numModes variable from the on start block) it resets to 0.

There are also other triggers available -- you can trigger a mode change or variation when you shake or move the Circuit Playground, or whenever a loud sound is heard, or a whole host of other options. Look under the INPUT tab to see them all.

This is an easy way to make your project react to its environment. Make the torch change color whenever you clap your hands!

This guide was first published on Aug 26, 2020. It was last updated on Aug 26, 2020.

This page (Code with MakeCode) was last updated on Aug 21, 2020.

Text editor powered by tinymce.