Hey!
For a project I need to make the following arduino setup:
I must have a piezo speaker that can play 5 songs. There is a button as well and if you press the button, the next song should start playing. But the problem is that in every song there is a delay which makes sure there are pauses between the notes so all the notes don't play at once. And because of this delay, I can't read the button state while the song is playing.
My question is: How can I convert this delay to millis() so that I CAN read the button state while the song is playing?
Thanks in advance! ![]()
#include <pitches.h>
int nextSongButton = 8;
int previousSongButton = 7;
int piezoPin = 3;
int trackCount = 0;
int track_1_speed = 90;
int melody[] = {NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4};
int noteDurations[] = {4, 8, 8, 4, 4, 4, 4, 4};
void setup() {
Serial.begin(9600);
pinMode(nextSongButton, INPUT);
pinMode(previousSongButton, INPUT);
pinMode(piezoPin, OUTPUT);
}
void loop() {
int nextSongB = digitalRead(nextSongButton);
int previousSongB = digitalRead(previousSongButton);
for (int thisNote = 0; thisNote < 8; thisNote++) {
int noteDuration = 1000 / noteDurations[thisNote];
tone(piezoPin, melody[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
noTone(piezoPin);
}
}

