Components and supplies
1
BC547 transistor
1
Jumper wires (generic)
1
Speaker: 0.25W, 8 ohms
1
Arduino UNO
Tools and machines
1
Soldering iron (generic)
Apps and platforms
1
Arduino IDE
Project description
Code
Untitled file
c_cpp
1 2#define NOTE_B0 31 3#define NOTE_C1 33 4#define NOTE_CS1 35 5#define NOTE_D1 37 6#define NOTE_DS1 39 7#define NOTE_E1 41 8#define NOTE_F1 44 9#define NOTE_FS1 46 10#define NOTE_G1 49 11#define NOTE_GS1 52 12#define NOTE_A1 55 13#define NOTE_AS1 58 14#define NOTE_B1 62 15#define NOTE_C2 65 16#define NOTE_CS2 69 17#define NOTE_D2 73 18#define NOTE_DS2 78 19#define NOTE_E2 82 20#define NOTE_F2 87 21#define NOTE_FS2 93 22#define NOTE_G2 98 23#define NOTE_GS2 104 24#define NOTE_A2 110 25#define NOTE_AS2 117 26#define NOTE_B2 123 27#define NOTE_C3 131 28#define NOTE_CS3 139 29#define NOTE_D3 147 30#define NOTE_DS3 156 31#define NOTE_E3 165 32#define NOTE_F3 175 33#define NOTE_FS3 185 34#define NOTE_G3 196 35#define NOTE_GS3 208 36#define NOTE_A3 220 37#define NOTE_AS3 233 38#define NOTE_B3 247 39#define NOTE_C4 262 40#define NOTE_CS4 277 41#define NOTE_D4 294 42#define NOTE_DS4 311 43#define NOTE_E4 330 44#define NOTE_F4 349 45#define NOTE_FS4 370 46#define NOTE_G4 392 47#define NOTE_GS4 415 48#define NOTE_A4 440 49#define NOTE_AS4 466 50#define NOTE_B4 494 51#define NOTE_C5 523 52#define NOTE_CS5 554 53#define NOTE_D5 587 54#define NOTE_DS5 622 55#define NOTE_E5 659 56#define NOTE_F5 698 57#define NOTE_FS5 740 58#define NOTE_G5 784 59#define NOTE_GS5 831 60#define NOTE_A5 880 61#define NOTE_AS5 932 62#define NOTE_B5 988 63#define NOTE_C6 1047 64#define NOTE_CS6 1109 65#define NOTE_D6 1175 66#define NOTE_DS6 1245 67#define NOTE_E6 1319 68#define NOTE_F6 1397 69#define NOTE_FS6 1480 70#define NOTE_G6 1568 71#define NOTE_GS6 1661 72#define NOTE_A6 1760 73#define NOTE_AS6 1865 74#define NOTE_B6 1976 75#define NOTE_C7 2093 76#define NOTE_CS7 2217 77#define NOTE_D7 2349 78#define NOTE_DS7 2489 79#define NOTE_E7 2637 80#define NOTE_F7 2794 81#define NOTE_FS7 2960 82#define NOTE_G7 3136 83#define NOTE_GS7 3322 84#define NOTE_A7 3520 85#define NOTE_AS7 3729 86#define NOTE_B7 3951 87#define NOTE_C8 4186 88#define NOTE_CS8 4435 89#define NOTE_D8 4699 90#define NOTE_DS8 4978 91 92#define melodyPin 3 93//Mario main theme melody 94int melody[] = { 95 NOTE_E7, NOTE_E7, 0, NOTE_E7, 96 0, NOTE_C7, NOTE_E7, 0, 97 NOTE_G7, 0, 0, 0, 98 NOTE_G6, 0, 0, 0, 99 100 NOTE_C7, 0, 0, NOTE_G6, 101 0, 0, NOTE_E6, 0, 102 0, NOTE_A6, 0, NOTE_B6, 103 0, NOTE_AS6, NOTE_A6, 0, 104 105 NOTE_G6, NOTE_E7, NOTE_G7, 106 NOTE_A7, 0, NOTE_F7, NOTE_G7, 107 0, NOTE_E7, 0, NOTE_C7, 108 NOTE_D7, NOTE_B6, 0, 0, 109 110 NOTE_C7, 0, 0, NOTE_G6, 111 0, 0, NOTE_E6, 0, 112 0, NOTE_A6, 0, NOTE_B6, 113 0, NOTE_AS6, NOTE_A6, 0, 114 115 NOTE_G6, NOTE_E7, NOTE_G7, 116 NOTE_A7, 0, NOTE_F7, NOTE_G7, 117 0, NOTE_E7, 0, NOTE_C7, 118 NOTE_D7, NOTE_B6, 0, 0 119}; 120//Mario main them tempo 121int tempo[] = { 122 12, 12, 12, 12, 123 12, 12, 12, 12, 124 12, 12, 12, 12, 125 12, 12, 12, 12, 126 127 12, 12, 12, 12, 128 12, 12, 12, 12, 129 12, 12, 12, 12, 130 12, 12, 12, 12, 131 132 9, 9, 9, 133 12, 12, 12, 12, 134 12, 12, 12, 12, 135 12, 12, 12, 12, 136 137 12, 12, 12, 12, 138 12, 12, 12, 12, 139 12, 12, 12, 12, 140 12, 12, 12, 12, 141 142 9, 9, 9, 143 12, 12, 12, 12, 144 12, 12, 12, 12, 145 12, 12, 12, 12, 146}; 147//Underworld melody 148int underworld_melody[] = { 149 NOTE_C4, NOTE_C5, NOTE_A3, NOTE_A4, 150 NOTE_AS3, NOTE_AS4, 0, 151 0, 152 NOTE_C4, NOTE_C5, NOTE_A3, NOTE_A4, 153 NOTE_AS3, NOTE_AS4, 0, 154 0, 155 NOTE_F3, NOTE_F4, NOTE_D3, NOTE_D4, 156 NOTE_DS3, NOTE_DS4, 0, 157 0, 158 NOTE_F3, NOTE_F4, NOTE_D3, NOTE_D4, 159 NOTE_DS3, NOTE_DS4, 0, 160 0, NOTE_DS4, NOTE_CS4, NOTE_D4, 161 NOTE_CS4, NOTE_DS4, 162 NOTE_DS4, NOTE_GS3, 163 NOTE_G3, NOTE_CS4, 164 NOTE_C4, NOTE_FS4, NOTE_F4, NOTE_E3, NOTE_AS4, NOTE_A4, 165 NOTE_GS4, NOTE_DS4, NOTE_B3, 166 NOTE_AS3, NOTE_A3, NOTE_GS3, 167 0, 0, 0 168}; 169//Underwolrd tempo 170int underworld_tempo[] = { 171 12, 12, 12, 12, 172 12, 12, 6, 173 3, 174 12, 12, 12, 12, 175 12, 12, 6, 176 3, 177 12, 12, 12, 12, 178 12, 12, 6, 179 3, 180 12, 12, 12, 12, 181 12, 12, 6, 182 6, 18, 18, 18, 183 6, 6, 184 6, 6, 185 6, 6, 186 18, 18, 18, 18, 18, 18, 187 10, 10, 10, 188 10, 10, 10, 189 3, 3, 3 190}; 191 192void setup(void) 193{ 194 pinMode(3, OUTPUT);//buzzer 195 pinMode(13, OUTPUT);//led indicator when singing a note 196 197} 198void loop() 199{ 200 //sing the tunes 201 sing(1); 202 sing(1); 203 sing(2); 204} 205int song = 0; 206 207void sing(int s) { 208 // iterate over the notes of the melody: 209 song = s; 210 if (song == 2) { 211 Serial.println(" 'Underworld Theme'"); 212 int size = sizeof(underworld_melody) / sizeof(int); 213 for (int thisNote = 0; thisNote < size; thisNote++) { 214 215 // to calculate the note duration, take one second 216 // divided by the note type. 217 //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc. 218 int noteDuration = 1000 / underworld_tempo[thisNote]; 219 220 buzz(melodyPin, underworld_melody[thisNote], noteDuration); 221 222 // to distinguish the notes, set a minimum time between them. 223 // the note's duration + 30% seems to work well: 224 int pauseBetweenNotes = noteDuration * 1.30; 225 delay(pauseBetweenNotes); 226 227 // stop the tone playing: 228 buzz(melodyPin, 0, noteDuration); 229 230 } 231 232 } else { 233 234 Serial.println(" 'Mario Theme'"); 235 int size = sizeof(melody) / sizeof(int); 236 for (int thisNote = 0; thisNote < size; thisNote++) { 237 238 // to calculate the note duration, take one second 239 // divided by the note type. 240 //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc. 241 int noteDuration = 1000 / tempo[thisNote]; 242 243 buzz(melodyPin, melody[thisNote], noteDuration); 244 245 // to distinguish the notes, set a minimum time between them. 246 // the note's duration + 30% seems to work well: 247 int pauseBetweenNotes = noteDuration * 1.30; 248 delay(pauseBetweenNotes); 249 250 // stop the tone playing: 251 buzz(melodyPin, 0, noteDuration); 252 253 } 254 } 255} 256 257void buzz(int targetPin, long frequency, long length) { 258 digitalWrite(13, HIGH); 259 long delayValue = 1000000 / frequency / 2; // calculate the delay value between transitions 260 //// 1 second's worth of microseconds, divided by the frequency, then split in half since 261 //// there are two phases to each cycle 262 long numCycles = frequency * length / 1000; // calculate the number of cycles for proper timing 263 //// multiply frequency, which is really cycles per second, by the number of seconds to 264 //// get the total number of cycles to produce 265 for (long i = 0; i < numCycles; i++) { // for the calculated length of time... 266 digitalWrite(targetPin, HIGH); // write the buzzer pin high to push out the diaphram 267 delayMicroseconds(delayValue); // wait for the calculated delay value 268 digitalWrite(targetPin, LOW); // write the buzzer pin low to pull back the diaphram 269 delayMicroseconds(delayValue); // wait again or the calculated delay value 270 } 271 digitalWrite(13, LOW); 272}
Downloadable files
(1)_hyR3EAKZPd.png
(1)_hyR3EAKZPd.png

(1)_hyR3EAKZPd.png
(1)_hyR3EAKZPd.png

Comments
Only logged in users can leave comments