/*
Forum: https://forum.arduino.cc/t/tone-on-2-pins-simultaneously/1193203/7
Wokwi: https://wokwi.com/projects/382583782521458689
*/
#include <Tone.h>
int notes1[] = {NOTE_C4, NOTE_D5, NOTE_E5, NOTE_F5, NOTE_G5};
int noOfNotes1 = sizeof notes1 / sizeof notes1[0];
int notes2[] = {NOTE_D6, NOTE_E4, NOTE_G5, NOTE_A4, NOTE_C5};
int noOfNotes2 = sizeof notes2 / sizeof notes2[0];
Tone tone1;
Tone tone2;
int count1 = 0;
int count2 = 0;
int count3 = 0;
unsigned long lastChange = 0;
void setup()
{
tone1.begin(13);
tone2.begin(12);
}
void loop()
{
if (millis() - lastChange > 33) {
lastChange = millis();
tone1.play(notes1[count1]);
tone2.play(notes2[count2]);
count1++;
if (count1 >= noOfNotes1) {
count1 = 0;
count2++;
if (count2 >= noOfNotes2) {
count2 = 0;
count3++;
}
}
if (count3 > 2){
tone1.stop();
tone2.stop();
lastChange = millis();
while (millis()-lastChange < 333);
count3 = 0;
count2 = 0;
count1 = 0;
}
}
}