Now we'll take everything we learned and put it together!
Be sure to save your current code.py if you've changed anything you'd like to keep. Download the following file. Rename it to code.py and save it to your Circuit Playground Express.
# SPDX-FileCopyrightText: 2017 Kattni Rembor for Adafruit Industries # # SPDX-License-Identifier: MIT from adafruit_circuitplayground import cp while True: if cp.switch: print("Slide switch off!") cp.pixels.fill((0, 0, 0)) cp.stop_tone() continue if cp.touch_A4: print('Touched A4!') cp.pixels.fill((15, 0, 0)) cp.start_tone(262) elif cp.touch_A5: print('Touched A5!') cp.pixels.fill((15, 5, 0)) cp.start_tone(294) elif cp.touch_A6: print('Touched A6!') cp.pixels.fill((15, 15, 0)) cp.start_tone(330) elif cp.touch_A7: print('Touched A7!') cp.pixels.fill((0, 15, 0)) cp.start_tone(349) elif cp.touch_A1: print('Touched A1!') cp.pixels.fill((0, 15, 15)) cp.start_tone(392) elif cp.touch_A2 and not cp.touch_A3: print('Touched A2!') cp.pixels.fill((0, 0, 15)) cp.start_tone(440) elif cp.touch_A3 and not cp.touch_A2: print('Touched A3!') cp.pixels.fill((5, 0, 15)) cp.start_tone(494) elif cp.touch_A2 and cp.touch_A3: print('Touched "8"!') cp.pixels.fill((15, 0, 15)) cp.start_tone(523) else: cp.pixels.fill((0, 0, 0)) cp.stop_tone()
Now, we're going to break down the code.
The first thing we do is setup the slide switch to be able to turn the project on and off.
while True: if cp.switch: print("Slide switch off!") cp.pixels.fill((0, 0, 0)) cp.stop_tone() continue
This is the beginning code. All of the code in this project is inside this while
loop. The first thing the code does inside the loop is check whether the slide switch is to the left. For the purposes of this project, left means "Off." If it is off, the code prints, "Slide switch off!
", turns off the LEDs, and stops playing any sound. Then, continue
ensures that the code doesn't stop here, and instead continues on to the next sections.
Next, we're going to code each touch pad to play a different tone and display a different color on the NeoPixels. We'll leave the print statements in so we have some feedback in the REPL.
A piano plays low to high from left to right. To allow our "keys" to be in the correct configuration using the alligator clip jumper wires to spread them out, we'll need to do things a bit out of order. We'll code the touch pads in the same order that we'll place the limes, starting on the left. We'll start with A4:
if cpx.touch_A4: print('Touched A4!') cp.pixels.fill((15, 0, 0)) cp.start_tone(262)
If you touch A4, the code prints "Touched A4!
" to the REPL, lights up the NeoPixels red, and plays a tone at 262 Hz (which is Middle C). This is the first "key" in our touch piano! A5, A6, A7 and A1 are essentially the same. For each one, we change the message, color and tone. These will use elif
statements instead of if
.
There are 7 touch pads. We're going to include 8 tones to create a full scale. To simulate the 8th touch pad, you will touch both A2 and A3 at the same time. Therefore, we have to write the code to tell the difference between A2, A3 and both at the same time.
Let's take a look:
elif cp.touch_A2 and not cpx.touch_A3: print('Touched A2!') cp.pixels.fill((0, 0, 15)) cp.start_tone(440) elif cp.touch_A3 and not cpx.touch_A2: print('Touched A3!') cp.pixels.fill((5, 0, 15)) cp.start_tone(494)
If you touch A2 and not A3, the code prints, "Touched A2!
", displays the given color and plays the given tone. If you touch A3 and not A2, the code prints, "Touched A3!
", displays the given color and plays the given tone.
The simulated touch pad looks like this:
elif cp.touch_A2 and cp.touch_A3: print('Touched "8"!') cp.pixels.fill((15, 0, 15)) cp.start_tone(523)
If you are touching A2 and A3, the code prints "Touched "8"!
", displays the final color and plays the final tone.
The last piece of the code says if you're not touching anything, turn off the lights and stop playing the tone.
else: cp.stop_tone() cp.pixels.fill((0, 0, 0))
Now, if you touch the pads, you'll hear all the tones and see all the colors we assigned!
Now we'll add our fruit keys!
Power off your Circuit Playground Express. Attach one alligator clip jumper wire to each touch pad.
You'll want to arrange them so they don't touch. You can slightly crease the wires on your clips to persuade them to stay in a particular place. If the wires are touching, they can cause the attached pads to act like they're being touched.
This arrangement works consistently:
The clips are attached in a way that allows them to fan out, and avoid interference from the other clips.
Note the clip attached to A7 is creased twice to shorten it up.
Now you'll attach the fruits. With key limes, it's easiest to simply choose a location and puncture the skin with the male jumper wire end.
Now arrange the the limes in a line, but not too close together. As with the wires, placing the limes too close together can cause interference.
Plug in your Circuit Playground Express. You may need to press the reset button after initially powering it on if it's plugged into a battery.
Now it's time to play! This video shows a song being played on the Piano in the Key of Lime. Have fun playing!
Text editor powered by tinymce.