#include <Tone.h>
#include "pitches.h"
Tone tone1;
Tone tone2;
int toneArray1[] = {
NOTE_C5, NOTE_C5, NOTE_B4, NOTE_B4, NOTE_AS4, NOTE_AS4, NOTE_A4, NOTE_A4, NOTE_GS4, NOTE_GS4
};
int toneArray2[] = {
NOTE_E5, NOTE_DS5, NOTE_DS5, NOTE_D5, NOTE_D5, NOTE_CS5, NOTE_CS5, NOTE_C5, NOTE_C5, NOTE_B4
};
long durationArray[] = {
1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000
};
int toneArray1_alt[] = {
NOTE_NONE,
NOTE_AS3, NOTE_C4, NOTE_D4, NOTE_DS4, NOTE_F4, NOTE_G4, NOTE_A4,
NOTE_AS4, NOTE_A4, NOTE_G4, NOTE_F4, NOTE_DS4, NOTE_D4, NOTE_C4,
NOTE_AS3
};
int toneArray2_alt[] = {
NOTE_AS3, NOTE_C4, NOTE_D4, NOTE_DS4, NOTE_F4, NOTE_G4, NOTE_A4,
NOTE_AS4, NOTE_A4, NOTE_G4, NOTE_F4, NOTE_DS4, NOTE_D4, NOTE_C4,
NOTE_AS3//, NOTE_NONE
};
long durationArray1[] = {
1500,
500, 500, 500, 500, 500, 500, 500,
500, 500, 500, 500, 500, 500, 500,
500
};
long durationArray2[] = {
500, 500, 500, 500, 500, 500, 500,
500, 500, 500, 500, 500, 500, 500,
500//, 2000
};
float partialDurationArray1[] = {
0.8,
0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8,
0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8,
0.8
};
float partialDurationArray2[] = {
0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8,
0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8,
0.8//, 0.8
};
const byte buttonPin1 = 7;
const byte buttonPin2 = 8;
void setup() {
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
Serial.begin(115200);
tone1.begin(22);
tone2.begin(23);
tone1.play(NOTE_C5);
tone2.play(NOTE_DS5);
tone1.stop();
tone2.stop();
}
void loop() {
if (digitalRead(buttonPin1)==LOW) {
// delay(500);
for (int i=0; i<(sizeof(toneArray1) / sizeof(int)); i++) {
Serial.println(i);
tone1.play(toneArray1[i], 0.7*durationArray[i]);
tone2.play(toneArray2[i], 0.7*durationArray[i]);
delay(durationArray[i]);
}
tone1.stop();
tone2.stop();
}
if (digitalRead(buttonPin2)==LOW) {
long toneCounter1 = -1;
long toneCounter2 = -1;
long timeRemaining1 = 0;//durationArray1[toneCounter1];
long timeRemaining2 = 0;//durationArray2[toneCounter2];
long startTime = millis();
long lastTime = startTime;
// tone1.play(toneArray1_alt[toneCounter1], durationArray1[toneCounter1]*partialDurationArray1[toneCounter1]);
// tone2.play(toneArray2_alt[toneCounter2], durationArray2[toneCounter2]*partialDurationArray2[toneCounter2]);
while (toneCounter1 < (sizeof(toneArray1_alt) / sizeof(int)) || toneCounter2 < (sizeof(toneArray2_alt) / sizeof(int))) {
long thisTime = millis();
if ((thisTime-lastTime) >= timeRemaining1) {
toneCounter1 = toneCounter1 + 1;
timeRemaining1 = durationArray1[toneCounter1];
if (toneCounter1 < (sizeof(toneArray1_alt) / sizeof(int))) {
if (toneArray1_alt[toneCounter1] != NOTE_NONE) {
tone1.play(toneArray1_alt[toneCounter1], durationArray1[toneCounter1]*partialDurationArray1[toneCounter1]);
} else {
tone1.stop();
}
Serial.println("next note 1");
} else {
tone1.stop();
Serial.println(millis());
Serial.println("tone1 stopped");
}
} else {
timeRemaining1 = timeRemaining1 - (thisTime-lastTime);
}
if ((thisTime-lastTime) >= timeRemaining2) {
toneCounter2 = toneCounter2 + 1;
timeRemaining2 = durationArray2[toneCounter2];
if (toneCounter2 < (sizeof(toneArray2_alt) / sizeof(int))) {
if (toneArray2_alt[toneCounter2] != NOTE_NONE) {
tone2.play(toneArray2_alt[toneCounter2], durationArray2[toneCounter2]*partialDurationArray2[toneCounter2]);
} else {
tone2.stop();
}
Serial.println("next note 2");
} else {
tone2.stop();
Serial.println(millis());
Serial.println("tone2 stopped");
}
} else {
timeRemaining2 = timeRemaining2 - (thisTime-lastTime);
}
// Serial.print("toneCounter1 = ");
// Serial.print(toneCounter1);
// Serial.print("\t\ttoneCounter2 = ");
// Serial.println(toneCounter2);
lastTime = thisTime;
}
Serial.println("testCheck");
// for (int i=0; i<(sizeof(toneArray1) / sizeof(int)); i++) {
// Serial.println(i);
// tone1.play(toneArray1[i], 0.7*durationArray[i]);
// tone2.play(toneArray2[i], 0.7*durationArray[i]);
// delay(durationArray[i]);
// }
}
delay(10);
}