I cant get it to work as intended. It almost always fails to play sound second time in second condition block. arduino gets there (controlled with serial responses), command get sent, but player do not play sound. And if I send Loop command, then whatever file number I send is ignored and DFPlayer loops last played file.
Maybe Im blind and there is an error in my code, but sometimes it plays sound second time as it should.
I have 1KOhm resistor in Tx-Rx line.
Faulty module?
#include <DFRobotDFPlayerMini.h>
#include <SoftwareSerial.h>
SoftwareSerial ss(4,5);
DFRobotDFPlayerMini mp3;
#define SWITCH_PIN 10
float soundTimer=0;
long startMillis=0;
int playingSound = 0;
void setup() {
pinMode(SWITCH_PIN,INPUT_PULLUP);
Serial.begin(9600);
ss.begin(9600);
while(!mp3.begin(ss)){
}
mp3.volume(30);
delay(200);
}
void loop() {
if(digitalRead(SWITCH_PIN) == LOW && playingSound == 0)
{
mp3.play(1);
delay(200);
playingSound = 1;
soundTimer = 1626;
startMillis = millis();
}
if(playingSound == 1 && millis()-startMillis > soundTimer)
{
mp3.play(1);
delay(200);
playingSound = 2;
soundTimer = 1626;//1826
startMillis = millis();
}
}