THERE IS NOTHING MORE ANNOYING than hearing your favorite song played poorly by an adoring minion. You could take the easy way out and tell the minion that he or she is terrible, but is that truly evil? What if you could make the minion think that he or she is an amazing musician and actually create an instrument that only uses one button to play an entire song. There is no way the minion could hit a bad note, and when he or she shows off to others, it looks like you have attracted a high-quality entourage! For this project you are going to create a simple thumb piano with house plants, a discarded box, and a programming trick in Scratch (see Figure 4-10).
Figure 4-10 Finished kalimba.
Cost: $
Make time: 30 minutes
Skill level:
You learned one way to program a song with variables in the last project; now you are going to go further with this concept by counting notes and using the power of combining “if” statements with “or” operators to play any song you want—all with just one key press.
We’ll show you how to create the classic “Heart and Soul,” but once you get the hang of creating music with math, feel free to create your own music with this one-button hack.
To figure out the note count for a song, look at Figure 4-11, where we’ve counted the notes on a keyboard for “Heart and Soul.” In Scratch, you can choose a note and its duration with the “Play note 60 for 0.5 beats” block and adjusting the note sound by the dropdown arrow and adjusting the length of the beat. This is actually very similar to creating musical notes for piezos with the Arduino programming language. In the Arduino IDE, you would define the notes and note duration and then have the song play based on counting notes (also similar to the last project!).
Figure 4-11 Keyboard template with “Heart and Soul” note count.
For “Heart and Soul,” we drew a small keyboard similar to Figure 4-11, and then wrote down a number for each song note. You can create your own song with this same project by using the keyboard in Figure 4-11 and renumbering the keys for the song you want to play.
Create a variable called “next note” in the “Data” palette, and add “Set next note to 0” to a “When flag clicked” block. In the “Sounds” palette, find a “Set instrument to ” block, and pick your favorite instrument sound (see Figure 4-12).
Figure 4-12 Set “next note” and instrument sound.
Now that you have your song notes by number, it’s time to double up your “or” operators and tell Scratch which note to play following your count. Every time the space key is pressed, you want the variable to change by 1. To do this, simply add a “Change next note to 1” block under the event block “When space key pressed.”
To play “Heart and Soul,” you need to program Scratch to play middle C when the space key is pressed only if the count is on 1, 2, 3, 4, 8, 15, 20, or 26. You can use multiple “or” operators to put all these numbers in one “if/then” statement or break them up into separate “if/then” statements, as shown in Figure 4-13. Follow the template (Figure 4-11) and Figure 4-13 to have Scratch count notes and play the right tones based on number. Also make sure to adjust the note duration in the “beats” section of the “Play note” block accordingly. We calibrated this program to produce each note with a short length to mimic the sound of a kalimba.
Figure 4-13 Scratch program for playing by number.
The last block you need for this program is to reset the counter. Drag one more “if/then” statement and a data block to “Set next note to 1” once the count equals 26. (If you write your own song, make sure to adjust the number here to match the number of notes you want your song to play.)
Find a box that will fit comfortably in your hands. Dig up some backyard onions, or cut a few long skinny leaves off of a houseplant. Place your leaves as in Figure 4-14, and place a piece of aluminum foil over the top. Use a thumbtack to hold each leaf to the box, but also ensure that all leaves will join the capacitance of the aluminum foil strip. Clip an alligator clip to a thumbtack inside the box, and get ready to play!
Figure 4-14 Kalimba go!
What other items would make interesting one-button pianos? How could you combine this with other projects?