This project will show you how to build a 4-note musical instrument where you use tilt, rather than buttons or keys, to control the notes you play. A different LED will light up when each note plays. You can build this project on the Propeller Activity Board's prototyping area or Propeller FLiP Try-it Kit's breadboard. If you are using the FLiP Kit, add extra features to your project on the open breadboard space.
The sensor, LEDs, and piezo speaker use very little current. It is fine to power this project via the boards' USB cable only.
Let's build the BlocklyProp code for programming your Tilt Tones musical instrument, and take a look at how it works.
The 4-Directional Tilt Sensor uses two I/O pins; we connected to P14 and P15 in our project. The diagram below shows what each I/O pin will detect when the sensor is tilted in each direction, either a one or a zero. There are four output states for the sensor: binary 11 = 3, binary 10 = 2, binary 01= 1, and binary 00 = 0.
The code begins with a set multiple pins block. It is configured to set the directions for I/O pins P14 and P15 to IN (input), so the Propeller can receive signals from the Tilt Sensor.
The next task is to initialize and fill two arrays, each with four elements. Why four elements? One for every output state that the Tilt Sensor can provide. The LED array holds the I/O pin numbers for the LED circuits built on the breadboard. The Note array holds four different frequencies, which will be used to play notes on the piezo speaker.
Then, two set variable blocks declare and initialize variables. First, you will set the beat variable to 500, and it will be used later when playing tones. The index variable gets an initial value of zero, and it will be used to access array elements.
The action happens inside a repeat forever loop. The binary get pins block reads the input states of a contiguous group of I/O pins, and the state of each I/O pin becomes a bit in a binary number. The highest I/O pin number provides the most significant bit. Our block uses P15 and P14, so the possible values are binary 11 = 3, binary 10 = 2, binary 01= 1, and binary 00 = 0. In this way, the output state of the Tilt Sensor is stored as a binary number in the index variable.
Now, the freshly-set index variable will be used to select the element at the value of that position from the LED and Notes arrays in the next three blocks. For example, if you tilt the board towards you, P14 and P15 both detect 0, making the value of index 00, which means it grabs element 0 from the array.
To turn on an LED, the index variable is put inside a get array [LED] element block, which is inside a programmable make PIN block. Using the above example, the 0th element in the LED array is 4, so the LED on P4 lights up when this block sets it high.
To play a note, the index variable is used inside the get array [LED] element block, which is inside a frequency out block. To continue with the example, the 0th element of the note array is 1175. That frequency plays for 500 ms - the value of the beat variable used in the duration (ms) field.
After the note plays, the loop finishes with a second make PIN block. Like the first one, it uses the indexed array [LED] element, setting the I/O pin low to turn off the LED off before the loop repeats.
The example code is easy to modify to make your own custom mini-tunes.
In the example code, recall that a set variable block initialized beat to 500. Later, beat is inserted in to the the frequency out block's duration field.
This makes each 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 makes another trip through the repeat forever loop. If you want to slow down the tempo, 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.
If you want to play different notes to make your own 4-note tunes, simply replace the four elements in the Note array with new frequencies.
To make it easier to see which tilt motion will correspond to which frequency (and which LED will light up) you can insert this debugging code into your program, just below the index = get the states...blocks.
Here's what the Terminal would show if you tilt your Board downward 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 Tunes 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
If you are using the Propeller FLiP Try-it Kit, you can add additional parts to this project, using the extra breadboard space and free Propeller I/O pins.
A 7-segment LED display (left) visually indicates the direction of tilt by lighting up the segments that correspond to the lowest edge of the breadboard. See the Seven-Segment Display [5] tutorial to learn more about this device works.
A potentiometer (right) will let you adjust the tempo all you want during run-time just by twisting the potentiometer's knob. No need to adjust the set variable beat block and reprogram your FLiP module just to change the tempo! You can learn more about the potentiometer and this circuit from the Potentiometer Position [6] page in the Circuit Practice with BlocklyProp [7] tutorial.
Updating the BlocklyProp project to make use of the 7-segment LED and potentiometer circuit requires several tasks. Let's tackle them one at a time.
Let's add array code for the 7-segment LED, below the Note array blocks.
Now, let's add code for the potentiometer circuit connected to P0. Whenever this function is called, it will take an RC discharge measurement time that indicates the position of the potentiometer's knob and stores it in a variable named pot. In turn, pot is added to 250 to set the variable beat. 250 becomes the minimum tempo, in times when pot measurement yields a zero.
Now, the only items left are a few changes to the repeat forever loop that will use the new Edge array and potentiometer function.
Okay, now you are ready to test drive the additions!
Links
[1] https://www.parallax.com/product/32912
[2] https://www.parallax.com/product/32023
[3] https://www.parallax.com/product/900-00001
[4] http://www.parallax.com/product/28036
[5] http://learn.parallax.com/tutorials/language/blocklyprop/circuit-practice-blocklyprop/seven-segment-display
[6] http://learn.parallax.com/tutorials/language/blocklyprop/circuit-practice-blocklyprop/potentimeter-position
[7] http://learn.parallax.com/tutorials/language/blocklyprop/circuit-practice-blocklyprop