Project 4.03 Piezo C Major
The piezoTone function allows us to do a lot with the piezo device without a lot of extra coding. In this project, we teach the Arno to play the C-major scale and set up some of the coding needed to play a song in the next project.
Concepts: arrays, functions
Circuits:
Concepts: arrays, functions
Circuits:
We start by creating several arrays. The notes array holds the names of the 12 tones in the chromatic scale. The lower-case letters indicate flats:
char notes[] = {'C','d','D','e','E','F','g','G','a','A','b','B'};
The tones array holds the frequencies of each note:
float freqs[] = {16.352,17.324,18.354,19.445,20.602,
21.827,23.125,24.500,25.957,27.500,29.135,30.868};
We actually don’t need the full chromatic scale for this project since we’re only going to play the C-major scale. However, we want to create durable, reusable code. It wasn’t much more work to include the full chromatic scale and we can use it later. You may have also noticed that we don’t use the notes array anywhere in this sketch. But stay tuned. We’ll use it in a more complex sketch in the next project.
The majorC array holds the positions of the C-major notes within the freqs and notes arrays:
int majorC[] = {0,2,4,5,7,9,11};
The chromatic scale repeats itself in different octaves. We don’t need to write out the frequency of every octave since there’s a mathematical relationship between the octaves. You can learn more about octaves here:
http://en.wikipedia.org/wiki/Scientific_pitch_notation
We use the variable octave to hold the octave value. It’s initially set to zero:
int octave = 0;
The loop() block is fairly simple. We start a for loop to run through the 7 notes of the C-Major scale:
for(int i=0;i<7;i++){
We retrieve the position of note i in the C-major scale:
int k = majorC[i];
Next we need to calculate the frequency of the note. The piezoTone function expects a long value instead of a float. This means we lose some precision (the decimal part of the frequency). We control the octave using the built-in pow function. The pow function raises its first argument to the power of its second argument (e.g. pow(2,3) = 23 = 8). The value is cast into a long by including (long) in the statement:
note = (long) freqs[k]*pow(2,octave);
Now we call the piezoTone function for the note, delay for 50 milliseconds between notes, and then move on to the next note:
piezoTone(note,100);
delay(50);
} //end loop through notes
After going through the seven notes, we move up an octave (remember that octave++ is the same as writing octave = octave + 1). After playing octave 8, we go back to 0:
octave++;
if(octave>8) octave = 0;
char notes[] = {'C','d','D','e','E','F','g','G','a','A','b','B'};
The tones array holds the frequencies of each note:
float freqs[] = {16.352,17.324,18.354,19.445,20.602,
21.827,23.125,24.500,25.957,27.500,29.135,30.868};
We actually don’t need the full chromatic scale for this project since we’re only going to play the C-major scale. However, we want to create durable, reusable code. It wasn’t much more work to include the full chromatic scale and we can use it later. You may have also noticed that we don’t use the notes array anywhere in this sketch. But stay tuned. We’ll use it in a more complex sketch in the next project.
The majorC array holds the positions of the C-major notes within the freqs and notes arrays:
int majorC[] = {0,2,4,5,7,9,11};
The chromatic scale repeats itself in different octaves. We don’t need to write out the frequency of every octave since there’s a mathematical relationship between the octaves. You can learn more about octaves here:
http://en.wikipedia.org/wiki/Scientific_pitch_notation
We use the variable octave to hold the octave value. It’s initially set to zero:
int octave = 0;
The loop() block is fairly simple. We start a for loop to run through the 7 notes of the C-Major scale:
for(int i=0;i<7;i++){
We retrieve the position of note i in the C-major scale:
int k = majorC[i];
Next we need to calculate the frequency of the note. The piezoTone function expects a long value instead of a float. This means we lose some precision (the decimal part of the frequency). We control the octave using the built-in pow function. The pow function raises its first argument to the power of its second argument (e.g. pow(2,3) = 23 = 8). The value is cast into a long by including (long) in the statement:
note = (long) freqs[k]*pow(2,octave);
Now we call the piezoTone function for the note, delay for 50 milliseconds between notes, and then move on to the next note:
piezoTone(note,100);
delay(50);
} //end loop through notes
After going through the seven notes, we move up an octave (remember that octave++ is the same as writing octave = octave + 1). After playing octave 8, we go back to 0:
octave++;
if(octave>8) octave = 0;