Hi guys,
I am a novice programmer and first time using Arduino. I am Using an Uno to control a DFPlayer Mini through the use of tactile buttons to play specific audio files. I can confirm all three buttons work and return the serial message that they are playing their respective file, however only button 3 successfully plays the file. When the other buttons are pressed, the DFPlayer flashes and no audio is played. Can't work out if it's my code telling it to play files that don't exist, or if it's a problem with the DFPlayer. The files are named 0001.mp3, 0002.mp3 and 0003.mp3.
Just want to push button 1 and play track one, push button 2 and play track 2 etc..
Can anyone help?
TIA!
/*
To create audio: https://ttsmaker.com/
DFPlayer module supports up to 3W.
SD card: 2GB ~ 32GB formatted with FAT or FAT32.
MP3 / WAV - audio files.
*/
#include "SoftwareSerial.h" // Include the SoftwareSerial library for serial communication
#include "DFRobotDFPlayerMini.h" // Include the DFRobotDFPlayerMini library for the DFPlayer Mini module
SoftwareSerial mySoftwareSerial(10, 11); // Create a software serial connection on pins 10 (RX) and 11 (TX)
DFRobotDFPlayerMini myDFPlayer; // Create a DFPlayerMini object
const int pushButtonPin1 = 2;
const int pushButtonPin2 = 3;
const int pushButtonPin3 = 4; // Define the pin number for the push button
bool buttonState1;
bool buttonState2;
bool buttonState3; // Variable to store the state of the button
bool wasButton1Pressed = false;
bool wasButton2Pressed = false;
bool wasButton3Pressed = false; // Flag to track whether the button was previously pressed
void setup() {
mySoftwareSerial.begin(9600); // Start software serial communication at 9600 baud rate
Serial.begin(115200); // Start serial communication at 115200 baud rate
pinMode(pushButtonPin1, INPUT_PULLUP); // Set push button pin as input with internal pull-up resistor
pinMode(pushButtonPin2, INPUT_PULLUP);
pinMode(pushButtonPin3, INPUT_PULLUP);
if (!myDFPlayer.begin(mySoftwareSerial)) { // Initialize the DFPlayer Mini module
Serial.println(F("Not initialized:"));
Serial.println(F("1. Check the DFPlayer Mini connections"));
Serial.println(F("2. Insert an SD card"));
while (true); // If initialization fails, print error messages and halt the program
}
Serial.println();
Serial.println(F("DFPlayer Mini module initialized!")); // Print initialization success message
myDFPlayer.setTimeOut(500); // Set the timeout value for serial communication
myDFPlayer.volume(30); // Set the volume level (0 to 30)
myDFPlayer.EQ(0); // Set the equalizer setting (0: Normal, 1: Pop, 2: Rock, 3: Jazz, 4: Classic, 5: Bass)
}
void loop() {
buttonState1 = digitalRead(pushButtonPin1); // Read the state of the button
if (buttonState1 == LOW) { // If the button is pressed (assuming pull-up configuration)
if (!wasButton1Pressed) { // If the button was not previously pressed
Serial.println("Playing song 1");
myDFPlayer.play(1); // Play the first audio file on the SD card
delay(2000); // Delay for 2 seconds
wasButton1Pressed = true; // Set the flag to true to indicate that the button was pressed
}
} else { // If the button is not pressed
Serial.println("Push button not pressed");
wasButton1Pressed = false; // Reset the flag when the button is released
}
buttonState2 = digitalRead(pushButtonPin2); // Read the state of the button
if (buttonState2 == LOW) { // If the button is pressed (assuming pull-up configuration)
if (!wasButton2Pressed) { // If the button was not previously pressed
Serial.println("Playing song 2");
myDFPlayer.play(2); // Play the second audio file on the SD card
delay(2000); // Delay for 2 seconds
wasButton2Pressed = true; // Set the flag to true to indicate that the button was pressed
}
} else { // If the button is not pressed
Serial.println("Push button not pressed");
wasButton2Pressed = false; // Reset the flag when the button is released
}
buttonState3 = digitalRead(pushButtonPin3); // Read the state of the button
if (buttonState3 == LOW) { // If the button is pressed (assuming pull-up configuration)
if (!wasButton3Pressed) { // If the button was not previously pressed
Serial.println("Playing song 3");
myDFPlayer.play(3); // Play the third audio file on the SD card
delay(2000); // Delay for 2 seconds
wasButton3Pressed = true; // Set the flag to true to indicate that the button was pressed
}
} else { // If the button is not pressed
Serial.println("Push button not pressed");
wasButton3Pressed = false; // Reset the flag when the button is released
}
}