2 Arduino Mario Bros Tunes
3 With Piezo Buzzer and PWM
10 //Mario main theme melody
12 NOTE_E7, NOTE_E7, 0, NOTE_E7,
13 0, NOTE_C7, NOTE_E7, 0,
17 NOTE_C7, 0, 0, NOTE_G6,
19 0, NOTE_A6, 0, NOTE_B6,
20 0, NOTE_AS6, NOTE_A6, 0,
22 NOTE_G6, NOTE_E7, NOTE_G7,
23 NOTE_A7, 0, NOTE_F7, NOTE_G7,
24 0, NOTE_E7, 0,NOTE_C7,
25 NOTE_D7, NOTE_B6, 0, 0,
27 NOTE_C7, 0, 0, NOTE_G6,
29 0, NOTE_A6, 0, NOTE_B6,
30 0, NOTE_AS6, NOTE_A6, 0,
32 NOTE_G6, NOTE_E7, NOTE_G7,
33 NOTE_A7, 0, NOTE_F7, NOTE_G7,
34 0, NOTE_E7, 0,NOTE_C7,
35 NOTE_D7, NOTE_B6, 0, 0
37 //Mario main them tempo
68 int underworld_melody[] = {
69 NOTE_C4, NOTE_C5, NOTE_A3, NOTE_A4,
70 NOTE_AS3, NOTE_AS4, 0,
72 NOTE_C4, NOTE_C5, NOTE_A3, NOTE_A4,
73 NOTE_AS3, NOTE_AS4, 0,
75 NOTE_F3, NOTE_F4, NOTE_D3, NOTE_D4,
76 NOTE_DS3, NOTE_DS4, 0,
78 NOTE_F3, NOTE_F4, NOTE_D3, NOTE_D4,
79 NOTE_DS3, NOTE_DS4, 0,
80 0, NOTE_DS4, NOTE_CS4, NOTE_D4,
84 NOTE_C4, NOTE_FS4,NOTE_F4, NOTE_E3, NOTE_AS4, NOTE_A4,
85 NOTE_GS4, NOTE_DS4, NOTE_B3,
86 NOTE_AS3, NOTE_A3, NOTE_GS3,
90 int underworld_tempo[] = {
106 18, 18, 18,18, 18, 18,
114 pinMode(3, OUTPUT);//buzzer
115 pinMode(13, OUTPUT);//led indicator when singing a note
128 // iterate over the notes of the melody:
131 Serial.println(" 'Underworld Theme'");
132 int size = sizeof(underworld_melody) / sizeof(int);
133 for (int thisNote = 0; thisNote < size; thisNote++) {
135 // to calculate the note duration, take one second
136 // divided by the note type.
137 //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
138 int noteDuration = 1000/underworld_tempo[thisNote];
140 buzz(melodyPin, underworld_melody[thisNote],noteDuration);
142 // to distinguish the notes, set a minimum time between them.
143 // the note's duration + 30% seems to work well:
144 int pauseBetweenNotes = noteDuration * 1.30;
145 delay(pauseBetweenNotes);
147 // stop the tone playing:
148 buzz(melodyPin, 0,noteDuration);
154 Serial.println(" 'Mario Theme'");
155 int size = sizeof(melody) / sizeof(int);
156 for (int thisNote = 0; thisNote < size; thisNote++) {
158 // to calculate the note duration, take one second
159 // divided by the note type.
160 //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
161 int noteDuration = 1000/tempo[thisNote];
163 buzz(melodyPin, melody[thisNote],noteDuration);
165 // to distinguish the notes, set a minimum time between them.
166 // the note's duration + 30% seems to work well:
167 int pauseBetweenNotes = noteDuration * 1.30;
168 delay(pauseBetweenNotes);
170 // stop the tone playing:
171 buzz(melodyPin, 0,noteDuration);
177 void buzz(int targetPin, long frequency, long length) {
178 digitalWrite(13,HIGH);
179 long delayValue = 1000000/frequency/2; // calculate the delay value between transitions
180 //// 1 second's worth of microseconds, divided by the frequency, then split in half since
181 //// there are two phases to each cycle
182 long numCycles = frequency * length/ 1000; // calculate the number of cycles for proper timing
183 //// multiply frequency, which is really cycles per second, by the number of seconds to
184 //// get the total number of cycles to produce
185 for (long i=0; i < numCycles; i++){ // for the calculated length of time...
186 digitalWrite(targetPin,HIGH); // write the buzzer pin high to push out the diaphram
187 delayMicroseconds(delayValue); // wait for the calculated delay value
188 digitalWrite(targetPin,LOW); // write the buzzer pin low to pull back the diaphram
189 delayMicroseconds(delayValue); // wait again or the calculated delay value
191 digitalWrite(13,LOW);