7

(avery) #1
FORGE

Next, let’s set all the pixels to black (off) and get
ready to sample a colour. Go back into the Light
menu, click on the NeoPixel menu, and grab the
‘strip set all pixels to (red)’ block. Drag it onto the
workspace and drop it in the On Start loop. To
change the colour setting to black, you’ll need to
head back into the Light menu, scroll down to the
Colour section, and grab the round module labelled
‘red’. Drag this module onto the workspace and
drop it directly on top of the red colour block to
replace it. You can then click on the word ‘red’ and
select ‘black’.
To set the on-board NeoPixel ring to black, simply
duplicate this block, and drag the copy into the On
Start loop under the original block. Head back into
the Light menu, then the NeoPixel menu and find
the round module for ‘onboard strip’. Drag this
module onto the workspace, and drop it on top of
the ‘strip’ variable to replace it with ‘onboard strip’.
That’s it for setup!


MAKE THE MAGIC HAPPEN
We’re getting there! All that’s left is to tell the Circuit
Playground Express to read the light sensor and set
the on-board strip to the same colour as it detects.
Start by going into the Input menu and finding the
On Light Dark loop. Drag it onto the workspace.
Now, anything we put inside this loop will happen
when the light sensor is blocked by an object and it
detects ‘dark’.
Let’s have it take a colour reading and display
that colour on our NeoPixel strip. Head back into
the NeoPixel menu (remember, it is nested inside
the Light menu) and grab the ‘strip set all pixels
to (colour)’ block, and drop it into the On Light
Dark loop. Replace the colour in the block with the
‘ambient colour’ module, which you’ll find in the
Input menu. Now, if you wave an object over the
light sensor, the NeoPixel strip should change to
match the colour of the object!
It’s a magical effect, and we can embellish the
magic by adding a few more actions. Let’s make the
on-board NeoPixel ring display the rainbow animation
once the colour sensor takes its reading. From the
Light menu, drag the ‘show animation rainbow for
500 ms’ onto the workspace and add it to the On
Light Dark loop. The Circuit Playground Express can
also play sounds, so let’s add a magic wand sound
as well. In the Music menu, grab the ‘Play sound
Power Up until done’ block and add it to the On
Light Dark loop. Click on the drop-down menu to
change ‘Power Up’ to ‘Magic Wand’. To return the
CPX to its ready state, let’s set the on-board pixels


to black again. Simply duplicate the final block of the
On Start loop, ‘onboard strip set all pixels to black’,
and drop it into the bottom of your On Light Dark
loop. Your program is complete!
You can test this out on the Circuit Playground
Express simulator by clicking and dragging on the
yellow light sensor input icon to the left of the
CPX image. You should see the on-board rainbow
animation and hear the magic wand sound. If
everything works as expected on the simulator,
click Download, and follow the instructions in
MakeCode to install the code on your Circuit
Playground Express.

HAVE FUN COLLECTING COLOURS!
Now you’re ready to sample colours from the world
around you! Remember that it may take some
experimenting with the light sensor threshold values
to get a solid reading at first. Look for the eye icon
on the Circuit Playground Express – hold the object
in that area a few centimetres away from the board.
When the sensor takes a reading, you’ll hear the
satisfying magic wand sound, and see your bag
change colour. It’s like magic, but better, because
you made it!
We’ve just scratched the surface with sensors
in this project, and because the Circuit Playground
Express is easy to program on the front of your
bag, you can modify this project in lots of ways. In
addition to the light and colour sensor, the CPX also
has an accelerometer, a temperature sensor, a sound
sensor, and an infrared receiver and transmitter on
board. How are you using these sensors in your
projects? Show us at @HackSpaceMag!

Below
The MakeCode
editor is easy to use
and powerful enough
for our needs

Don’t carry a clutch?
You can apply the
same circuit and
code to almost
any clothing or
fashion accessory.

QUICK TIP

Free download pdf