Build a walking piano with the RaspberrySTEM Creator Kit
Lessons
Each lesson takes the user through progressively more involved programming and hardware projects. Project builds are illustrated and discussed very clearly. Code for a project is usually presented complete and described as needed. For more in-depth knowledge, hyperlinked "Concepts" pages go beyond the project at hand.
Hardware concepts presented include providing power, breadboarding, working with LEDs and buttons, using GPIO pins as input or output, adding a circuit board, and reading a schematic. Programming concepts cover lists, strings, numbers, and variables; assignments and math; conditional statements and for
and while
loops; and functions and modules.
Project
Three of the lessons included in the RDE show you how to build a piano, starting with four buttons assigned four notes of equal length, then modifying the program to play notes that respond to the length of time you press a button. The final lesson shows you how to create you own piano "key" and wire it to the RPi2.
The Fritzing diagram in Figure 7 shows a button on the breadboard attached to GPIO15. The button outside the breadboard represents the self-made "button" presented in the final piano lesson; it is wired in parallel with the breadboard button, so pressing either button plays the note assigned to GPIO15. The black wire to the blue rail is ground (GND). Both buttons have one side wired to GND. The blue and yellow wires attach the other sides of the buttons to the correct pin. The code to play a C is shown in Listing 1; rstem.button
and rstem.sound
are RaspberrySTEM modules that provide the additional code needed to handle the built-in speaker and button functions.
Listing 1
Playing a Note
01 from rstem.button import Button 02 from rstem.sound import Note 03 import time 04 05 button = Button(15) 06 note = Note('C') 07 08 while True: 09 if button.is_pressed(): 10 note.play() 11 time.sleep(0.01)
I wanted to expand on Project 14: Build a Walkable Piano and create a walking keyboard of eight notes. The first step was to map my project (Figure 8). Each key must be wired to its own GPIO pin. Next, I had to make eight piano keys.
After gathering the materials (Figure 9), I set about creating the keys, which are essentially home-made buttons. One key is made with two pieces of cardboard, each covered with a wide strip of foil and taped together like a book (Figure 10). Bumper strips are added along each side of one foil-wrapped piece to keep the two sides from touching when at rest. When wired to the RPi2 as shown in the Fritzing diagram, the button plays a note when pressed. Detailed instructions for building a button are described in the Project Guide.
After building all my keys, I wired each one to the GPIO connector board (Figure 11) as diagramed in my original plan and according to the Fritzing diagram. For the wires attached to the key, I exposed a good 1 inch for taping to the foil. On the other ends, I stripped just enough insulation to insert the wires into the breadboard.
The code in Listing 2 defines every key (Button
) and its corresponding GPIO pin in a list. The list in line 6 assigns a Note
to each button; it must have the same number of elements as the buttons
list and appear in the order in which they should be assigned to a button. The while
block looks for a pressed button and plays the appropriate note.
Listing 2
Walkable Piano
01 from rstem.button import Button 02 from rstem.sound import Note 03 import time 04 05 buttons = [Button(15), Button(18), Button(23), Button(24), Button(25), Button(7), Button(16), Button(21)] 06 notes = [Note('C'), Note('C#'), Note('D'), Note('Eb'), Note('E'), Note('F'), Note('F#'), Note('G')] 07 08 while True: 09 for button, note in zip(buttons, notes): 10 if button.is_pressed(): 11 if not note.is_playing(): 12 note.play(duration=None) 13 else: 14 note.stop() 15 time.sleep(0.01)
Although the resulting build (Figure 12) is indeed walkable, it is much easier to play by hand. The program can play more than one note at a time, but you might hear some trilling interference. Walking the keyboard, therefore, requires energetic jumping from key to key for clean sound output.
« Previous 1 2 3 Next »
Buy this article as PDF
Pages: 5
(incl. VAT)