This little board is fantastic for developers who are starting their hardware journey. It's packed with sensors and outputs for sound, light, motion, and more. Here, we'll use the built-in sound level sensor and ring of 10 RGB LEDs.
Since there are only 10 LEDs, we'll be using two light levels for each one as we count down. At the end, you get a satisfying rainbow light pattern and "da-ding!" sound. :)
To keep the whole thing touchless, we're using the CPX's sound sensor to start the timer when you clap your hands.The Code
1. Handwash timer
I want to create positive associations with washing hands, so we start with a full green LED ring when the sensor trips.
MakeCode has a handy "index" variable that makes it easy to create for loops. I've set up a for loop that will:
- wait 1 second, then turn the first LED to orange; then,
- wait 1 second again, and turn that LED off.
The index variable automatically advances each time the loop runs, until it reaches a certain value – in this case, 10 (the number of LEDs). So, here we have a simple countdown.
And then, at the end, you get the little audiovisual reward cue :)
Note: I had to disable the cute audio feedback, because it would retrigger the loud sound sensor. You could probably avoid this by turning down the volume, but I'd recommend choosing a sound that you REALLY like to hear all the time... 😅
I included a variable, "washing", so that I can activate the night light when the timer isn't running. The variable goes to 1 while we're washing, then back to 0.
2. Night light
For a "free" extra feature, I put in a low ambient nightlight function. This forever loop first checks the "washing" variable, and if it isn't active, checks the light sensor to see if it's below a certain threshold – in this case, 50 out of 128.
Then, it turns on a pattern of red and orange LEDs, which should provide some light without burning my eyes out of my skull. If you want, you can also add a Light block to set the LEDs' brightness lower.Deployment
To upload the code, plug the CPX into your computer and double-click the RESET button; this makes it show up as a flash drive, ready for you to drag-and-drop the.uf2 file onto the board. That's it for programming!
I want this to be simple, but water-resistant and easily cleaned, so I've dropped it into a Ziploc bag and left it plugged into a wall socket.