Thanks! @blh64
I've changed, what you said. I've applied change detection from an example and it works, reads every button state change.
However the main problem is still an issue, play command doesn't work either on the old reading and the new one. I've checked DFplayer state after play command by
Serial.println(myDFPlayer.readState());
and gives me -1, as below on Serial Monitor:
DFPlayer Mini online.
-1
**on**
**-1**
off
Any idea, what is going on?
new code below:
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
const int buttonPin = 2;
int buttonState = 0;
int lastButtonState = 0;
SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);
void setup() {
pinMode(buttonPin, INPUT); //Define each button as input with pullup
pinMode(4, OUTPUT);
mySoftwareSerial.begin(9600);
Serial.begin(9600);
if (!myDFPlayer.begin(mySoftwareSerial, false)) { //Use softwareSerial to communicate with mp3.
Serial.println(F("Unable to begin:"));
Serial.println(F("1.Please recheck the connection!"));
Serial.println(F("2.Please insert the SD card!"));
while (true);
}
Serial.println(F("DFPlayer Mini online."));
myDFPlayer.volume(30);
Serial.println(myDFPlayer.readVolume());
myDFPlayer.play(1); //Play the first mp3 ------------this doesn't work :(
}
void loop() {
//This still doesn't work
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
Serial.println("on");
myDFPlayer.play(1);
Serial.println(myDFPlayer.readState());
digitalWrite(4, HIGH);
} else {
Serial.println("off");
digitalWrite(4, LOW);
}
// Delay a little bit to avoid bouncing
delay(50);
}
lastButtonState = buttonState;
// This works fine -plays all mp3 every 3 seconds
/*
static unsigned long timer = millis();
if (millis() - timer > 3000) {
timer = millis();
myDFPlayer.next(); //Play next mp3 every 3 second.
}
if (myDFPlayer.available()) {
printDetail(myDFPlayer.readType(), myDFPlayer.read()); //Print the detail message from DFPlayer to handle different errors and states.
}
*/
}