Created
July 10, 2012 21:03
-
-
Save vcwu/3086215 to your computer and use it in GitHub Desktop.
Playing Music
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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 | |
| //Taken from http://www.instructables.com/id/Play-the-French-Can-Can-Using-an-Arduino-and-Buzze/step8/Play-Musical-Notes/ | |
| //Modified by Vicky Wu July 2012 | |
| //It may be helpful to create an array of notes... | |
| //http://www.phy.mtu.edu/~suits/notefreqs.html | |
| #define C5 523 | |
| #define D5 587 | |
| #define E5 659 | |
| #define F5 698 | |
| #define G5 783 | |
| #define a5 880 | |
| #define B5 987 | |
| #define C6 1046 | |
| int out = 4; | |
| void setup() | |
| { | |
| pinMode(4, OUTPUT); | |
| } | |
| 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 | |
| } | |
| } | |
| void loop() | |
| { | |
| //buzz(out, C5, 200); | |
| /* | |
| buzz(out, E5, 200); | |
| buzz(out, G5, 200); | |
| buzz(out, B5, 200); | |
| buzz(out, C6, 200); | |
| buzz(out, B5, 200); | |
| buzz(out, G5, 200); | |
| buzz(out, E5, 200); | |
| */ | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment