I use two buzzers to play <Two Tigers Song>. If you don`t press the button you hear only one part of the music.
When you press the button, another part will come out enrich the melody.
Here is the Arduino code :
// Buzzer example function for the CEM-1203 buzzer (Sparkfun's part #COM-07950).
// by Rob Faludi
// http://www.faludi.com
// Online tutorial here:http://www.instructables.com/id/Play-the-French-Can-Can-Using-an-Arduino-and-Buzze/
// Two Tigers Song V.1.0
// by Lisa Lee
// Nov 5 2010
// I use two buzzers to play this song. If you don`t press the button you hear only one part of the music.
// When you press the button, another part will come out enrich the melody.
float BuzzerPin1 = 2;
float BuzzerPin2 = 4;
float _G = 45.0;
float C = 261.6;
float D = 293.7;
float E = 329.6;
float F = 349.2;
float G = 392.0;
float A = 440.0;
float B = 493.9;
void setup() {
pinMode(BuzzerPin1, OUTPUT); // set a pin for buzzer output
pinMode(BuzzerPin2, OUTPUT); // set a pin for buzzer output
}
void loop() {
buzz(BuzzerPin1, C, 500); // buzz the buzzer on pin 2 at toneC for 500 milliseconds
buzz(BuzzerPin2, E, 100); // buzz the buzzer on pin 4 at toneE for 500 milliseconds
buzz(BuzzerPin1, D, 500);
buzz(BuzzerPin2, F, 100);
buzz(BuzzerPin1, E, 500);
buzz(BuzzerPin2, G, 100);
buzz(BuzzerPin1, C, 500);
buzz(BuzzerPin2, C, 100);
buzz(BuzzerPin1, C, 500);
buzz(BuzzerPin2, E, 100);
buzz(BuzzerPin1, D, 500);
buzz(BuzzerPin2, F, 100);
buzz(BuzzerPin1, E, 500);
buzz(BuzzerPin2, G, 100);
buzz(BuzzerPin1, C, 500);
buzz(BuzzerPin2, C, 100);
buzz(BuzzerPin1, E, 500);
buzz(BuzzerPin2, G, 100);
buzz(BuzzerPin1, F, 500);
buzz(BuzzerPin2, A, 100);
buzz(BuzzerPin1, G, 1000);
buzz(BuzzerPin1, E, 500);
buzz(BuzzerPin2, G, 100);
buzz(BuzzerPin1, F, 500);
buzz(BuzzerPin2, A, 100);
buzz(BuzzerPin1, G, 1000);
buzz(BuzzerPin1, G, 250);
buzz(BuzzerPin2, _G, 10);
buzz(BuzzerPin1, A, 250);
buzz(BuzzerPin2, A, 10);
buzz(BuzzerPin1, G, 250);
buzz(BuzzerPin2, _G, 10);
buzz(BuzzerPin1, D, 250);
buzz(BuzzerPin2, D, 10);
buzz(BuzzerPin1, E, 500);
buzz(BuzzerPin1, C, 500);
buzz(BuzzerPin1, G, 250);
buzz(BuzzerPin2, _G, 100);
buzz(BuzzerPin1, A, 250);
buzz(BuzzerPin2, A, 100);
buzz(BuzzerPin1, G, 250);
buzz(BuzzerPin2, _G, 100);
buzz(BuzzerPin1, D, 250);
buzz(BuzzerPin2, D, 100);
buzz(BuzzerPin1, E, 500);
buzz(BuzzerPin1, C, 500);
buzz(BuzzerPin1, C, 500);
buzz(BuzzerPin2, C, 100);
buzz(BuzzerPin1, _G, 500);
buzz(BuzzerPin2, _G, 100);
buzz(BuzzerPin1, C, 1000);
buzz(BuzzerPin1, C, 500);
buzz(BuzzerPin2, C, 100);
buzz(BuzzerPin1, _G, 500);
buzz(BuzzerPin2, _G, 100);
buzz(BuzzerPin1, C, 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
}
}
Now, bless me, " says such a mind, "I have done my duty, " when , as a matter of fact, it has merely done its old, unbreakable trick once again. I don't like this.
Posted by: Discount Sunglasses Outlet | 04/21/2011 at 05:03 AM