FOR THIS PROJECT YOU WILL NEED
- A computer with access to the Internet
- An Adafruit Circuit Playground Express
- A dollar store toy
In the 1980s and 90s there were many commercials for “innovating” products. From plants that were pets to clap controlled room lights and this is where we draw inspiration for this issues tutorial. We shall use a dollar store toy as a diffuser to create a subtle light, but what electronics shall we use?
For this we shall use an Adafruit Circuit Playground Express which has the two components needed for this project. We have a small microphone which we shall use to detect when we clap to control the LEDs. For the LEDs we shall use the built in 10 neopixels and what’s more we shall have these LEDs choose a random colour each time they turn on.
In this project we will learn about
- Events and how we can use them to start our code.
- Conditional test and how they can be used to change the flow of our code.
- Inputs and outputs, and how the computer will understand what to do.
- Variables and how we can use them to store the state of our lights.
- Generating random numbers.
So let’s get started!
Writing the code
Plug in your Adafruit Circuit Playground Express, we shall refer to this as CPX during the tutorial. The CPX will appear as a USB flash drive, but for now just leave it plugged in and we shall work with it later.
To write the code we shall use the Makecode editor at https://makecode.adafruit.com which is a block based way to write code.
On the left of the screen we have a CPX simulator which we can use test our code before we write it to the CPX. In the centre we have a section where the blocks that we use to code are stored. These blocks are stored based on their function/use. For example we can control the LEDs using the LIGHT blocks, all we need to do is click on the block to see all of the blocks that can be used to do this. Then we drag the blocks into the large grey coding area to the right of the screen.
To start our project we shall go to the LOOPS section and find the “on start” block. Drag this into the coding area. This is a loop that will run only once and that is when the CPX is powered up. But what shall we put inside this loop?
When our CPX powers up we need it to create a variable, a container that will hold data which in this case is a toggle, to record the state of the LEDs, with 1 meaning ON and 0 OFF. To create a variable we go to VARIABLES and click “Make A Variable…” Call the variable “toggle” and then drag “set toggle to 0” into the “on start” loop.
Now when the CPX powers up our toggle variable will be set to 0. We now move on to create another new loop.
This new loop is called “on loud sound” and it lives in the INPUT section. Drag “on loud sound” into the coding area.
Inside of this loop we shall insert a conditional test, this is found in LOGIC and it is called “if true then…else” drag this block into the loop.
For the next step we shall use our conditional test to check two values against one another. This is called a comparison and for this we need the “0 = 0” block found in LOGIC. Drag this block so that it replaces the “true” part of the conditional test.
The comparison will test to see if the value of our “toggle” variable is equal to 0. To do this we go back to VARIABLES and drag the “toggle” block into the first “0” of the comparison.
So what happens if the value of “toggle” is 0? Well it will first turn on the neopixel LEDs on the CPX. To do this we use the “set all pixels to …” block from LIGHT. In fact we can drag two of these blocks into the code, one for the “if” section and another for the “else” For the block in the “else” section, change the color to black, as this will turn off the LEDs.
To set the color of our LEDs we shall use a block called “red 255, green 255, blue 255” found in the LIGHT section. This uses numbers from 0 to 255 to control the intensity of each color. So with this we can mix our own colors! If you have used Photoshop to edit images you will know this as RGB color values. Drag this block into the first “set all pixels” block so that it replaces the existing color.
So how can we create a random color each time we turn the light on? Well by going to the MATH section we can use the “pick random 0 to 10” block. Drag this block three times and use it to replace the 255 value for red, green and blue. Then change the values so that they read “0 to 255” for each color.
Now for the last two blocks of code! For this we need to go to VARIABLES and drag two “set toggle to 0” blocks. Place one inside the “if” part of the conditional test, and change the “0” to a “1” This will ensure that our CPX knows that we want the light to be on, and stay on. The second “set toggle to 0” will go in the “else” section and it will activate when we clap to turn the LEDs off. This relies on the conditional test skipping the first test as our “toggle” variable is storing the value “1” so that means our “else” condition is true, causing the LEDS to turn black (off) and for the “toggle” variable to be reset to 0.
All of the code for this project should look like this, check your code before moving on.
To save our work we need to go to the bottom of the screen and use the save dialog. Name your project “clap-lights” and click on the Download box.
We now have a copy of the code in our Downloads folder. In order to save this to our CPX we need to follow the instructions given in the editor.
Our CPX is already connected to the computer, so we can just press the RESET button and see that our LEDs light up green. A new drive will appear called CPLAYBOOT, drag the project code file from the Downloads folder into the CPLAYBOOT drive and let go. After a few seconds the board will be programmed and ready. Place the dollar store toy over the CPX, you may need to trim or cut a hole for the USB cable.
Clap your hands and few times. Does the CPX light up? Do the LEDs turn off?
Each time you clap the lights on they will turn on in a different color to before. All we need to do is clap to turn the lights on and off!
Well done! You have made your own clap activated night light!
So what have we learnt?
- How we can use sound as an input.
- How to use a conditional test to control the flow of the code.
- How to save data in a variable.
- How to use a comparison test.
- How to control neopixels
How can we extend this project?
This project shows how we can control anything using sound, but we chose to control the LEDs as they are built in to the CPX. But this control can be extended to the GPIO (General Purpose Input Output) pins so our clap could be used to control a motor, fan, sound effect. This project can be whatever you want it to be!