Components and supplies
Speaker, Piezo
Tactile Switch, Top Actuated
TLC5940
Resistor 10k ohm
Arduino Nano R3
Resistor 2.21k ohm
Slide Switch
LED (generic)
Project description
Code
Music Player Code
arduino
Slightly modified from the impressive code I found on this Github Repository: https://github.com/robsoncouto/arduino-songs
1#define NOTE_B0 31 2#define NOTE_C1 33 3#define NOTE_CS1 35 4#define NOTE_D1 37 5#define NOTE_DS1 39 6#define NOTE_E1 41 7#define NOTE_F1 44 8#define NOTE_FS1 46 9#define NOTE_G1 49 10#define NOTE_GS1 52 11#define NOTE_A1 55 12#define NOTE_AS1 58 13#define NOTE_B1 62 14#define NOTE_C2 65 15#define NOTE_CS2 69 16#define NOTE_D2 73 17#define NOTE_DS2 78 18#define NOTE_E2 82 19#define NOTE_F2 87 20#define NOTE_FS2 93 21#define NOTE_G2 98 22#define NOTE_GS2 104 23#define NOTE_A2 110 24#define NOTE_AS2 117 25#define NOTE_B2 123 26#define NOTE_C3 131 27#define NOTE_CS3 139 28#define NOTE_D3 147 29#define NOTE_DS3 156 30#define NOTE_E3 165 31#define NOTE_F3 175 32#define NOTE_FS3 185 33#define NOTE_G3 196 34#define NOTE_GS3 208 35#define NOTE_A3 220 36#define NOTE_AS3 233 37#define NOTE_B3 247 38#define NOTE_C4 262 39#define NOTE_CS4 277 40#define NOTE_D4 294 41#define NOTE_DS4 311 42#define NOTE_E4 330 43#define NOTE_F4 349 44#define NOTE_FS4 370 45#define NOTE_G4 392 46#define NOTE_GS4 415 47#define NOTE_A4 440 48#define NOTE_AS4 466 49#define NOTE_B4 494 50#define NOTE_C5 523 51#define NOTE_CS5 554 52#define NOTE_D5 587 53#define NOTE_DS5 622 54#define NOTE_E5 659 55#define NOTE_F5 698 56#define NOTE_FS5 740 57#define NOTE_G5 784 58#define NOTE_GS5 831 59#define NOTE_A5 880 60#define NOTE_AS5 932 61#define NOTE_B5 988 62#define NOTE_C6 1047 63#define NOTE_CS6 1109 64#define NOTE_D6 1175 65#define NOTE_DS6 1245 66#define NOTE_E6 1319 67#define NOTE_F6 1397 68#define NOTE_FS6 1480 69#define NOTE_G6 1568 70#define NOTE_GS6 1661 71#define NOTE_A6 1760 72#define NOTE_AS6 1865 73#define NOTE_B6 1976 74#define NOTE_C7 2093 75#define NOTE_CS7 2217 76#define NOTE_D7 2349 77#define NOTE_DS7 2489 78#define NOTE_E7 2637 79#define NOTE_F7 2794 80#define NOTE_FS7 2960 81#define NOTE_G7 3136 82#define NOTE_GS7 3322 83#define NOTE_A7 3520 84#define NOTE_AS7 3729 85#define NOTE_B7 3951 86#define NOTE_C8 4186 87#define NOTE_CS8 4435 88#define NOTE_D8 4699 89#define NOTE_DS8 4978 90#define REST 0 91 92 93// change this to make the song slower or faster 94int tempo = 80; 95 96// change this to whichever pin you want to use 97int buzzer = 5; 98int key = 9; 99int key2 = digitalRead(key); 100 101// notes of the moledy followed by the duration. 102// a 4 means a quarter note, 8 an eighteenth , 16 sixteenth, so on 103// !!negative numbers are used to represent dotted notes, 104// so -4 means a dotted quarter note, that is, a quarter plus an eighteenth!! 105int melody[] = { 106 107 // The Godfather theme 108 // Score available at https://musescore.com/user/35463/scores/55160 109 110 REST, 4, REST, 8, REST, 8, REST, 8, NOTE_E4, 8, NOTE_A4, 8, NOTE_C5, 8, //1 111 NOTE_B4, 8, NOTE_A4, 8, NOTE_C5, 8, NOTE_A4, 8, NOTE_B4, 8, NOTE_A4, 8, NOTE_F4, 8, NOTE_G4, 8, 112 NOTE_E4, 2, NOTE_E4, 8, NOTE_A4, 8, NOTE_C5, 8, 113 NOTE_B4, 8, NOTE_A4, 8, NOTE_C5, 8, NOTE_A4, 8, NOTE_C5, 8, NOTE_A4, 8, NOTE_E4, 8, NOTE_DS4, 8, 114 115 NOTE_D4, 2, NOTE_D4, 8, NOTE_F4, 8, NOTE_GS4, 8, //5 116 NOTE_B4, 2, NOTE_D4, 8, NOTE_F4, 8, NOTE_GS4, 8, 117 NOTE_A4, 2, NOTE_C4, 8, NOTE_C4, 8, NOTE_G4, 8, 118 NOTE_F4, 8, NOTE_E4, 8, NOTE_G4, 8, NOTE_F4, 8, NOTE_F4, 8, NOTE_E4, 8, NOTE_E4, 8, NOTE_GS4, 8, 119 120 NOTE_A4, 2, REST,8, NOTE_A4, 8, NOTE_A4, 8, NOTE_GS4, 8, //9 121 NOTE_G4, 2, NOTE_B4, 8, NOTE_A4, 8, NOTE_F4, 8, 122 NOTE_E4, 2, NOTE_E4, 8, NOTE_G4, 8, NOTE_E4, 8, 123 NOTE_D4, 2, NOTE_D4, 8, NOTE_D4, 8, NOTE_F4, 8, NOTE_DS4, 8, 124 125 NOTE_E4, 2, REST, 8, NOTE_E4, 8, NOTE_A4, 8, NOTE_C5, 8, //13 126 127 //repeats from 2 128 NOTE_B4, 8, NOTE_A4, 8, NOTE_C5, 8, NOTE_A4, 8, NOTE_B4, 8, NOTE_A4, 8, NOTE_F4, 8, NOTE_G4, 8, //2 129 NOTE_E4, 2, NOTE_E4, 8, NOTE_A4, 8, NOTE_C5, 8, 130 NOTE_B4, 8, NOTE_A4, 8, NOTE_C5, 8, NOTE_A4, 8, NOTE_C5, 8, NOTE_A4, 8, NOTE_E4, 8, NOTE_DS4, 8, 131 132 NOTE_D4, 2, NOTE_D4, 8, NOTE_F4, 8, NOTE_GS4, 8, //5 133 NOTE_B4, 2, NOTE_D4, 8, NOTE_F4, 8, NOTE_GS4, 8, 134 NOTE_A4, 2, NOTE_C4, 8, NOTE_C4, 8, NOTE_G4, 8, 135 NOTE_F4, 8, NOTE_E4, 8, NOTE_G4, 8, NOTE_F4, 8, NOTE_F4, 8, NOTE_E4, 8, NOTE_E4, 8, NOTE_GS4, 8, 136 137 NOTE_A4, 2, REST,8, NOTE_A4, 8, NOTE_A4, 8, NOTE_GS4, 8, //9 138 NOTE_G4, 2, NOTE_B4, 8, NOTE_A4, 8, NOTE_F4, 8, 139 NOTE_E4, 2, NOTE_E4, 8, NOTE_G4, 8, NOTE_E4, 8, 140 NOTE_D4, 2, NOTE_D4, 8, NOTE_D4, 8, NOTE_F4, 8, NOTE_DS4, 8, 141 142 NOTE_E4, 2 //13 143}; 144 145// sizeof gives the number of bytes, each int value is composed of two bytes (16 bits) 146// there are two values per note (pitch and duration), so for each note there are four bytes 147int notes = sizeof(melody) / sizeof(melody[0]) / 2; 148 149// this calculates the duration of a whole note in ms 150int wholenote = (60000 * 4) / tempo; 151 152int divider = 0, noteDuration = 0; 153 154void setup() { 155 pinMode(key, INPUT); 156 // iterate over the notes of the melody. 157 // Remember, the array is twice the number of notes (notes + durations) 158 159} 160 161void loop() { 162 163 int keyPress = digitalRead(key); 164 if (keyPress == HIGH){ 165 for (int thisNote = 0; thisNote < notes * 2; thisNote = thisNote + 2) { 166 167 // calculates the duration of each note 168 divider = melody[thisNote + 1]; 169 if (divider > 0) { 170 // regular note, just proceed 171 noteDuration = (wholenote) / divider; 172 } else if (divider < 0) { 173 // dotted notes are represented with negative durations!! 174 noteDuration = (wholenote) / abs(divider); 175 noteDuration *= 1.5; // increases the duration in half for dotted notes 176 } 177 178 // we only play the note for 90% of the duration, leaving 10% as a pause 179 tone(buzzer, melody[thisNote], noteDuration * 0.9); 180 181 // Wait for the specief duration before playing the next note. 182 delay(noteDuration); 183 int key2 = digitalRead(key); 184 if (key2 == HIGH) 185 { 186 delay(1000); 187 break; 188 } 189 // stop the waveform generation before the next note. 190 noTone(buzzer); 191 192 } 193 } 194}
Music Player Code
arduino
Slightly modified from the impressive code I found on this Github Repository: https://github.com/robsoncouto/arduino-songs
1#define NOTE_B0 31 2#define NOTE_C1 33 3#define NOTE_CS1 35 4#define 5 NOTE_D1 37 6#define NOTE_DS1 39 7#define NOTE_E1 41 8#define NOTE_F1 44 9#define 10 NOTE_FS1 46 11#define NOTE_G1 49 12#define NOTE_GS1 52 13#define NOTE_A1 55 14#define 15 NOTE_AS1 58 16#define NOTE_B1 62 17#define NOTE_C2 65 18#define NOTE_CS2 69 19#define 20 NOTE_D2 73 21#define NOTE_DS2 78 22#define NOTE_E2 82 23#define NOTE_F2 87 24#define 25 NOTE_FS2 93 26#define NOTE_G2 98 27#define NOTE_GS2 104 28#define NOTE_A2 110 29#define 30 NOTE_AS2 117 31#define NOTE_B2 123 32#define NOTE_C3 131 33#define NOTE_CS3 34 139 35#define NOTE_D3 147 36#define NOTE_DS3 156 37#define NOTE_E3 165 38#define 39 NOTE_F3 175 40#define NOTE_FS3 185 41#define NOTE_G3 196 42#define NOTE_GS3 43 208 44#define NOTE_A3 220 45#define NOTE_AS3 233 46#define NOTE_B3 247 47#define 48 NOTE_C4 262 49#define NOTE_CS4 277 50#define NOTE_D4 294 51#define NOTE_DS4 52 311 53#define NOTE_E4 330 54#define NOTE_F4 349 55#define NOTE_FS4 370 56#define 57 NOTE_G4 392 58#define NOTE_GS4 415 59#define NOTE_A4 440 60#define NOTE_AS4 61 466 62#define NOTE_B4 494 63#define NOTE_C5 523 64#define NOTE_CS5 554 65#define 66 NOTE_D5 587 67#define NOTE_DS5 622 68#define NOTE_E5 659 69#define NOTE_F5 70 698 71#define NOTE_FS5 740 72#define NOTE_G5 784 73#define NOTE_GS5 831 74#define 75 NOTE_A5 880 76#define NOTE_AS5 932 77#define NOTE_B5 988 78#define NOTE_C6 79 1047 80#define NOTE_CS6 1109 81#define NOTE_D6 1175 82#define NOTE_DS6 1245 83#define 84 NOTE_E6 1319 85#define NOTE_F6 1397 86#define NOTE_FS6 1480 87#define NOTE_G6 88 1568 89#define NOTE_GS6 1661 90#define NOTE_A6 1760 91#define NOTE_AS6 1865 92#define 93 NOTE_B6 1976 94#define NOTE_C7 2093 95#define NOTE_CS7 2217 96#define NOTE_D7 97 2349 98#define NOTE_DS7 2489 99#define NOTE_E7 2637 100#define NOTE_F7 2794 101#define 102 NOTE_FS7 2960 103#define NOTE_G7 3136 104#define NOTE_GS7 3322 105#define NOTE_A7 106 3520 107#define NOTE_AS7 3729 108#define NOTE_B7 3951 109#define NOTE_C8 4186 110#define 111 NOTE_CS8 4435 112#define NOTE_D8 4699 113#define NOTE_DS8 4978 114#define REST 115 0 116 117 118// change this to make the song slower or faster 119int tempo 120 = 80; 121 122// change this to whichever pin you want to use 123int buzzer = 5; 124int 125 key = 9; 126int key2 = digitalRead(key); 127 128// notes of the moledy followed 129 by the duration. 130// a 4 means a quarter note, 8 an eighteenth , 16 sixteenth, 131 so on 132// !!negative numbers are used to represent dotted notes, 133// so -4 means 134 a dotted quarter note, that is, a quarter plus an eighteenth!! 135int melody[] = 136 { 137 138 // The Godfather theme 139 // Score available at https://musescore.com/user/35463/scores/55160 140 141 142 REST, 4, REST, 8, REST, 8, REST, 8, NOTE_E4, 8, NOTE_A4, 8, NOTE_C5, 8, //1 143 144 NOTE_B4, 8, NOTE_A4, 8, NOTE_C5, 8, NOTE_A4, 8, NOTE_B4, 8, NOTE_A4, 8, NOTE_F4, 145 8, NOTE_G4, 8, 146 NOTE_E4, 2, NOTE_E4, 8, NOTE_A4, 8, NOTE_C5, 8, 147 NOTE_B4, 148 8, NOTE_A4, 8, NOTE_C5, 8, NOTE_A4, 8, NOTE_C5, 8, NOTE_A4, 8, NOTE_E4, 8, NOTE_DS4, 149 8, 150 151 NOTE_D4, 2, NOTE_D4, 8, NOTE_F4, 8, NOTE_GS4, 8, //5 152 NOTE_B4, 153 2, NOTE_D4, 8, NOTE_F4, 8, NOTE_GS4, 8, 154 NOTE_A4, 2, NOTE_C4, 8, NOTE_C4, 8, 155 NOTE_G4, 8, 156 NOTE_F4, 8, NOTE_E4, 8, NOTE_G4, 8, NOTE_F4, 8, NOTE_F4, 8, NOTE_E4, 157 8, NOTE_E4, 8, NOTE_GS4, 8, 158 159 NOTE_A4, 2, REST,8, NOTE_A4, 8, NOTE_A4, 8, 160 NOTE_GS4, 8, //9 161 NOTE_G4, 2, NOTE_B4, 8, NOTE_A4, 8, NOTE_F4, 8, 162 NOTE_E4, 163 2, NOTE_E4, 8, NOTE_G4, 8, NOTE_E4, 8, 164 NOTE_D4, 2, NOTE_D4, 8, NOTE_D4, 8, 165 NOTE_F4, 8, NOTE_DS4, 8, 166 167 NOTE_E4, 2, REST, 8, NOTE_E4, 8, NOTE_A4, 168 8, NOTE_C5, 8, //13 169 170 //repeats from 2 171 NOTE_B4, 8, NOTE_A4, 8, NOTE_C5, 172 8, NOTE_A4, 8, NOTE_B4, 8, NOTE_A4, 8, NOTE_F4, 8, NOTE_G4, 8, //2 173 NOTE_E4, 174 2, NOTE_E4, 8, NOTE_A4, 8, NOTE_C5, 8, 175 NOTE_B4, 8, NOTE_A4, 8, NOTE_C5, 8, 176 NOTE_A4, 8, NOTE_C5, 8, NOTE_A4, 8, NOTE_E4, 8, NOTE_DS4, 8, 177 178 NOTE_D4, 179 2, NOTE_D4, 8, NOTE_F4, 8, NOTE_GS4, 8, //5 180 NOTE_B4, 2, NOTE_D4, 8, NOTE_F4, 181 8, NOTE_GS4, 8, 182 NOTE_A4, 2, NOTE_C4, 8, NOTE_C4, 8, NOTE_G4, 8, 183 NOTE_F4, 184 8, NOTE_E4, 8, NOTE_G4, 8, NOTE_F4, 8, NOTE_F4, 8, NOTE_E4, 8, NOTE_E4, 8, NOTE_GS4, 185 8, 186 187 NOTE_A4, 2, REST,8, NOTE_A4, 8, NOTE_A4, 8, NOTE_GS4, 8, //9 188 NOTE_G4, 189 2, NOTE_B4, 8, NOTE_A4, 8, NOTE_F4, 8, 190 NOTE_E4, 2, NOTE_E4, 8, NOTE_G4, 8, 191 NOTE_E4, 8, 192 NOTE_D4, 2, NOTE_D4, 8, NOTE_D4, 8, NOTE_F4, 8, NOTE_DS4, 8, 193 194 195 NOTE_E4, 2 //13 196}; 197 198// sizeof gives the number of bytes, each 199 int value is composed of two bytes (16 bits) 200// there are two values per note 201 (pitch and duration), so for each note there are four bytes 202int notes = sizeof(melody) 203 / sizeof(melody[0]) / 2; 204 205// this calculates the duration of a whole note 206 in ms 207int wholenote = (60000 * 4) / tempo; 208 209int divider = 0, noteDuration 210 = 0; 211 212void setup() { 213 pinMode(key, INPUT); 214 // iterate over the notes 215 of the melody. 216 // Remember, the array is twice the number of notes (notes + 217 durations) 218 219} 220 221void loop() { 222 223 int keyPress = digitalRead(key); 224 225 if (keyPress == HIGH){ 226 for (int thisNote = 0; thisNote < notes * 2; thisNote 227 = thisNote + 2) { 228 229 // calculates the duration of each note 230 231 divider = melody[thisNote + 1]; 232 if (divider > 0) { 233 // 234 regular note, just proceed 235 noteDuration = (wholenote) / divider; 236 237 } else if (divider < 0) { 238 // dotted notes are represented with 239 negative durations!! 240 noteDuration = (wholenote) / abs(divider); 241 noteDuration 242 *= 1.5; // increases the duration in half for dotted notes 243 } 244 245 246 // we only play the note for 90% of the duration, leaving 10% as a pause 247 248 tone(buzzer, melody[thisNote], noteDuration * 0.9); 249 250 // Wait for 251 the specief duration before playing the next note. 252 delay(noteDuration); 253 254 int key2 = digitalRead(key); 255 if (key2 == HIGH) 256 { 257 delay(1000); 258 259 break; 260 } 261 // stop the waveform generation before the 262 next note. 263 noTone(buzzer); 264 265 } 266 } 267}
Lights Code
arduino
Uses a library to manage the TLC5940 IC, it randomly increases or decreases the light of different LEDs, to create a nice visual effect.
1 2#include <Tlc5940.h> 3 4void setup() 5{ 6 7 8 Tlc.init(0);//initalizes all LEDs to off at first 9} 10 11void loop() 12{ 13 14 updateLight(random(16), random(4096)); 15 16 17} 18 19void updateAllLights() 20{ 21 for(int i=0; i<16; i++) 22 Tlc.set(i, random(4096)); 23 Tlc.update(); 24} 25 26void updateLight(int pin, int light) 27{ 28 int current = Tlc.get(pin); 29 int diff = light - current; 30 if (diff == 0) return; 31 32 if (diff > 0) 33 { 34 for (int i=current; i<=light; i+=10) 35 { 36 Tlc.set(pin, i); 37 Tlc.update(); 38 delay(1); 39 } 40 } 41 else 42 { 43 for (int i=current; i>=light; i-=10) 44 { 45 Tlc.set(pin, i); 46 Tlc.update(); 47 delay(1); 48 } 49 } 50}
Downloadable files
TLC5940 Schematic
I obtained this part of the schematic (for expanding PWM pins) from here: tinyurl.com/t2y4vzy
TLC5940 Schematic
TLC5940 Schematic
I obtained this part of the schematic (for expanding PWM pins) from here: tinyurl.com/t2y4vzy
TLC5940 Schematic
Comments
Only logged in users can leave comments
behrooz66
0 Followers
•0 Projects
Table of contents
Intro
7
0