Performing an action in a delay

Hello, I am trying to write a program to play an MP3 file and a lamp to turn on at the same time. I’m using a DFPlayer Mini for the MP3 player, but I want to add an Ultrasonic Sensor to turn it on or off with a boolean. I have the sensor working but for the DFPlayer, I need to have a delay to play the song, so when I put my hand in front of the sensor, nothing happens. Does anyone know any way to perform an action while a delay is in action?

Thanks in advance.

Hello
show your sketch formated and in code tags </> .

Look at the example “blink without the delay”. It is the same in principle.

Here:

Yeah, I looked at that. The way the DFPlayer works is that you use the specific code “myDFPlayer.play();” but to play the song for longer than a second, you make a delay. if the delay is 5 seconds, the song plays for 5 seconds, if 100, then 100, but it needs to be a delay. So I’m trying to do something during that delay because I am planning on it being 100 seconds.

image

<#include “SoftwareSerial.h”
#include “DFRobotDFPlayerMini.h”

SoftwareSerial DFplayer(18, 19); // RX, TX
DFRobotDFPlayerMini myDFPlayer;

int i=1;

void setup() {
Serial.begin(9600);
DFplayer.begin(9600);

myDFPlayer.begin(DFplayer);

myDFPlayer.volume(30);// Min 0, Max 30

}

void loop() {
myDFPlayer.play(i);
Serial.println(myDFPlayer.readState());
i++;
delay(100000);
}

Hello
I can´t see the used relais and Ultrasonic Sensor in your sketch.

Yes, I first have this code that I want to finish with the delays and then when I complete it then I want to add it to that code

If you want to do more than one thing at the ‘same’ time, look at my tutorial on
Multi-tasking in Arduino
To remove delays see my tutorial on
How to write Timers and Delays in Arduino
multitaskingDiagramSmall

I think you are mistaken. The DFPlayer library has functions for starting to play, checking to see if the player is playing, pausing and resuming play, and stopping play. The ‘delay()’ just keeps you from sending a new play() command for some period of time. Please study the library and examples to see how you can continue to do other stuff while the player is playing.