Is there any way to get the DFPlayer play immediately and not with 1 second delay?
I have one file on a 16 GB SD card and it's an uncompressed WAV file.
Does the one second delay come from the code you didn't post?
No, there is no scripted delay. Here's all code:
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Libraries
#include "SoftwareSerial.h"
#include <DFRobotDFPlayerMini.h>
//Configure these to fit your design...
#define STBY 2
#define AIN1 3
#define AIN2 4
#define PWM1 5
#define SMOK 6
#define LEDS 7
#define BLTX 9 // Unused
#define BLRX 10
#define DFRX 11 // Unused
#define DFTX 12
// Connections
SoftwareSerial Bluetooth_Serial(BLRX, BLTX); // RX, TX for D10 and D7 pins
SoftwareSerial DFPlayer_Serial(DFRX, DFTX); // RX, TX for D12 and D11 pins
// Create the Player object
DFRobotDFPlayerMini DFPlayer;
int OldSpeed;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
pinMode(PWM1, OUTPUT); // Motor speed
pinMode(AIN1, OUTPUT); // Motor A +
pinMode(AIN2, OUTPUT); // Motor A -
pinMode(STBY, OUTPUT); // Must be HIGH to disable standby
pinMode(SMOK, OUTPUT); // Smoke unit
pinMode(LEDS, OUTPUT); // Leds
// Software serial so USB doesnt need to be disconnected
Bluetooth_Serial.begin(9600);
DFPlayer_Serial.begin(9600);
Serial.begin(9600);
Serial.println("Software serial initialized.");
// Turn off motor in case it's on
analogWrite(PWM1, 150); //Speed control of Motor A | default speed
digitalWrite(STBY, LOW);
digitalWrite(AIN1, LOW);
digitalWrite(AIN2, LOW);
OldSpeed = 150;
digitalWrite(SMOK, LOW);
digitalWrite(LEDS, LOW);
if (!DFPlayer.begin(DFPlayer_Serial)) { //Use softwareSerial to communicate with mp3.
Serial.println(F("Unable to begin:"));
Serial.println(F("1.Please recheck the connection!"));
Serial.println(F("2.Please insert the SD card!"));
}
else {
Serial.println(F("DFPlayer Mini online."));
}
}
void loop() {
AppControl();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void AppControl() {
Bluetooth_Serial.listen(); // Listen to bluetooth, can only listen to one port at a time.
if (Bluetooth_Serial.available() > 0) { //Listen for signal in the software serial port
int data = Bluetooth_Serial.read();
Serial.println(data); // Use hardware serial for printing to serial monitor
if (data == 0) {
digitalWrite(STBY, LOW);
digitalWrite(AIN1, LOW); //Motor A Rotate Clockwise
digitalWrite(AIN2, LOW); //Motor A Rotate Clockwise
}
else if (data == 1) {
digitalWrite(STBY, HIGH);
digitalWrite(AIN1, HIGH); //Motor A Rotate Clockwise
digitalWrite(AIN2, LOW); //Motor A Rotate Clockwise
}
else if (data == 2) {
digitalWrite(STBY, HIGH);
digitalWrite(AIN1, LOW); //Motor A Rotate Clockwise
digitalWrite(AIN2, HIGH); //Motor A Rotate Clockwise
}
else if (data == 10) {
digitalWrite(LEDS, HIGH);
}
else if (data == 11) {
digitalWrite(LEDS, LOW);
}
else if (data == 12) {
digitalWrite(SMOK, HIGH);
}
else if (data == 13) {
digitalWrite(SMOK, LOW);
}
else if (data == 14) {
DFPlayer.volume(15); // Set volume to maximum (0 to 30).
DFPlayer.play(1); // Play the "0001.mp3" in the "mp3" folder on the SD card
}
else if (data == 15) {
DFPlayer.pause();
}
else if (data == 100) {
analogWrite(PWM1, 150);
}
else if (data == 101) {
analogWrite(PWM1, 200);
}
else if (data == 102) {
analogWrite(PWM1, 255);
}
}
}
void ChangeSpeedGradually(int NewSpeed) {
if (NewSpeed == OldSpeed) {
return; //Abort function, do nothing
}
else if (NewSpeed > OldSpeed) {
for ( int i = OldSpeed ; i <= NewSpeed; i++) { //gradual speed increase
analogWrite(PWM1, i); //Speed control of Motor A, range 0 - 255
OldSpeed = i;
delay(50);
Serial.println(i);
}
}
else if (NewSpeed < OldSpeed) {
for ( int i = OldSpeed ; i >= NewSpeed; i--) { //gradual speed increase
analogWrite(PWM1, i); //Speed control of Motor A, range 0 - 255
OldSpeed = i;
delay(50);
Serial.println(i);
}
}
}
I had the same issue with some code of mine. The setting of the volume alone induced a lag of 500ms in my code. I think the serial communication with the dfPlayer takes so long.
You could try to set the volume only once in the setup part and not every time you want to play the sound file (you don't seem to vary the volume, so this would make sense anyway).
Cheers, Flo