Created
July 30, 2012 05:08
-
-
Save paf31/3204891 to your computer and use it in GitHub Desktop.
Ode to Joy
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
| const int SPEAKER1 = 13; | |
| const int SPEAKER2 = 11; | |
| const int PITCH[8] = { 727, 647, 611, 545, 485, 458, 408, 363 }; | |
| char PITCH1[61] = { | |
| 'E', 'E', 'F', 'G', 'G', 'F', 'E', 'D', 'C', 'C', 'D', 'E', 'E', 'D', 'D', | |
| 'E', 'E', 'F', 'G', 'G', 'F', 'E', 'D', 'C', 'C', 'D', 'E', 'D', 'C', 'C', | |
| 'D', 'E', 'C', 'D', 'E', 'F', 'E', 'C', 'D', 'E', 'F', 'E', 'D', 'C', 'D', 'G', | |
| 'E', 'E', 'F', 'G', 'G', 'F', 'E', 'D', 'C', 'C', 'D', 'E', 'D', 'C', 'C' | |
| }; | |
| char PITCH2[14] = { | |
| 'C', 'G', 'C', 'G', | |
| 'C', 'G', 'C', 'G', | |
| 'G', | |
| 'C', 'G', 'C', 'G', 'C' | |
| }; | |
| int DURATION1[61] = { | |
| 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 2, 8, | |
| 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 2, 8, | |
| 8, 4, 4, 4, 2, 2, 4, 4, 4, 2, 2, 4, 4, 4, 4, 8, | |
| 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 2, 8 | |
| }; | |
| int DURATION2[14] = { | |
| 16, 16, 16, 16, | |
| 16, 16, 16, 16, | |
| 64, | |
| 16, 16, 16, 8, 8 | |
| }; | |
| const long DURATION = 15000; | |
| long i; | |
| int pos1, pos2; | |
| int pitch1, pitch2; | |
| int peakCycles1, peakCycles2; | |
| long noteCycles1, noteCycles2; | |
| int state1, state2; | |
| void setup () | |
| { | |
| pinMode (SPEAKER1, OUTPUT); | |
| pinMode (SPEAKER2, OUTPUT); | |
| state1 = LOW; | |
| state2 = LOW; | |
| pos1 = pos2 = 0; | |
| pitch1 = PITCH[PITCH1[0] - ((int)'A')] / 2; | |
| pitch2 = PITCH[PITCH2[0] - ((int)'A')]; | |
| peakCycles1 = pitch1; | |
| peakCycles2 = pitch2; | |
| noteCycles1 = DURATION * DURATION1[0]; | |
| noteCycles2 = DURATION * DURATION2[0]; | |
| } | |
| void loop() { | |
| peakCycles1--; | |
| if (peakCycles1 == 0) { | |
| state1 = state1 == LOW ? HIGH : LOW; | |
| digitalWrite(SPEAKER1, state1); | |
| peakCycles1 = pitch1; | |
| } | |
| peakCycles2--; | |
| if (peakCycles2 == 0) { | |
| state2 = state2 == LOW ? HIGH : LOW; | |
| digitalWrite(SPEAKER2, state2); | |
| peakCycles2 = pitch2; | |
| } | |
| noteCycles1--; | |
| if (noteCycles1 == 0) { | |
| pos1++; | |
| if (pos1 >= sizeof(PITCH1)) { | |
| pos1 = 0; | |
| } | |
| noteCycles1 = DURATION * DURATION1[pos1]; | |
| pitch1 = PITCH[PITCH1[pos1] - ((int)'A')] / 2; | |
| } | |
| noteCycles2--; | |
| if (noteCycles2 == 0) { | |
| pos2++; | |
| if (pos2 >= sizeof(PITCH2)) { | |
| pos2 = 0; | |
| } | |
| noteCycles2 = DURATION * DURATION2[pos2]; | |
| pitch2 = PITCH[PITCH2[pos2] - ((int)'A')]; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment