Step 8Play Musical Notes
Since the buzz() function in the example sketch allows a pitch frequency to be specified, it's time to look up what frequencies make musical notes and test out a musical scale.
Searching Google for [musical note frequencies] revealed this great chart containing the frequencies of a large musical range. That helped answer the question, "How do I make the buzzer make musical tones?".
One sketch to play an octave of notes could look like this:
----------------copy after this line----------------
// Buzzer example function for the CEM-1203 buzzer (Sparkfun's part #COM-07950).
// by Rob Faludi
// http://www.faludi.com
// Additions by Christopher Stevens
// http://www.christopherstevens.cc
void setup() {
pinMode(4, OUTPUT); // set a pin for buzzer output
}
void loop() {
buzz(4, 996, 1000); //pin, note frequency, play length in miliseconds
buzz(4, 1050, 1000);
buzz(4, 1110, 1000);
buzz(4, 1180, 1000);
buzz(4, 1250, 1000);
buzz(4, 1320, 1000);
buzz(4, 1400, 1000);
buzz(4, 1490, 1000);
buzz(4, 1580, 1000);
buzz(4, 1670, 1000);
buzz(4, 1770, 1000);
buzz(4, 1870, 1000);
delay(1000); // wait a bit between buzzes
}
void buzz(int targetPin, long frequency, long length) {
long delayValue = 1000000/frequency/2; // calculate the delay value between transitions
//// 1 second's worth of microseconds, divided by the frequency, then split in half since
//// there are two phases to each cycle
long numCycles = frequency * length/ 1000; // calculate the number of cycles for proper timing
//// multiply frequency, which is really cycles per second, by the number of seconds to
//// get the total number of cycles to produce
for (long i=0; i < numCycles; i++){ // for the calculated length of time...
digitalWrite(targetPin,HIGH); // write the buzzer pin high to push out the diaphram
delayMicroseconds(delayValue); // wait for the calculated delay value
digitalWrite(targetPin,LOW); // write the buzzer pin low to pull back the diaphram
delayMicroseconds(delayValue); // wait againf or the calculated delay value
}
}
----------------copy before this line----------------
That would work, but looking up the frequencies on a chart every time a note is created would be too cumbersome for some. The sketch below adds an array of frequencies for reference, and a new playNote() function that plays a referenced frequency in the array by number, and calls the buzz function with that frequency.
One note about arrays, is that they start with 0, not 1. So the first item in noteFreqArr below would be referenced as noteFreqArr[0], the second would be noteFreqArr[1], and so on.
----------------copy after this line----------------
// Buzzer example function for the CEM-1203 buzzer (Sparkfun's part #COM-07950).
// by Rob Faludi
// http://www.faludi.com
//referenced from http://www.phy.mtu.edu/~suits/notefreqs.html
//starting with F noteFreqArr[0]
int noteFreqArr[] = {
49.4, 52.3, 55.4, 58.7, 62.2, 65.9, 69.9, 74, 78.4, 83.1, 88, 93.2,
98.8, 105, 111, 117, 124, 132, 140, 148, 157, 166, 176, 186,
198, 209, 222, 235, 249, 264, 279, 296, 314, 332, 352, 373,
395, 419, 444, 470, 498, 527, 559, 592, 627, 665, 704, 746,
790, 837, 887, 940, 996, 1050, 1110, 1180, 1250, 1320, 1400, 1490,
1580, 1670, 1770, 1870, 1990, 2100};
void setup() {
pinMode(4, OUTPUT); // set a pin for buzzer output
}
void playNote(int noteInt, long length, long breath = 0) {
length = length - breath;
buzz(4, noteFreqArr[noteInt], length);
if(breath > 0) { //take a short pause or 'breath' if specified
delay(breath);
}
}
void loop() {
playNote(52, 1000);
playNote(53, 1000);
playNote(54, 1000);
playNote(55, 1000, 100);
playNote(56, 1000);
playNote(57, 1000);
playNote(58, 1000);
playNote(59, 1000, 100);
playNote(60, 1000);
playNote(61, 1000);
playNote(62, 1000);
playNote(63, 1000, 100);
delay(1000); // wait a bit between buzzes
}
void buzz(int targetPin, long frequency, long length) {
long delayValue = 1000000/frequency/2; // calculate the delay value between transitions
//// 1 second's worth of microseconds, divided by the frequency, then split in half since
//// there are two phases to each cycle
long numCycles = frequency * length/ 1000; // calculate the number of cycles for proper timing
//// multiply frequency, which is really cycles per second, by the number of seconds to
//// get the total number of cycles to produce
for (long i=0; i < numCycles; i++){ // for the calculated length of time...
digitalWrite(targetPin,HIGH); // write the buzzer pin high to push out the diaphram
delayMicroseconds(delayValue); // wait for the calculated delay value
digitalWrite(targetPin,LOW); // write the buzzer pin low to pull back the diaphram
delayMicroseconds(delayValue); // wait againf or the calculated delay value
}
}
----------------copy before this line----------------
Cool! Now to make some music in the next step...
| « Previous Step | Download PDFView All Steps | Next Step » |
![]() |
Add Comment
|














































