How do you break out of a delay in loop?

For some reason my post was deleted - I am trying to break out of a delay in a loop that I have for an MP3 player.

void loop()
{
  static unsigned long timer = millis();

  byte buttonState2 = digitalRead(BUTTON_PIN2);
  
  if (buttonState2 == HIGH) {
      myDFPlayer.enableLoop();
      myDFPlayer.playMp3Folder(0);
      delay(15000);
      myDFPlayer.disableLoopAll();
  }
}

There are actually six of these buttons.

myDFPlayer.enableLoop() sets the looping mode to on for the mp3 player
myDFPlayer.playMp3Folder(0) - plays the first track in the mp3 folder
delay(15000) - delay 15 seconds while playing the mp3 on loop
myDFPlayer.disableLoopAll() - stop playback of mp3 after time elapsed.

I really want this to play a different track if a different button is pressed, or stop altogether if the break button is pressed. Is there a way to handle this differently? Thank you!

You can't do this if you use the delay() function. It blocks all other code while it completes.

You need a different technique, using another function called millis().

Check out this...

https://docs.arduino.cc/built-in-examples/digital/BlinkWithoutDelay

Hello barrychapman

Make it simple and stupid.

Run some tutorials for the hardware selected.
If you are happy with the results of the tutorials you can merge these to your project.

Have a nice day and enjoy coding in C++.

Then use a "commutable delay" instead of a delay.
I explain it here
Commutable Delay

I am creating a module that will essentially read 6 INPUT buttons which will trigger an audio file to play on a loop within the DFRobot MP3 Player:

Here is some of the code, with other buttons edited out for brevity:

void setup()
{
  mySoftwareSerial.begin(9600);
  Serial.begin(115200);
  
  pinMode(BUTTON_PIN2, INPUT);
  pinMode(BUTTON_PIN3, INPUT);
}


void loop()
{
  static unsigned long timer = millis();

  byte buttonState2 = digitalRead(BUTTON_PIN2);
  byte buttonState3 = digitalRead(BUTTON_PIN3);
  
  if (buttonState2 == HIGH) {
      myDFPlayer.enableLoop();
      myDFPlayer.playMp3Folder(0);
      delay(15000);
      myDFPlayer.disableLoopAll();
  }
  
  if (buttonState3 == HIGH) {
      myDFPlayer.enableLoop();
      myDFPlayer.playMp3Folder(1);
      delay(15000);
      myDFPlayer.disableLoopAll();
  }
}

The trouble is - this thing will be playing these sound samples through a set of 6 amplified loudspeakers, at around 131dB. I need a way to stop this thing if need be.

As you can see from the code above, the thing essentially captures the button state, and then plays the appropriate sound sample for whatever button was pressed (button 2 - 7).

myDFPlayer.enableLoop() - this will enable the MP3 to be played on a loop.
myDFPlayer.playMp3Folder(1) - play the second file in the MP3 folder. 2 for third, and so forth.
delay(15000) - delay for 15 seconds (will eventually be set to a minute unless an alternate solution is found.
myDFPlayer.disableLoopAll(); - stops all playing MP3's

My concern is that the delay(15000) would be blocking execution for anything else - making it difficult to listen for an interrupt. What would be the best approach here?

https://docs.arduino.cc/built-in-examples/digital/BlinkWithoutDelay

I think this post got deleted... then re-instated ?

In the meantime this one got created...

Suggest this one get closed/merged.

Topics merged

Do you want to be deaf soon?

This is not to be listened to up close, its a prototype for a warning system

Yes it would. That is why I gave you the answer in post #4.

Any reason you don't like it?
Is it too complex for you to understand?
You can always ask specifically about things you don't understand.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.