#include <SoftwareSerial.h>
#include <DFRobotDFPlayerMini.h>
SoftwareSerial MP3(8, 9);
DFRobotDFPlayerMini player;
const int buttonPin1 = A2;
const int buttonPin2 = A1;
const int buttonPin3 = A0;
const int selPin1 = A3;
const int selPin2 = A4;
const int selPin3 = A5;
const int potpin = A7;
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int selState1 = 0;
int selState2 = 0;
int selState3 = 0;
int count1 = 0;
int count2 = 0;
int count3 = 0;
int vol = 0;
void setup()
{
pinMode(potpin, INPUT);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(selPin1, INPUT);
pinMode(selPin2, INPUT);
pinMode(selPin3, INPUT);
Serial.begin(9600);
MP3.begin(9600);
if (player.begin(MP3))
{
Serial.println("OK");
player.volume(40);
}
else
{
Serial.println("Connecting to DFPlayer Mini failed!");
}
}
void volum()
{
vol = analogRead(potpin);
vol = map(vol, 0, 1023, 0, 50);
player.volume(vol);
}
void loop()
{
selState1 = digitalRead(selPin1);
selState2 = digitalRead(selPin2);
selState3 = digitalRead(selPin3);
delay(10);
if (selState1 == LOW && selState2 == HIGH && selState3 == HIGH)
{
sel1loop();
}
else if (selState1 == HIGH && selState2 == LOW && selState3 == HIGH)
{
sel2loop();
}
else if (selState1 == HIGH && selState2 == HIGH && selState3 == LOW)
{
sel3loop();
}
}
void sel1loop()
{
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
delay(100);
if (buttonState1 == HIGH && buttonState2 == LOW && buttonState3 == LOW && count1 == 0)
{
count1 = 1;
volum();
player.play(1);
delay(100);
}
else if (buttonState1 == LOW && buttonState2 == HIGH && buttonState3 == LOW && count1 == 0)
{
count1 = 1;
volum();
player.play(2);
delay(100);
}
else if (buttonState1 == LOW && buttonState2 == LOW && buttonState3 == HIGH && count1 == 0)
{
count1 = 1;
volum();
player.play(3);
delay(100);
}
else if (buttonState1 == HIGH && buttonState2 == LOW && buttonState3 == LOW && count1 == 1)
{
delay(100);
}
else if (buttonState1 == LOW && buttonState2 == LOW && buttonState3 == LOW && count1 == 1)
{
count1 = 0;
player.stop();
delay(100);
}
}
void sel2loop()
{
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
delay(100);
if (buttonState1 == HIGH && buttonState2 == LOW && buttonState3 == LOW && count2 == 0)
{
count2 = 1;
volum();
player.play(4);
delay(100);
}
else if (buttonState1 == LOW && buttonState2 == HIGH && buttonState3 == LOW && count2 == 0)
{
count2 = 1;
volum();
player.play(5);
delay(100);
}
else if (buttonState1 == LOW && buttonState2 == LOW && buttonState3 == HIGH && count2 == 0)
{
count2 = 1;
volum();
player.play(6);
delay(100);
}
else if (buttonState1 == HIGH && buttonState2 == LOW && buttonState3 == LOW && count2 == 1)
{
delay(100);
}
else if (buttonState1 == LOW && buttonState2 == LOW && buttonState3 == LOW && count2 == 1)
{
count2 = 0;
player.stop();
delay(100);
}
}
void sel3loop()
{
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
delay(100);
if (buttonState1 == HIGH && buttonState2 == LOW && buttonState3 == LOW && count3 == 0)
{
count3 = 1;
volum();
player.play(7);
delay(100);
}
else if (buttonState1 == LOW && buttonState2 == HIGH && buttonState3 == LOW && count3 == 0)
{
count3 = 1;
volum();
player.play(8);
delay(100);
}
else if (buttonState1 == LOW && buttonState2 == LOW && buttonState3 == HIGH && count3 == 0)
{
count3 = 1;
volum();
player.play(9);
delay(100);
}
else if (buttonState1 == HIGH && buttonState2 == LOW && buttonState3 == LOW && count3 == 1)
{
delay(100);
}
else if (buttonState1 == LOW && buttonState2 == LOW && buttonState3 == LOW && count3 == 1)
{
count3 = 0;
player.stop();
delay(100);
}
}
Hardware part is properly soldered but the mp3 player fails to connect with the nano every time, can anyone help. I have tried changing the RX and TX pin also. Is there a test code to check the mp3 player ? Also I shorted the IO_1 and IO_2 pins of the mp3 player and the sound got played!
