After several hours of google searches with no luck I decided to make an account. I'm trying to make a 30 second timer that plays a pre written melody on the piezo while a number countdown is displayed on a 32spi OLED
If I remove the code for the piezo I can get the numbers to countdown, but when the code is added the numbers only countdown after the melody has fully played through.
Any help would greatly be appreciated!!
void loop()
{
display.setTextSize(5);
display.setTextColor(WHITE);
display.setCursor(textPos, 0);
display.println(secondsValue);
display.display();
// Timer logic
unsigned long currentMillis = millis();
if (startTimer) {
sing();
if (currentMillis - timerCountDownMillis > countDownMillis) {
display.clearDisplay();
secondsValue = secondsValue.toInt() - 1;
timerCountDownMillis = currentMillis;
if (secondsValue.toInt() == 0) {
secondsValue = "0";
}
if (secondsValue.toInt() < "10") {
textPos = sTextPos;
}
if (secondsValue == "0") {
startTimer = false;
}
}
}
}
// Piezo logic
void sing() {
int size = sizeof(songMelody) / sizeof(int);
if (startTimer) {
for (int thisNote = 0; thisNote < size; thisNote++) {
int songNoteDuration = 1000 / songNoteDurations[thisNote];
tone(SND, songMelody[thisNote], songNoteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = songNoteDuration * 1.30;
delay(pauseBetweenNotes);
//tunePlayed = true;
// stop the tone playing:
noTone(SND);
}
}
}