The example program is easy to modify to make your own custom mini-tunes.
Turn Up the Beat
In the example program, recall that the beat variable was initialized to 500. Later, beat is used in the freqout function's msTime parameter:
freqout(1, beat, note[index])
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 while(1) 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.
- Experiment!
New Notes
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 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 get_states function call:
print("%c", CLS); print("index (binary) = %b\n", index); print("index (decimal) = %d\n", index); print("led = %d\n", led[index]); print("note = %d\n", note[index]);
Here's what the SimpleIDE Terminal would show if you tilt your Activity Board downward to the right.
Challenge Yourself
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
- Referring to the keyboard diagram below, try modifying your program to use these four notes, then see if you can play the song.