Level: Beginner
Skills Required: PBASIC programming, breadboard circuit-building
Hours to Complete: 1
This project will show you how to build a 4-note musical ‘keyboard’ where you use tilt, rather than actual buttons or keys, to control the notes you play. We also added LEDs that light up in different colors depending on which note is playing, because no fun project is complete without LED lights! This activity is suitable to be built on a BASIC Stamp HomeWork Board or Board of Education, but it is also available for the Propeller Activity Board (accessible on its own project page).
If you know how to program in PBASIC but are unfamiliar with Propeller C, this could be a great time to introduce yourself to multicore programming! The Propeller Activity Board version [1] of the Tilt Tones project uses the simple concepts taught in the Propeller C Tutorials, which are available right here on the Parallax Learn website.
If you are new to the BASIC Stamp microcontroller or to programming, it would be a good idea to review the following before beginning:
Figure 1 shows the schematics for connecting the 4-Directional Tilt Sensor, piezo speaker and 4 LED lights.
Figure 1 – Tilt Tones Schematics (BASIC Stamp HomeWork Board)
220 ohm resistors are built into the HomeWork Board.
If you are using a HomeWork board, you do not need to add the 220 ohm resistors shown in the schematic above to your breadboard, as shown in Figure 2. The little tiny chips next to the I/O pin socket labels are surface-mount 220-ohm resistors. However, if you are using the BASIC Stamp Board of Education, you will need to use a 220 ohm resistor on the breadboard for each LED.
Figure 2 – Tilt Tones Wiring Diagram (BASIC Stamp HomeWork Board)
When you have finished setting up your board, it should look something like the wiring diagram in Figure 2. If your LED leads are too long, they can be cut, but be sure to make note of which side is the cathode and which is the anode. If you look at the top of the LED, the cathode is the side where the edge is either flattened or has a small notch in it.
' BASIC Stamp Tilt Tones.bs2 ' ' {$STAMP BS2} ' {$PBASIC 2.5} led VAR Nib ' I/O pin number of LED to turn on/off note VAR Word ' note to play, in Hz index VAR Nib ' holds state of tilt sensor's output pins beat CON 500 ' how long to play each note, in ms index = 0 ' initialize index to 0 DO index.BIT0 = IN14 ' read P14 tilt sensor state, store in bit 0 of index index.BIT1 = IN15 ' read P15 tilt sensor state, store in bit 1 of index LOOKUP index,[4, 6, 8, 10], led ' index selects LED to light LOOKUP index,[2093, 2349, 2637, 2794], note ' index selects a note (frequency in Hz) to play HIGH led ' turn LED on FREQOUT 1, beat, note ' play note on P1 piezo for beat ms LOW led ' turn LED off LOOP
If nothing lights up when you try the circuit, double check that you have the LEDs’ cathode leads connected properly to GND and not to the I/O pins.
The 4-Directional Tilt Sensor uses two I/O pins; we have them connected to P14 and P15 in our example. The diagram below shows what each I/O pin will detect when the sensor is tilted in each direction.
Let’s take a peek inside the program. First, the variables led, note, and index are declared, along with a constant named beat that is initialized to 500. Also, index is initialized to zero.
led VAR Nib note VAR Word index VAR Nib beat CON 500 index = 0
Now for the action. The code inside the DO…LOOP checks the sensor output, and uses that information to select a note to play and an LED to light up. Let’s look at each line to see exactly how that happens.
DO index.BIT0 = IN14 index.BIT1 = IN15
The line index.BIT0 = IN14 translates to “check the input state of I/O pin P14 to see if the sensor is sending a 0 or a 1, and store it in the lowest bit (bit 0) of the variable named index.” Similarly, the next line stores the input state of P15 in bit 1 of index.
Recall that index was declared as a NIB, a 4-bit variable, initialized to zero (binary 0000). So, depending on which way the sensor is tilted, these two lines of code will set the index variable to binary 0000, 0001, 0010, or 0011. That's the same as decimal 0, 1, 2, and 3.
Next, the freshly-set index variable is used in two LOOKUP statements.
LOOKUP index,[4, 6, 8, 10], led LOOKUP index,[2093, 2349, 2637, 2794], tone
The first line translates to “find the 'index-th' element in the list and assign that value to the led variable.” For example, if you tilt the board towards you, P14 and P15 both detect a 0, making the value of index 0000. The 0th element in the list is 4 (counting 0,1,2,3) so that’s the value that gets assigned to the led variable.
In the next line, index is used again to select the corresponding frequency to assign to the note variable. Since index is still 0000, the 0th value 2093 is assigned to note. Continuing with this example,
HIGH led FREQOUT 1, beat, note LOW led
...HIGH led turns on the LED connected to P4. Then, the piezo speaker on P1 plays the 2093 Hz note. The note lasts for 500 ms, since that’s how the constant beat was initialized at the beginning of the program. After that, LOW led turns the P4 led back off, then loop repeats.
It's easy to customize your Tilt Tones musical instrument by modifying the example program.
In the example program, beat CON 500 initializes the constant beat to 500. Later, beat is used as the FREQOUT command’s duration argument:
FREQOUT 1, beat, note
This makes the note last for 500 ms, giving you a little time to tilt the board to a new position to select a new note before the code repeats the DO…LOOP. If you want to slow down the beat, perhaps to practice the movements to play a tune, initialize beat to 750 or 1000. For peppier tunes — and a coordination challenge — try reducing beat to 250.
It’s easy to modify the program to play different notes to make your own 4-note tunes. Simply replace the four elements in the LOOKUP, index[...], note list.
To make it easier to see which tilt motion will correspond to which frequency (and which LED will light up) insert this debugging code into your program, just below the LOOKUP statements:
DEBUG "index (binary) = ", BIN4 index, CR DEBUG "index (decimal) = ", DEC index, CR DEBUG "led = ", DEC led, CR DEBUG "note = ", DEC note, CR
Then, add this line just above LOOP:
DEBUG CLS
Here's what the Debug Terminal would look like if you are tilting the board down to the right.
The familiar song Mary Had a Little Lamb uses only four notes: A, B, D, and G. It also has a simple, steady beat which works well for our Tilt Tones toy.
B A G A B B B A A A B D D Ma-ry had a lit-tle lamb, lit-tle lamb, lit-tle lamb, B A G A B B B B A A B A G Ma-ry had a lit-tle lamb, its fleece was white as snow.
The Piezo is Quieter on the HomeWork Board compared to the Board of Education because of the built-in resistor. Frequencies between 3000 and 5000 Hz tend to play louder than those above or below that range.
Links
[1] https://learn.parallax.com/project/propeller-tilt-tones
[2] https://learn.parallax.com/sites/default/files/Files/Docs/Projects/TiltTonesBasic/BASIC-Stamp-Tilt-Tones.zip