Although I do have now a new problem which I will be happy do ask you guys. So I am trying to connect the DFPlayer to the Arduino now and something very weird happens to me. So I connected the DFPlayer and two buttons, one should start to play a random MP3 file (buttonStart), the other should stop/continue the playing (buttonStop).
The problem that I encounter is the following : when I press the start button a random number is generated and all is good. Then, when I press again on the startButton (in order to let a different file play) it plays file number 1. Again. And again. And again. I started to hate this file sooo much ahahahah, can't hear it anymore. When I press the stopButton and just after that on the StartButton a new random number is generated and a random file is played. When I change the creation of the random number from (1,numberFiles) to (2, numberFiles) then a random number is created, file number 2 appeared to be playing more than the others but it might be that I just paranoid. I would appreciate your help so much.
This is the code (without lines which are unnecessary for the question like volume and so) :
#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
SoftwareSerial mySoftwareSerial(10, 11);
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);
int buttonStart = 5 ;
int buttonStop = 4 ;
int stopButtonWillNotStart = 0 ;
int randomNumber ;
void setup()
{
mySoftwareSerial.begin(9600);
Serial.begin(115200);
pinMode (buttonStart, INPUT) ;
digitalWrite (buttonStart,HIGH);
pinMode (buttonStop, INPUT) ;
digitalWrite (buttonStop,HIGH);
}
void loop()
{
int numberFiles = myDFPlayer.readFileCounts() + 1 ; //counts the number of files in DFP
if (!digitalRead (buttonStart)) {
delay (100);
if (!digitalRead (buttonStart)) {
// I placed randomNumber = in different lines and the result is always the same
randomNumber = random (1,numberFiles) ;
stopButtonWillNotStart = 1 ;
myDFPlayer.play(randomNumber);
delay (300);
}
}
if (!digitalRead (buttonStop) && stopButtonWillNotStart == 1) {
delay (100);
if (!digitalRead (buttonStop) && stopButtonWillNotStart == 1) {
myDFPlayer.pause () ;
delay (300) ;
}
}
if (!digitalRead (buttonStop) && stopButtonWillNotStart == 1 ) {
delay (100);
if (!digitalRead (buttonStop) && stopButtonWillNotStart == 1 ) {
myDFPlayer.start () ;
delay (300);
}
}
}
Thank you thank you thank you