i was also wondering if i got this code put together correctly....
//////////NOTE LIBRARY
#define NOTE_B0 31
#define NOTE_C1 33
#define NOTE_CS1 35
#define NOTE_D1 37
#define NOTE_DS1 39
#define NOTE_E1 41
#define NOTE_F1 44
#define NOTE_FS1 46
#define NOTE_G1 49
#define NOTE_GS1 52
#define NOTE_A1 55
#define NOTE_AS1 58
#define NOTE_B1 62
#define NOTE_C2 65
#define NOTE_CS2 69
#define NOTE_D2 73
#define NOTE_DS2 78
#define NOTE_E2 82
#define NOTE_F2 87
#define NOTE_FS2 93
#define NOTE_G2 98
#define NOTE_GS2 104
#define NOTE_A2 110
#define NOTE_AS2 117
#define NOTE_B2 123
#define NOTE_C3 131
#define NOTE_CS3 139
#define NOTE_D3 147
#define NOTE_DS3 156
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_FS3 185
#define NOTE_G3 196
#define NOTE_GS3 208
#define NOTE_A3 220
#define NOTE_AS3 233
#define NOTE_B3 247
#define NOTE_C4 262
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_G4 392
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_CS5 554
#define NOTE_D5 587
#define NOTE_DS5 622
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_FS5 740
#define NOTE_G5 784
#define NOTE_GS5 831
#define NOTE_A5 880
#define NOTE_AS5 932
#define NOTE_B5 988
#define NOTE_C6 1047
#define NOTE_CS6 1109
#define NOTE_D6 1175
#define NOTE_DS6 1245
#define NOTE_E6 1319
#define NOTE_F6 1397
#define NOTE_FS6 1480
#define NOTE_G6 1568
#define NOTE_GS6 1661
#define NOTE_A6 1760
#define NOTE_AS6 1865
#define NOTE_B6 1976
#define NOTE_C7 2093
#define NOTE_CS7 2217
#define NOTE_D7 2349
#define NOTE_DS7 2489
#define NOTE_E7 2637
#define NOTE_F7 2794
#define NOTE_FS7 2960
#define NOTE_G7 3136
#define NOTE_GS7 3322
#define NOTE_A7 3520
#define NOTE_AS7 3729
#define NOTE_B7 3951
#define NOTE_C8 4186
#define NOTE_CS8 4435
#define NOTE_DS8 4978
#define FIRST_TONE_PIN 2
#define NUM_OF_VOICES 2
/////////////////////////////////////OH CHRISTMAS TREE
//the notes in the melody of this song, 0's are pauses
int melody1 = {
NOTE_D5, 0, NOTE_G5, 0, NOTE_G5, 0, NOTE_G5, 0,
NOTE_A5, 0, NOTE_B5, 0, NOTE_B5, 0, NOTE_B5, 0,
NOTE_B5, 0, NOTE_A5, 0, NOTE_B5, 0, NOTE_C6, 0,
NOTE_FS5, 0, NOTE_A5, 0, NOTE_G5, 0
};
//the note lengths in milliseconds for each note/pause in the melody
int melDurations1 = {
533, 20, 400, 20, 133, 20, 533, 20,
533, 20, 400, 20, 133, 20, 533, 40,
266, 20, 266, 20, 266, 20, 533, 20,
533, 20, 533, 20, 533
};
//the notes in the harmony of this song
int harmony1 = {
0, 0, NOTE_D5, 0, NOTE_D5, 0, NOTE_D5, 0,
NOTE_G5, 0, NOTE_G5, 0, NOTE_G5, 0, NOTE_G5, 0,
NOTE_G5, 0, NOTE_G5, 0, NOTE_GS5, 0, NOTE_A5, 0,
NOTE_D5, 0, NOTE_D5, 0, NOTE_D5, 0
};
//the notes lengths for the harmony
int harmDurations1 = {
533, 20, 400, 20, 133, 20, 533, 20,
533, 20, 400, 20, 133, 20, 533, 40,
266, 20, 266, 20, 266, 20, 533, 20,
533, 20, 533, 20, 533
};
////////////////////////////FROSTY THE SNOWMAN
int melody2 = {
NOTE_G5, 0, NOTE_E5, 0, NOTE_F5, 0,
NOTE_G5, 0, NOTE_C6, 0, NOTE_B5, 0, NOTE_C6, 0,
NOTE_D6, 0, NOTE_C6, 0, NOTE_B5, 0, NOTE_A5, 0, NOTE_G5, 0,
NOTE_B5, 0, NOTE_C6, 0,
NOTE_D6, 0, NOTE_C6, 0, NOTE_B5,0, NOTE_A5, 0, NOTE_A5, 0,
NOTE_G5, 0, NOTE_C6, 0, NOTE_E5, 0, NOTE_G5, 0, NOTE_A5, 0,
NOTE_G5, 0, NOTE_F5, 0, NOTE_E5, 0, NOTE_F5, 0, NOTE_G5
};
int melDurations2 = {
400, 20, 200, 20, 100, 20,
200, 20, 400, 20, 100, 20, 100, 20,
200, 20, 200, 20, 200, 20, 100, 20, 300, 200,
100, 20, 100, 20,
200, 20, 200, 20, 200, 20, 100, 20, 100, 20,
100, 20, 200, 20, 300, 20, 100, 20, 100, 20,
200, 20, 200, 20, 200, 20, 200, 20, 400
};
int harmony2 = {
NOTE_G5, 0, NOTE_E5, 0, NOTE_F5, 0,
NOTE_G5, 0, NOTE_C6, 0, NOTE_B5, 0, NOTE_C6, 0,
NOTE_D6, 0, NOTE_C6, 0, NOTE_B5, 0, NOTE_A5, 0, NOTE_G5, 0,
NOTE_B5, 0, NOTE_C6, 0,
NOTE_D6, 0, NOTE_C6, 0, NOTE_B5,0, NOTE_A5, 0, NOTE_A5, 0,
NOTE_G5, 0, NOTE_C6, 0, NOTE_E5, 0, NOTE_G5, 0, NOTE_A5, 0,
NOTE_G5, 0, NOTE_F5, 0, NOTE_E5, 0, NOTE_F5, 0, NOTE_G5
};
int harmDurations2 = {
400, 20, 200, 20, 100, 20,
200, 20, 400, 20, 100, 20, 100, 20,
200, 20, 200, 20, 200, 20, 100, 20, 300, 200,
100, 20, 100, 20,
200, 20, 200, 20, 200, 20, 100, 20, 100, 20,
100, 20, 200, 20, 300, 20, 100, 20, 100, 20,
200, 20, 200, 20, 200, 20, 200, 20, 400
};
///////////////////////////////////////////////JINGLE BELLS
int melody3 = {
NOTE_E5, 0, NOTE_E5, 0, NOTE_E5, 0,
NOTE_E5, 0, NOTE_E5, 0, NOTE_E5, 0,
NOTE_E5, 0, NOTE_G5, 0, NOTE_C5, 0, NOTE_D5, 0, NOTE_E5,
NOTE_F5, 0, NOTE_F5, 0, NOTE_F5, 0, NOTE_F5, 0,
NOTE_F5, 0, NOTE_E5, 0, NOTE_E5, 0, NOTE_E5, 0,
NOTE_G5, 0, NOTE_G5, 0, NOTE_F5, 0, NOTE_D5, 0, NOTE_C5
};
int melDurations3 = {
200, 20, 200, 20, 400, 20,
200, 20, 200, 20, 400, 20,
200, 20, 200, 20, 200, 20, 200, 20, 800,
200, 20, 200, 20, 200, 20, 200, 20,
200, 20, 200, 20, 200, 20, 200, 20,
200, 20, 200, 20, 200, 20, 200, 20, 800
};
int harmony3 = {
NOTE_C5, 0, NOTE_G4, 0, NOTE_C5, 0, NOTE_G4, 0,
NOTE_C5, 0, NOTE_G4, 0, NOTE_C5, 0, NOTE_G4, 0,
NOTE_G4, 0, NOTE_A4, 0, NOTE_G4, 0, NOTE_G4, 0,
NOTE_B4, 0, NOTE_G4, 0, NOTE_E4
};
int harmDurations3 = {
420, 20, 400, 20, 420, 20, 400, 20,
420, 20, 400, 20, 420, 20, 400, 20,
420, 20, 400, 20, 420, 20, 400, 20,
420, 20, 400, 20, 800
};
///////////////////////////////////////////////////DECK THE HALLS
int melody4 = {
NOTE_C5, 0, NOTE_AS4, 0, NOTE_A4, 0, NOTE_G4, 0,
NOTE_F4, 0, NOTE_G4, 0, NOTE_A4, 0, NOTE_F4, 0,
NOTE_G4, 0, NOTE_A4, 0, NOTE_AS4, 0, NOTE_G4, 0, NOTE_A4, 0, NOTE_G4, 0,
NOTE_F4, 0, NOTE_E4, 0, NOTE_F4, 0,
NOTE_C5, 0, NOTE_AS4, 0, NOTE_A4, 0, NOTE_G4, 0,
NOTE_F4, 0, NOTE_G4, 0, NOTE_A4, 0, NOTE_F4, 0,
NOTE_G4, 0, NOTE_A4, 0, NOTE_AS4, 0, NOTE_G4, 0, NOTE_A4, 0, NOTE_G4, 0,
NOTE_F4, 0, NOTE_E4, 0, NOTE_F4, 0
};
int melDurations4 = {
730, 20, 230, 20, 480, 20, 480, 20,
480, 20, 480, 20, 480, 20, 480, 20,
230, 20, 230, 20, 230, 20, 230, 20, 730, 20, 230, 20,
480, 20, 480, 20, 980, 20,
730, 20, 230, 20, 480, 20, 480, 20,
480, 20, 480, 20, 480, 20, 480, 20,
230, 20, 230, 20, 230, 20, 230, 20, 730, 20, 230, 20,
480, 20, 480, 20, 980, 20
};
int harmony4={
NOTE_A4, 0, NOTE_G4, 0, NOTE_F4, 0, NOTE_E4, 0,
NOTE_D4, 0, NOTE_E4, 0, NOTE_F4, 0, NOTE_D4, 0,
NOTE_E4, 0, NOTE_F4, 0, NOTE_G4, 0, NOTE_E4, 0, NOTE_F4, 0, NOTE_D4, 0,
NOTE_C4, 0, NOTE_C4, 0, NOTE_C4, 0,
NOTE_A4, 0, NOTE_G4, 0, NOTE_F4, 0, NOTE_E4, 0,
NOTE_D4, 0, NOTE_E4, 0, NOTE_F4, 0, NOTE_D4, 0,
NOTE_E4, 0, NOTE_F4, 0, NOTE_G4, 0, NOTE_E4, 0, NOTE_F4, 0, NOTE_D4, 0,
NOTE_C4, 0, NOTE_C4, 0, NOTE_C4, 0
};
int harmDurations4={
730, 20, 230, 20, 480, 20, 480, 20,
480, 20, 480, 20, 480, 20, 480, 20,
230, 20, 230, 20, 230, 20, 230, 20, 730, 20, 230, 20,
480, 20, 480, 20, 980, 20,
730, 20, 230, 20, 480, 20, 480, 20,
480, 20, 480, 20, 480, 20, 480, 20,
230, 20, 230, 20, 230, 20, 230, 20, 730, 20, 230, 20,
480, 20, 480, 20, 980, 20
};
/////////////////////////////////////////////////////////UP ON THE HOUSETOP
int melody5 = {
NOTE_G4,0, NOTE_G4, 0, NOTE_A4, 0, NOTE_G4, 0, NOTE_E4, 0,
NOTE_C4, 0, NOTE_E4, 0, NOTE_G4, 0,
NOTE_A4, 0, NOTE_A4, 0, NOTE_G4, 0, NOTE_E4, 0,
NOTE_D4, 0, NOTE_G4, 0, NOTE_C4
};
int melDurations5 = {
400, 20, 200, 20, 200, 20, 400, 0, 400, 20,
400, 20, 400, 20, 800, 20,
400, 20, 400, 20, 400, 20, 400, 20,
400, 20, 400, 20, 800
};
int harmony5 = {
NOTE_G4,0, NOTE_G4, 0, NOTE_A4, 0, NOTE_G4, 0, NOTE_E4, 0,
NOTE_C4, 0, NOTE_E4, 0, NOTE_G4, 0,
NOTE_A4, 0, NOTE_A4, 0, NOTE_G4, 0, NOTE_E4, 0,
NOTE_D4, 0, NOTE_G4, 0, NOTE_C4
};
int harmDurations5 = {
400, 20, 200, 20, 200, 20, 400, 0, 400, 20,
400, 20, 400, 20, 800, 20,
400, 20, 400, 20, 400, 20, 400, 20,
400, 20, 400, 20, 800
};
//////////////////////////////////////WINTER WONDERLAND
int melody6= {
NOTE_C5, 0, NOTE_C5, 0, NOTE_C5, 0,
NOTE_C5, 0, NOTE_C5, 0, NOTE_A4, 0, NOTE_C5, 0,
NOTE_C5, 0, NOTE_C5, 0, NOTE_C5, 0, NOTE_C5, 0, NOTE_C5, 0, NOTE_AS4, 0, NOTE_C5, 0,
NOTE_C5, 0, NOTE_E5, 0, NOTE_E5, 0, NOTE_E5, 0, NOTE_D5, 0,
NOTE_C5, 0, NOTE_C5, 0, NOTE_C5, 0, NOTE_AS4, 0,
NOTE_A4, 0, NOTE_A4, 0, NOTE_A4, 0, NOTE_A4, 0, NOTE_G4, 0, NOTE_G4, 0, NOTE_G4, 0, NOTE_G4, 0, NOTE_F4
};
int melDurations6 = {
200, 20, 200, 20, 800, 20,
200, 20, 200, 20, 400,20, 800, 20,
200, 20, 200, 20, 800, 20, 200, 20, 200, 20, 400, 20, 800, 20,
200, 20, 200, 20, 200, 20, 200, 20, 800, 20,
200, 20, 200, 20, 200, 20, 800, 20,
200, 20, 200, 20, 200, 20, 200, 20, 200, 20, 200, 20, 200, 20, 200, 20, 800
};
int harmony6 = {
NOTE_C5, 0, NOTE_C5, 0, NOTE_C5, 0,
NOTE_C5, 0, NOTE_C5, 0, NOTE_A4, 0, NOTE_C5, 0,
NOTE_C5, 0, NOTE_C5, 0, NOTE_C5, 0, NOTE_C5, 0, NOTE_C5, 0, NOTE_AS4, 0, NOTE_C5, 0,
NOTE_C5, 0, NOTE_E5, 0, NOTE_E5, 0, NOTE_E5, 0, NOTE_D5, 0,
NOTE_C5, 0, NOTE_C5, 0, NOTE_C5, 0, NOTE_AS4, 0,
NOTE_A4, 0, NOTE_A4, 0, NOTE_A4, 0, NOTE_A4, 0, NOTE_G4, 0, NOTE_G4, 0, NOTE_G4, 0, NOTE_G4, 0, NOTE_F4
};
int harmDurations6 = {
200, 20, 200, 20, 800, 20,
200, 20, 200, 20, 400,20, 800, 20,
200, 20, 200, 20, 800, 20, 200, 20, 200, 20, 400, 20, 800, 20,
200, 20, 200, 20, 200, 20, 200, 20, 800, 20,
200, 20, 200, 20, 200, 20, 800, 20,
200, 20, 200, 20, 200, 20, 200, 20, 200, 20, 200, 20, 200, 20, 200, 20, 800
};
//declaring the Last Sensor Values(LSV)
int LSV=0;
int LSV2=0;
int LSV3=0;
int LSV4=0;
int LSV5=0;
int LSV6=0;
void setup() {
Serial.begin(9600);
for (unsigned char i = 0; i < NUM_OF_VOICES; i++) {
pinMode(i+FIRST_TONE_PIN,OUTPUT);
pinMode(8, OUTPUT);
pinMode(13, OUTPUT); //christmas lights
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
pinMode(7, OUTPUT);
LSV1=analogRead(0); //sensor 1 is connected to analog 0
LSV2=analogRead(1); //sensor 2 connects to analong 1
LSV3=analogRead(2);
LSV4=analogRead(3);
LSV5=analogRead(4);
LSV6=analogRead(5);
}
// call magic function to play both notes simultaneously//
playTune(melody1, sizeof(melody1)/sizeof(int), melDurations1, harmony1, sizeof(harmony1)/sizeof(int), harmDurations1);
playTune(melody2, sizeof(melody2)/sizeof(int), melDurations2, harmony2, sizeof(harmony2)/sizeof(int), harmDurations2);
playTune(melody3, sizeof(melody3)/sizeof(int), melDurations3, harmony3, sizeof(harmony3)/sizeof(int), harmDurations3);
playTune(melody4, sizeof(melody4)/sizeof(int), melDurations4, harmony4, sizeof(harmony4)/sizeof(int), harmDurations4);
playTune(melody5, sizeof(melody5)/sizeof(int), melDurations5, harmony5, sizeof(harmony5)/sizeof(int), harmDurations5);
playTune(melody6, sizeof(melody6)/sizeof(int), melDurations6, harmony6, sizeof(harmony6)/sizeof(int), harmDurations6);
}
void loop() {
////////sensor 1
int sensorValue1= analogRead(A0);
if (abs(sensorValue1-LSV1)>10 {
digitalWrite(13, HIGH);
playTune(melody1, sizeof(melody1)/sizeof(int), melDurations1, harmony1, sizeof(harmony1)/sizeof(int), harmDurations1);
LSV1=analogRead(A0);
delay(2);
digitalWrite(13, LOW);
/////////sensor 2
int sensorValue2= analogRead(A1);
if (abs(sensorValue2-LSV2)>10 {
digitalWrite(12, HIGH);
playTune(melody2, sizeof(melody2)/sizeof(int), melDurations2, harmony2, sizeof(harmony2)/sizeof(int), harmDurations2);
LSV2=analogRead(A1);
delay(2);
digitalWrite(12, LOW);
////////////sensor 3
int sensorValue3= analogRead(A2);
if (abs(sensorValue3-LSV3)>10 {
digitalWrite(11, HIGH);
playTune(melody3, sizeof(melody3)/sizeof(int), melDurations3, harmony3, sizeof(harmony3)/sizeof(int), harmDurations3);
LSV3=analogRead(A2);
delay(2);
digitalWrite(11, LOW);
////////sensor 4
int sensorValue4= analogRead(A3);
if (abs(sensorValue4-LSV4)>10 {
digitalWrite(10, HIGH);
playTune(melody4, sizeof(melody4)/sizeof(int), melDurations4, harmony4, sizeof(harmony4)/sizeof(int), harmDurations4);
LSV4=analogRead(A3);
delay(2);
digitalWrite(10, LOW);
///////sensor 5
int sensorValue5= analogRead(A4);
if (abs(sensorValue5-LSV5)>10 {
digitalWrite(9, HIGH);
playTune(melody5, sizeof(melody5)/sizeof(int), melDurations5, harmony5, sizeof(harmony5)/sizeof(int), harmDurations5);
LSV5=analogRead(A4);
delay(2);
digitalWrite(9, LOW);
///sensor 6
int sensorValue6= analogRead(A5);
if (abs(sensorValue6-LSV6)>10 {
digitalWrite(7, HIGH);
playTune(melody6, sizeof(melody6)/sizeof(int), melDurations6, harmony6, sizeof(harmony6)/sizeof(int), harmDurations6);
LSV6=analogRead(A5);
delay(2);
digitalWrite(7, LOW);
}
//DO NOT DELETE // no need to modify anything below for basic use
void playTune(int melody[], int melLength, int melDurations[], int harmony[], int harmLength, int harmDurations[])
{
unsigned int Counts[] = { 0, 0};
unsigned int Periods[] = { 0, 0};
unsigned char States[] = { 0, 0};
unsigned long stopTimeNote1 = 0;
unsigned long stopTimeNote2 = 0;
unsigned char TimedPin = 0;
unsigned int CurrentCount = 0;
unsigned char i;
unsigned char indexMel = 0, indexHarm = 0;
while (1) {
CurrentCount = Counts[0];
TimedPin = 0;
for (i=1;i<NUM_OF_VOICES;i++) {
if (Counts[i] < CurrentCount) {
TimedPin = i;
CurrentCount = Counts[i];
} // for
}
if (CurrentCount > 3)
delayMicroseconds(CurrentCount);
if (Periods[TimedPin] < 65535)
States[TimedPin] = !States[TimedPin];
digitalWrite(FIRST_TONE_PIN+TimedPin,States[TimedPin]);
if (millis() >= stopTimeNote1) {
if (indexMel >= melLength)
break;
stopTimeNote1 = millis() + melDurations[indexMel];
Periods[0] = 1000000 / melody[indexMel++];
}
if (millis() >= stopTimeNote2) {
if (indexHarm >= harmLength)
break;
stopTimeNote2 = millis() + harmDurations[indexHarm];
Periods[1] = 1000000 / harmony[indexHarm++];
}
for (i = 0; i < NUM_OF_VOICES; i++) {
Counts[i] = Counts[i] - CurrentCount;
}
Counts[TimedPin] = Periods[TimedPin];
}
}