Hello,
I am trying to make an audio sequencer but without using delay. I have been trying with millis() but I cannot seem to get it to work . What I intend is simple:
Play tone 1 with a specific duration
delay(tempo)
Play tone 2 with specific duration
delay(tempo)
Play tone 3 with specific duration
delay(tempo)
Play tone 4 with specific duration
delay(tempo)
I can achieve this only with delay() but when I try to incorporate millis() things don't work as intended.
Here is a portion of the code I am trying unsuccessfully inside void loop:
unsigned long timenow = millis();
if ((timenow - timestart0 >= seqtempo)&&(playtone == false)){
tone (speakerPin1, NOTE_A4); // Start playing tone
playtone = true;
timestart0 = timenow;
}
if ((timenow - timestart0 >= toneduration)&&(playtone == true)){
noTone (speakerPin1); // Stop playing tone
playtone = false;
}
if ((timenow - timestart0 >= seqtempo)&&(playtone == false)){
tone (speakerPin1, NOTE_B4); // Start playing tone
playtone = true;
timestart1 = timenow;
}
if ((timenow - timestart1 >= toneduration)&&(playtone == true)){
noTone (speakerPin1); // Stop playing tone
playtone = false;
}
if ((timenow - timestart1 >= seqtempo)&&(playtone == false)){
tone (speakerPin1, NOTE_C4); // Start playing tone
playtone = true;
timestart2 = timenow;
}
if ((timenow - timestart2 >= toneduration)&&(playtone == true)){
noTone (speakerPin1); // Stop playing tone
playtone = false;
timestart0 = timenow;
}
}
}