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

Sketch of Components
This is a sketch that shows how to connect each LED and the buzzer to the board when connecting to the hat with conductive thread.
Sketch of Components

Comments
Only logged in users can leave comments