homework for week 5: Music Box

The two dancers dance elegantly with the music Canon, and the colorful light blinking inside the box as well.

Code:

/*
Canon for summer night (with LED) by aNdY 2013
*/

#include “pitches.h”
#include <Servo.h>

Servo myservo1; //180
Servo myservo2; //360
int value1 = 360;
int value2 = 0;

// notes in the melody:
int CanonTone[] = {
NOTE_E3, NOTE_G4, NOTE_C5, NOTE_E5, NOTE_B2, NOTE_G4, NOTE_B4, NOTE_D5,
NOTE_C3, NOTE_E4, NOTE_A4, NOTE_C5, NOTE_G2, NOTE_E4, NOTE_G4, NOTE_B4,
NOTE_A2, NOTE_C4, NOTE_F4, NOTE_A4, NOTE_E1, NOTE_C4, NOTE_E4, NOTE_G4,
NOTE_A2, NOTE_D4, NOTE_F4, NOTE_A4, NOTE_B2, NOTE_D4, NOTE_G4, NOTE_B4,
NOTE_E3, NOTE_E5, NOTE_F5, NOTE_G5, NOTE_E5, NOTE_F5, NOTE_G5, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5, NOTE_D5, NOTE_E5, NOTE_F5,
NOTE_E5, NOTE_C5, NOTE_D5, NOTE_E5, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_G4, NOTE_F4, NOTE_G4, NOTE_F4, NOTE_F4, NOTE_G4,
NOTE_F4, NOTE_A4, NOTE_G4, NOTE_F4, NOTE_E4, NOTE_D4, NOTE_E4, NOTE_D4, NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4,
NOTE_F4, NOTE_A4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5, NOTE_D5, NOTE_E5, NOTE_F5, NOTE_G5,
NOTE_E5, NOTE_C5, NOTE_D5, NOTE_E5, NOTE_D5, NOTE_C5, NOTE_D5, NOTE_B4, NOTE_C5, NOTE_D5, NOTE_E5, NOTE_D5, NOTE_C5, NOTE_B4,
NOTE_C5, NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_E4, NOTE_D4, NOTE_E4, NOTE_C5, NOTE_B4, NOTE_C5,
NOTE_A4, NOTE_C5, NOTE_B4, NOTE_A4, NOTE_G4, NOTE_F4, NOTE_G4, NOTE_F4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5,
NOTE_A4, NOTE_C5, NOTE_B4, NOTE_C5, NOTE_B4, NOTE_A4, NOTE_B4, NOTE_C5, NOTE_D5, NOTE_C5, NOTE_B4, NOTE_C5, NOTE_A4, NOTE_B4,
NOTE_C5,0
};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8,
8,16,16,8,16,16,16,16,16,16,16,16,16, 16,
8,16,16,8,16,16,16,16,16,16,16,16,16, 16,
8,16,16,8,16,16,16,16,16,16,16,16,16, 16,
8,16,16,8,16,16,16,16,16,16,16,16,16, 16,
8,16,16,8,16,16,16,16,16,16,16,16,16, 16,
8,16,16,8,16,16,16,16,16,16,16,16,16, 16,
8,16,16,8,16,16,16,16,16,16,16,16,16, 16,
8,16,16,8,16,16,16,16,16,16,16,16,16, 16,
1
};

int toneSpeed = 1800; // set music speed
int ledPin;
int speakerPin = A0;
int thisNote;

void setup() {
myservo1.attach(13);
myservo2.attach(0);

thisNote = 0;
for(int pin = 1; pin <= 12; pin++) {
pinMode(pin, OUTPUT);
}
}
void loop() {
while(CanonTone[thisNote] > 0) {
// int flag = 0;
// if (flag == 0) {
// value1 += 10;
// if (value1 == 180) {
// flag = 1;
// }
// }
// else if (flag == 1){
// value1 -= 10;
// if (value1 == 0) {
// flag = 0;
// }
// }
if(thisNote < 32)
value1 -= 45;
else
value1 -= 30;
value2 += 1;
if(value1 <= 0) {
value1 += 360;
}
if(value2 >= 360) {
value1 -= 360;
}
myservo1.write(value1);
myservo2.write(value2);

int toneLevel = CanonTone[thisNote]; // get tone pitch
switch(toneLevel) {
case 262: // if C4, pin = 1
ledPin = 1;
break;
case 294: // if D4, pin = 2
ledPin = 2;
break;
case 330: // if E4, pin = 3
ledPin = 3;
break;
case 349:
ledPin = 4;
break;
case 392:
ledPin = 5;
break;
case 440:
ledPin = 6;
break;
case 494:
ledPin = 7;
break;
case 523:
ledPin = 8;
break;
case 587:
ledPin = 9;
break;
case 659:
ledPin = 10;
break;
case 698:
ledPin = 11;
break;
case 784:
ledPin = 12;
break;
default:
ledPin = 1;
break;
}
digitalWrite(ledPin, HIGH);

int noteDuration = toneSpeed / noteDurations[thisNote];
tone(speakerPin, CanonTone[thisNote], noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note’s duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
digitalWrite(ledPin, LOW);
delay(2);
thisNote++;
if(thisNote >= 144)
thisNote -= 144;
}
// noTone(speakerPin); // turn speaker off
}

Leave a Reply

Your email address will not be published. Required fields are marked *