DFPlayer code help

I know only basics with Arduino and I need help for my next project which is live size LEGO motorcycle and I need to play two sounds with two buttons:

First button - engine - sound start after push the button, if you push button during this playback sound stop and start the new sound with engine shut off. If you leave this, the shut off sound will play after engine sound

Second button - horn - if you hold this button sound will play in the loop and if it is possible this should play even if the engine sound is playing

Now I have Arduino and DFplayer with some basic code from Youtube which play first file on the card and this is working but I don´t know how code everything else.

Is there anyone who can help me with code? Thank you.

Hi @psychoward666
welcome to the arduino-forum

directly after registering you got presented a link to read this
You should take 20 minutes of your precious time to read it.
It will help you finishing your project minimum 200 minutes faster

You should post the code as far as you have it.
Including your attempt to modify the code.
Use this method for posting the complete sketch

best regards Stefan

The DFPlayer can’t play two sounds at the same time but could switch to a mp3 recording with both sounds

Use a button library to make your life easy

Also the DF player has a lot of built in functionality and it may be possible to do all this without programming. Be sure to read the documentation carefully.

Thank you for your help, I can use some 12V horn separately and I will use Arduino and DFplayer only for motorcycle sound. I finally found solution and I have only one little problem.

Sound 1 is engine start and engine idle, reving etc..., sound 2 is engine shut off.

If you push the button during sound 1 sound 2 will play. But I need add sound 2 to play after sound 1 and during this button will not do anything. Any help with this? And here is my code, is originally code for MP3 player, I just changed the loop section

#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"

SoftwareSerial mySoftwareSerial(3, 2); // RX, TX
DFRobotDFPlayerMini myDFPlayer;

int button1 = 6;

boolean isPlaying = false;

void setup() {
  pinMode(button1, INPUT);
  digitalWrite(button1, HIGH);
  
  mySoftwareSerial.begin(9600);
  Serial.begin(9600);

  delay(1000);

  Serial.println();
  Serial.println("DFPlayer Mini Demo");
  Serial.println("Initializing DFPlayer...");

  if (!myDFPlayer.begin(mySoftwareSerial)) {
     Serial.println("Unable to begin:");
     Serial.println("1.Please recheck the connection!");
     Serial.println("2.Please insert the SD card!");
     while (true);
  }
  Serial.println(F("DFPlayer Mini online."));

  myDFPlayer.setTimeOut(500);


  //----Set volume----
  myDFPlayer.volume(30); //Set volume value (0~30).
  //myDFPlayer.volumeUp(); //Volume Up
  //myDFPlayer.volumeDown(); //Volume Down

  myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD);
}

//===========================================================
void loop() {
  if (digitalRead(button1) == LOW) {
        if (isPlaying) {
            myDFPlayer.play(2);
            isPlaying = false;
            delay(1000);
        } else {
            isPlaying = true;
            myDFPlayer.play(1);
            delay(5000);
            }
      delay(500);
    }    
}

I think I found a solution, it work only for file with the exact time, but I think it is ok for me.

#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"

unsigned long start_time; 
unsigned long timed_event;
unsigned long current_time;

SoftwareSerial mySoftwareSerial(3, 2); // RX, TX
DFRobotDFPlayerMini myDFPlayer;

int button1 = 6;

boolean isPlaying = false;

void setup() {
  pinMode(button1, INPUT);
  digitalWrite(button1, HIGH);
  
  mySoftwareSerial.begin(9600);
  Serial.begin(9600);

  timed_event = 65000; // after 1000 ms trigger the event
	current_time = millis();
	start_time = current_time;

  delay(1000);

  Serial.println();
  Serial.println("DFPlayer Mini Demo");
  Serial.println("Initializing DFPlayer...");

  if (!myDFPlayer.begin(mySoftwareSerial)) {
     Serial.println("Unable to begin:");
     Serial.println("1.Please recheck the connection!");
     Serial.println("2.Please insert the SD card!");
     while (true);
  }
  Serial.println(F("DFPlayer Mini online."));

  myDFPlayer.setTimeOut(500);


  //----Set volume----
  myDFPlayer.volume(30); //Set volume value (0~30).
  //myDFPlayer.volumeUp(); //Volume Up
  //myDFPlayer.volumeDown(); //Volume Down

  myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD);
}

//===========================================================
void loop() {
  if (digitalRead(button1) == LOW) {
    myDFPlayer.play(1);
    delay(2500);
    isPlaying = true;
    current_time = millis();
    while (isPlaying) {
      if(digitalRead(button1) == LOW) {
        myDFPlayer.play(2);
        isPlaying = false;
        delay(1000);
        return 0;
        }
    if (millis() - current_time >= timed_event) {
      isPlaying = false;
      myDFPlayer.play(2);
      delay(1000);  
      }
    }  
  }
}