DFPlayer Mini code Troubleshooting

Dear Arduino Community,

First of all I am sorry in advance to post this message on this topicon but for some reasons I cannot post any topic on the Audio section of the forum…

I have started an MP3 project with a DFPlayer Mini linked to a ultrasonic sensor.
So far everything works fine but in my "void loop", I tried to set up a volume fade in with a for loop into myDFPlayer.volume() , but unfortunately nothing works correctly (nothing happends), this is why I had to make this messy synthax changing the volume bite by bite…

Does anyone ever struggled with that probleme, there might be something wrong in my code :confused: ?

All the best,
B.

#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
#include <PCM.h>

SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;

const int trigPin = 13;
const int echoPin = 12;
// defining variables for sensor
long duration;
int distance;

// defining buttons for mp3 player
int buttonNext = 2; 
int buttonPause = 3;
int buttonPrevious = 4; 

boolean isPlaying = false;

void setup() {
    pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
    pinMode(echoPin, INPUT); // Sets the echoPin as an Input
    
    pinMode(buttonPause, INPUT);
    digitalWrite(buttonPause, HIGH);
    pinMode(buttonNext, INPUT);
    digitalWrite(buttonNext, HIGH); 
    pinMode(buttonPrevious, INPUT);
    digitalWrite(buttonPrevious, 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(0); //Set volume value (0~30).
    //myDFPlayer.volumeUp(); //Volume Up
    //myDFPlayer.volumeDown(); //Volume Down

    //----Set different EQ----
    myDFPlayer.EQ(DFPLAYER_EQ_NORMAL);
    //  myDFPlayer.EQ(DFPLAYER_EQ_POP);
    //  myDFPlayer.EQ(DFPLAYER_EQ_ROCK);
    //  myDFPlayer.EQ(DFPLAYER_EQ_JAZZ);
    //  myDFPlayer.EQ(DFPLAYER_EQ_CLASSIC);
    //  myDFPlayer.EQ(DFPLAYER_EQ_BASS);

    myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD);

    myDFPlayer.play(1); //Play the first song
    isPlaying = true;
    Serial.println("Playing..");
}

void loop() {
      
      digitalWrite(trigPin, LOW);
      delayMicroseconds(2);
      // Sets the trigPin on HIGH state for 10 micro seconds
      digitalWrite(trigPin, HIGH);
      delayMicroseconds(10);
      digitalWrite(trigPin, LOW);
      // Reads the echoPin, returns the sound wave travel time in microseconds
      duration = pulseIn(echoPin, HIGH);
      // Calculating the distance
      distance= duration*0.034/2;
      // Prints the distance on the Serial Monitor
      Serial.print("Distance: ");
      Serial.println(distance);
      
              if (distance > 20 ){
               myDFPlayer.pause();
              }            
              if ( distance < 20 and distance > 0){
              myDFPlayer.randomAll();
                for (int i=0; i <= 30; i++){
                myDFPlayer.volume(i);}

             /* myDFPlayer.volume(5);
              myDFPlayer.volume(6);
              myDFPlayer.volume(7);
              myDFPlayer.volume(8);
              myDFPlayer.volume(9);
              myDFPlayer.volume(10);
              myDFPlayer.volume(11);
              myDFPlayer.volume(12);
              myDFPlayer.volume(13);
              myDFPlayer.volume(14);
              myDFPlayer.volume(15);
              myDFPlayer.volume(16);
              myDFPlayer.volume(17);
              myDFPlayer.volume(18);
              myDFPlayer.volume(19);
              myDFPlayer.volume(20);
              myDFPlayer.volume(21);
              myDFPlayer.volume(22);
              myDFPlayer.volume(23);
              myDFPlayer.volume(24);
              myDFPlayer.volume(25);
              myDFPlayer.volume(26);
              myDFPlayer.volume(27);
              myDFPlayer.volume(28); */
             
              delay(20000);
              
              myDFPlayer.volume(28);
              myDFPlayer.volume(27);
              myDFPlayer.volume(26);
              myDFPlayer.volume(25);
              myDFPlayer.volume(24);
              myDFPlayer.volume(23);
              myDFPlayer.volume(22);
              myDFPlayer.volume(21);
              myDFPlayer.volume(20);
              myDFPlayer.volume(19);
              myDFPlayer.volume(18);
              myDFPlayer.volume(17);
              myDFPlayer.volume(16);
              myDFPlayer.volume(15);
              myDFPlayer.volume(14);
              myDFPlayer.volume(13);
              myDFPlayer.volume(12);
              myDFPlayer.volume(11);
              myDFPlayer.volume(10);
              myDFPlayer.volume(9);
              myDFPlayer.volume(8);
              myDFPlayer.volume(7);
              myDFPlayer.volume(6);
              myDFPlayer.volume(5);
              myDFPlayer.volume(4);
              myDFPlayer.volume(3);
              myDFPlayer.volume(2);
              myDFPlayer.volume(1);
              }

    
    if (digitalRead(buttonPause) == LOW) {
        if (isPlaying) {
            myDFPlayer.pause();
            isPlaying = false;
            Serial.println("Paused..");
        } else {
            isPlaying = true;
            myDFPlayer.start();
            Serial.println("Playing..");
     
        
        }
    }   
}

This should work for fading up:

    for (int i = 0; i <= 30; i++) {
      myDFPlayer.volume(i);
    }

and this for fading down:

    for (int i = 30; i >= 0; i--) {
      myDFPlayer.volume(i);
    }

However it will fade so fast you may not notice the fade. 20 seconds is a huge delay and will halt all processing so that you cannot process distance or buttons during this delay. You might want to time the fade by using the millis() function. Here are some tutorials that might help:

BlinkWithoutDelay
Arduino Multiple Things
Several Things at a Time