| 1 | 
 | 
  
    | 2 | /*************************************************
 | 
  
    | 3 |  * Public Constants
 | 
  
    | 4 |  *************************************************/
 | 
  
    | 5 | 
 | 
  
    | 6 | #define NOTE_B0  31
 | 
  
    | 7 | #define NOTE_C1  33
 | 
  
    | 8 | #define NOTE_CS1 35
 | 
  
    | 9 | #define NOTE_D1  37
 | 
  
    | 10 | #define NOTE_DS1 39
 | 
  
    | 11 | #define NOTE_E1  41
 | 
  
    | 12 | #define NOTE_F1  44
 | 
  
    | 13 | #define NOTE_FS1 46
 | 
  
    | 14 | #define NOTE_G1  49
 | 
  
    | 15 | #define NOTE_GS1 52
 | 
  
    | 16 | #define NOTE_A1  55
 | 
  
    | 17 | #define NOTE_AS1 58
 | 
  
    | 18 | #define NOTE_B1  62
 | 
  
    | 19 | #define NOTE_C2  65
 | 
  
    | 20 | #define NOTE_CS2 69
 | 
  
    | 21 | #define NOTE_D2  73
 | 
  
    | 22 | #define NOTE_DS2 78
 | 
  
    | 23 | #define NOTE_E2  82
 | 
  
    | 24 | #define NOTE_F2  87
 | 
  
    | 25 | #define NOTE_FS2 93
 | 
  
    | 26 | #define NOTE_G2  98
 | 
  
    | 27 | #define NOTE_GS2 104
 | 
  
    | 28 | #define NOTE_A2  110
 | 
  
    | 29 | #define NOTE_AS2 117
 | 
  
    | 30 | #define NOTE_B2  123
 | 
  
    | 31 | #define NOTE_C3  131
 | 
  
    | 32 | #define NOTE_CS3 139
 | 
  
    | 33 | #define NOTE_D3  147
 | 
  
    | 34 | #define NOTE_DS3 156
 | 
  
    | 35 | #define NOTE_E3  165
 | 
  
    | 36 | #define NOTE_F3  175
 | 
  
    | 37 | #define NOTE_FS3 185
 | 
  
    | 38 | #define NOTE_G3  196
 | 
  
    | 39 | #define NOTE_GS3 208
 | 
  
    | 40 | #define NOTE_A3  220
 | 
  
    | 41 | #define NOTE_AS3 233
 | 
  
    | 42 | #define NOTE_B3  247
 | 
  
    | 43 | #define NOTE_C4  262
 | 
  
    | 44 | #define NOTE_CS4 277
 | 
  
    | 45 | #define NOTE_D4  294
 | 
  
    | 46 | #define NOTE_DS4 311
 | 
  
    | 47 | #define NOTE_E4  330
 | 
  
    | 48 | #define NOTE_F4  349
 | 
  
    | 49 | #define NOTE_FS4 370
 | 
  
    | 50 | #define NOTE_G4  392
 | 
  
    | 51 | #define NOTE_GS4 415
 | 
  
    | 52 | #define NOTE_A4  440
 | 
  
    | 53 | #define NOTE_AS4 466
 | 
  
    | 54 | #define NOTE_B4  494
 | 
  
    | 55 | #define NOTE_C5  523
 | 
  
    | 56 | #define NOTE_CS5 554
 | 
  
    | 57 | #define NOTE_D5  587
 | 
  
    | 58 | #define NOTE_DS5 622
 | 
  
    | 59 | #define NOTE_E5  659
 | 
  
    | 60 | #define NOTE_F5  698
 | 
  
    | 61 | #define NOTE_FS5 740
 | 
  
    | 62 | #define NOTE_G5  784
 | 
  
    | 63 | #define NOTE_GS5 831
 | 
  
    | 64 | #define NOTE_A5  880
 | 
  
    | 65 | #define NOTE_AS5 932
 | 
  
    | 66 | #define NOTE_B5  988
 | 
  
    | 67 | #define NOTE_C6  1047
 | 
  
    | 68 | #define NOTE_CS6 1109
 | 
  
    | 69 | #define NOTE_D6  1175
 | 
  
    | 70 | #define NOTE_DS6 1245
 | 
  
    | 71 | #define NOTE_E6  1319
 | 
  
    | 72 | #define NOTE_F6  1397
 | 
  
    | 73 | #define NOTE_FS6 1480
 | 
  
    | 74 | #define NOTE_G6  1568
 | 
  
    | 75 | #define NOTE_GS6 1661
 | 
  
    | 76 | #define NOTE_A6  1760
 | 
  
    | 77 | #define NOTE_AS6 1865
 | 
  
    | 78 | #define NOTE_B6  1976
 | 
  
    | 79 | #define NOTE_C7  2093
 | 
  
    | 80 | #define NOTE_CS7 2217
 | 
  
    | 81 | #define NOTE_D7  2349
 | 
  
    | 82 | #define NOTE_DS7 2489
 | 
  
    | 83 | #define NOTE_E7  2637
 | 
  
    | 84 | #define NOTE_F7  2794
 | 
  
    | 85 | #define NOTE_FS7 2960
 | 
  
    | 86 | #define NOTE_G7  3136
 | 
  
    | 87 | #define NOTE_GS7 3322
 | 
  
    | 88 | #define NOTE_A7  3520
 | 
  
    | 89 | #define NOTE_AS7 3729
 | 
  
    | 90 | #define NOTE_B7  3951
 | 
  
    | 91 | #define NOTE_C8  4186
 | 
  
    | 92 | #define NOTE_CS8 4435
 | 
  
    | 93 | #define NOTE_D8  4699
 | 
  
    | 94 | #define NOTE_DS8 4978
 | 
  
    | 95 | 
 | 
  
    | 96 | 
 | 
  
    | 97 | #define melodyPin 9
 | 
  
    | 98 | 
 | 
  
    | 99 | // Jingle Bells
 | 
  
    | 100 | 
 | 
  
    | 101 | int melody[] = {
 | 
  
    | 102 |   NOTE_E5, NOTE_E5, NOTE_E5,
 | 
  
    | 103 |   NOTE_E5, NOTE_E5, NOTE_E5,
 | 
  
    | 104 |   NOTE_E5, NOTE_G5, NOTE_C5, NOTE_D5,
 | 
  
    | 105 |   NOTE_E5,
 | 
  
    | 106 |   NOTE_F5, NOTE_F5, NOTE_F5, NOTE_F5,
 | 
  
    | 107 |   NOTE_F5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5,
 | 
  
    | 108 |   NOTE_E5, NOTE_D5, NOTE_D5, NOTE_E5,
 | 
  
    | 109 |   NOTE_D5, NOTE_G5
 | 
  
    | 110 | };
 | 
  
    | 111 | 
 | 
  
    | 112 | int tempo[] = {
 | 
  
    | 113 |   8, 8, 4,
 | 
  
    | 114 |   8, 8, 4,
 | 
  
    | 115 |   8, 8, 8, 8,
 | 
  
    | 116 |   2,
 | 
  
    | 117 |   8, 8, 8, 8,
 | 
  
    | 118 |   8, 8, 8, 16, 16,
 | 
  
    | 119 |   8, 8, 8, 8,
 | 
  
    | 120 |   4, 4
 | 
  
    | 121 | };
 | 
  
    | 122 | 
 | 
  
    | 123 | // We wish you a merry Christmas
 | 
  
    | 124 | 
 | 
  
    | 125 | int wish_melody[] = {
 | 
  
    | 126 |   NOTE_B3, 
 | 
  
    | 127 |   NOTE_F4, NOTE_F4, NOTE_G4, NOTE_F4, NOTE_E4,
 | 
  
    | 128 |   NOTE_D4, NOTE_D4, NOTE_D4,
 | 
  
    | 129 |   NOTE_G4, NOTE_G4, NOTE_A4, NOTE_G4, NOTE_F4,
 | 
  
    | 130 |   NOTE_E4, NOTE_E4, NOTE_E4,
 | 
  
    | 131 |   NOTE_A4, NOTE_A4, NOTE_B4, NOTE_A4, NOTE_G4,
 | 
  
    | 132 |   NOTE_F4, NOTE_D4, NOTE_B3, NOTE_B3,
 | 
  
    | 133 |   NOTE_D4, NOTE_G4, NOTE_E4,
 | 
  
    | 134 |   NOTE_F4
 | 
  
    | 135 | };
 | 
  
    | 136 | 
 | 
  
    | 137 | int wish_tempo[] = {
 | 
  
    | 138 |   4,
 | 
  
    | 139 |   4, 8, 8, 8, 8,
 | 
  
    | 140 |   4, 4, 4,
 | 
  
    | 141 |   4, 8, 8, 8, 8,
 | 
  
    | 142 |   4, 4, 4,
 | 
  
    | 143 |   4, 8, 8, 8, 8,
 | 
  
    | 144 |   4, 4, 8, 8,
 | 
  
    | 145 |   4, 4, 4,
 | 
  
    | 146 |   2
 | 
  
    | 147 | };
 | 
  
    | 148 | 
 | 
  
    | 149 | // Santa Claus is coming to town
 | 
  
    | 150 | 
 | 
  
    | 151 | int santa_melody[] = {
 | 
  
    | 152 |   NOTE_G4,
 | 
  
    | 153 |   NOTE_E4, NOTE_F4, NOTE_G4, NOTE_G4, NOTE_G4,
 | 
  
    | 154 |   NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, NOTE_C5,
 | 
  
    | 155 |   NOTE_E4, NOTE_F4, NOTE_G4, NOTE_G4, NOTE_G4,
 | 
  
    | 156 |   NOTE_A4, NOTE_G4, NOTE_F4, NOTE_F4,
 | 
  
    | 157 |   NOTE_E4, NOTE_G4, NOTE_C4, NOTE_E4,
 | 
  
    | 158 |   NOTE_D4, NOTE_F4, NOTE_B3,
 | 
  
    | 159 |   NOTE_C4
 | 
  
    | 160 | };
 | 
  
    | 161 | 
 | 
  
    | 162 | int santa_tempo[] = {
 | 
  
    | 163 |   8,
 | 
  
    | 164 |   8, 8, 4, 4, 4,
 | 
  
    | 165 |   8, 8, 4, 4, 4,
 | 
  
    | 166 |   8, 8, 4, 4, 4,
 | 
  
    | 167 |   8, 8, 4, 2,
 | 
  
    | 168 |   4, 4, 4, 4,
 | 
  
    | 169 |   4, 2, 4,
 | 
  
    | 170 |   1
 | 
  
    | 171 | };
 | 
  
    | 172 | 
 | 
  
    | 173 | 
 | 
  
    | 174 | 
 | 
  
    | 175 | 
 | 
  
    | 176 | 
 | 
  
    | 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
 | 
  
    | 190 |   }
 | 
  
    | 191 |   digitalWrite(13, LOW);
 | 
  
    | 192 | 
 | 
  
    | 193 | }
 | 
  
    | 194 | 
 | 
  
    | 195 | int song = 0;
 | 
  
    | 196 | 
 | 
  
    | 197 | void sing(int s) {
 | 
  
    | 198 |   // iterate over the notes of the melody:
 | 
  
    | 199 |   song = s;
 | 
  
    | 200 |   if (song == 3) {
 | 
  
    | 201 |     Serial.println(" 'We wish you a Merry Christmas'");
 | 
  
    | 202 |     int size = sizeof(wish_melody) / sizeof(int);
 | 
  
    | 203 |     for (int thisNote = 0; thisNote < size; thisNote++) {
 | 
  
    | 204 | 
 | 
  
    | 205 |       // to calculate the note duration, take one second
 | 
  
    | 206 |       // divided by the note type.
 | 
  
    | 207 |       //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
 | 
  
    | 208 |       int noteDuration = 1000 / wish_tempo[thisNote];
 | 
  
    | 209 | 
 | 
  
    | 210 |       buzz(melodyPin, wish_melody[thisNote], noteDuration);
 | 
  
    | 211 | 
 | 
  
    | 212 |       // to distinguish the notes, set a minimum time between them.
 | 
  
    | 213 |       // the note's duration + 30% seems to work well:
 | 
  
    | 214 |       int pauseBetweenNotes = noteDuration * 1.30;
 | 
  
    | 215 |       delay(pauseBetweenNotes);
 | 
  
    | 216 | 
 | 
  
    | 217 |       // stop the tone playing:
 | 
  
    | 218 |       buzz(melodyPin, 0, noteDuration);
 | 
  
    | 219 | 
 | 
  
    | 220 |     }
 | 
  
    | 221 |   } else if (song == 2) {
 | 
  
    | 222 |     Serial.println(" 'Santa Claus is coming to town'");
 | 
  
    | 223 |     int size = sizeof(santa_melody) / sizeof(int);
 | 
  
    | 224 |     for (int thisNote = 0; thisNote < size; thisNote++) {
 | 
  
    | 225 | 
 | 
  
    | 226 |       // to calculate the note duration, take one second
 | 
  
    | 227 |       // divided by the note type.
 | 
  
    | 228 |       //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
 | 
  
    | 229 |       int noteDuration = 900 / santa_tempo[thisNote];
 | 
  
    | 230 | 
 | 
  
    | 231 |       buzz(melodyPin, santa_melody[thisNote], noteDuration);
 | 
  
    | 232 | 
 | 
  
    | 233 |       // to distinguish the notes, set a minimum time between them.
 | 
  
    | 234 |       // the note's duration + 30% seems to work well:
 | 
  
    | 235 |       int pauseBetweenNotes = noteDuration * 1.30;
 | 
  
    | 236 |       delay(pauseBetweenNotes);
 | 
  
    | 237 | 
 | 
  
    | 238 |       // stop the tone playing:
 | 
  
    | 239 |       buzz(melodyPin, 0, noteDuration);
 | 
  
    | 240 | 
 | 
  
    | 241 |     }
 | 
  
    | 242 |   } else {
 | 
  
    | 243 | 
 | 
  
    | 244 |     Serial.println(" 'Jingle Bells'");
 | 
  
    | 245 |     int size = sizeof(melody) / sizeof(int);
 | 
  
    | 246 |     for (int thisNote = 0; thisNote < size; thisNote++) {
 | 
  
    | 247 | 
 | 
  
    | 248 |       // to calculate the note duration, take one second
 | 
  
    | 249 |       // divided by the note type.
 | 
  
    | 250 |       //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
 | 
  
    | 251 |       int noteDuration = 1000 / tempo[thisNote];
 | 
  
    | 252 | 
 | 
  
    | 253 |       buzz(melodyPin, melody[thisNote], noteDuration);
 | 
  
    | 254 | 
 | 
  
    | 255 |       // to distinguish the notes, set a minimum time between them.
 | 
  
    | 256 |       // the note's duration + 30% seems to work well:
 | 
  
    | 257 |       int pauseBetweenNotes = noteDuration * 1.30;
 | 
  
    | 258 |       delay(pauseBetweenNotes);
 | 
  
    | 259 | 
 | 
  
    | 260 |       // stop the tone playing:
 | 
  
    | 261 |       buzz(melodyPin, 0, noteDuration);
 | 
  
    | 262 | 
 | 
  
    | 263 |     }
 | 
  
    | 264 |   }
 | 
  
    | 265 | }
 | 
  
    | 266 | 
 | 
  
    | 267 | 
 | 
  
    | 268 | 
 | 
  
    | 269 | 
 | 
  
    | 270 | 
 | 
  
    | 271 | 
 | 
  
    | 272 | 
 |