Project 4.02 Controlling the Piezo with a Function
In project 4.01, we used the piezo to create a single tone. To really make it useful, we need to create different tones with different durations. In other words, we want the piezo to create music. This is the perfect place to introduce functions, reusable pieces of codes where we change a couple of parameters, but more or less do the same thing over and over again.
Concepts: Functions, delayMicroseconds
Circuits:
Concepts: Functions, delayMicroseconds
Circuits:
This code looks different than everything else we’ve done so far. In addition to the setup() and loop() blocks, there’s a function declaration. Remember, the function is declared after the loop() block, but we can still call the function from either the setup() or loop() blocks. Don’t be confused by the order of the blocks. Functions can be called from anywhere in the code, even from other functions.
Let’s look at the function first. We declare the function with the term void to indicate that the function doesn’t return any information. The Arno doesn’t need to save a place in memory to return a value from the function. We tell the Arno to expect two parameters, freq controls the frequency of the tone and duration controls how long it lasts, in milliseconds:
void piezoTone(long freq, long duration){
Like in project 4.01, we still need to know how many microseconds are in a second and the period of time between vibrations. Since we declare these variables within the function, they’re local variables and aren’t available for use elsewhere in the code. That’s OK, we don’t need them anywhere else and it frees up memory to use them only when we need them:
long aSecond = 1000000;
long period = aSecond/freq;
It’s convenient to send duration to the function in milliseconds, since it saves us from writing out a bunch of zeros, but we need better control of the timing of the vibrations so we convert the value to microseconds:
duration = duration*1000;
The variable period is the time between vibrations. We need to adjust duration so that it accounts for differences in the time between vibrations. This yields the number of vibrations needed to fill the period of time specified by duration:
duration = duration/period;
Now we simply vibrate the piezo element at the frequency specified by freq enough times to cover the period originally specified by duration:
for(long k = 0; k < duration; k++){
digitalWrite(piezo,HIGH);
delayMicroseconds(period/2);
digitalWrite(piezo,LOW);
delayMicroseconds(period/2);
}
Using the function makes playing different notes on the piezo easy. Four calls to the piezoTone function plays the notes of middle C, D, E, and F. We delay for 0.250 seconds before starting over again:
void loop(){
piezoTone(262,250);
delay(250);
piezoTone(294,250);
delay(250);
piezoTone(330,250);
delay(250);
piezoTone(349,250);
delay(250);
}
You can find the frequencies for the full range of notes on a piano here:
http://en.wikipedia.org/wiki/Piano_key_frequencies
Let’s look at the function first. We declare the function with the term void to indicate that the function doesn’t return any information. The Arno doesn’t need to save a place in memory to return a value from the function. We tell the Arno to expect two parameters, freq controls the frequency of the tone and duration controls how long it lasts, in milliseconds:
void piezoTone(long freq, long duration){
Like in project 4.01, we still need to know how many microseconds are in a second and the period of time between vibrations. Since we declare these variables within the function, they’re local variables and aren’t available for use elsewhere in the code. That’s OK, we don’t need them anywhere else and it frees up memory to use them only when we need them:
long aSecond = 1000000;
long period = aSecond/freq;
It’s convenient to send duration to the function in milliseconds, since it saves us from writing out a bunch of zeros, but we need better control of the timing of the vibrations so we convert the value to microseconds:
duration = duration*1000;
The variable period is the time between vibrations. We need to adjust duration so that it accounts for differences in the time between vibrations. This yields the number of vibrations needed to fill the period of time specified by duration:
duration = duration/period;
Now we simply vibrate the piezo element at the frequency specified by freq enough times to cover the period originally specified by duration:
for(long k = 0; k < duration; k++){
digitalWrite(piezo,HIGH);
delayMicroseconds(period/2);
digitalWrite(piezo,LOW);
delayMicroseconds(period/2);
}
Using the function makes playing different notes on the piezo easy. Four calls to the piezoTone function plays the notes of middle C, D, E, and F. We delay for 0.250 seconds before starting over again:
void loop(){
piezoTone(262,250);
delay(250);
piezoTone(294,250);
delay(250);
piezoTone(330,250);
delay(250);
piezoTone(349,250);
delay(250);
}
You can find the frequencies for the full range of notes on a piano here:
http://en.wikipedia.org/wiki/Piano_key_frequencies