I've build the device that is playing the single random mp3 file from the SD card on button push, using Arduino and DFPlayer mini. The device is working almost fine. The problem is that when I push the button and do not let go instantly, the button becomes irresponsive (it stops to initiate the next random file) until I reset the Arduino. Can you please help me fix this bug?
The code:
#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
const int BUTTON_PIN = 4;
int buttonState = 0;
SoftwareSerial mySoftwareSerial(3, 2); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void setup()
{
mySoftwareSerial.begin(9600);
Serial.begin(115200);
pinMode(BUTTON_PIN, INPUT_PULLUP);
if (!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3.
while(true){
delay(0); // Code to compatible with ESP8266 watch dog.
}
}
}
void loop()
{
byte buttonState = digitalRead(BUTTON_PIN);
if (buttonState == LOW){
myDFPlayer.volume(20); //Set volume value. From 0 to 30
myDFPlayer.play(random(1, 12));
}
delay(100);
}
I used the code but do not know why it stops working if I push the button while the sound is playing.