the DFplayer itself works a charm with the template "GetStarted" code from DFrobot (attached), meaning I hear the mp3's I uploaded and it cycles through all of them continuously with ease, but as soon as I add the PIR sensor and the relevant code: nothing seems to happen.
(The PIR sensor itself works fine as I have just tested it with another sketch for another project)
Here is my code:
include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
#define PIN_BUSY 3
SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
int motion = 2; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
int redLedPin = 2; // future use to light up led eyes
void setup () {
Serial.begin (115200);
Serial.println("Setup");
pinMode(PIN_BUSY, INPUT);
pinMode(motion, INPUT); // declare sensor as input
Serial.println("Setting up software serial");
mySoftwareSerial.begin(9600);
Serial.println("check dfplayer");
if (!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3.
Serial.println(F("DFPlayer error."));
//while (true);
}
Serial.println("Setting up software serial —- done");
//—-Mp3 play—-
Serial.println("Setup mp3 player settings");
myDFPlayer.volume(40);
myDFPlayer.setTimeOut(500); //Set serial communictaion time out 500ms
// test mp3
//playMp3(); Serial.println(“test play done”);
}
void loop () {
checkMotion();
}
void playMp3() {
Serial.println("—– playMp3 —–");
// play
myDFPlayer.play(0001);
delay(60000); // delay 1 minute
Serial.println("play done");
}
void checkMotion() {
val = digitalRead(motion); // read input value
if (val == HIGH) { // check if the input is HIGH
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected!");
// We only want to print on the output change, not state
pirState = HIGH;
myDFPlayer.play(0001);
delay(500);
}
} else {
if (pirState == HIGH) {
// we have just turned of
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
digitalWrite(redLedPin, LOW);
}
}
}
I have tried the code above, along with basically every other similar code that links a PIR to the DFplayer available on google, tried "frankensteining" parts of those codes together, but I really can't seem to get this to work, even though the PIR is clearly detecting movement.
What I don't understand is that all the code examples / tutorials / blog posts come from people who have clearly made it work, am I missing something ridiculously simple to get this up and running?
I hope this is the right way to formulate my question and ask for help again I'm very new here.
int motion = 2; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
int redLedPin = 2; // future use to light up led eyes
It's good that State and Pin appear in some names of variables containing pin numbers and pin states. It would be better if they appeared in ALL names of variables containing pin numbers and pin states.
but the PIR still doesn't seem to trigger any new audio. I'm wondering about the BUSY pin of the DFplayer (established as pin 3 in this sketch), but I have never seen any diagrams where it is connected to arduino. When I connect it it instantly plays the sound and loops it until I remove it.
Could the BUSY pin (which acts like a trigger) be coded to respond to the PIR being HIGH or LOW?
@WildBill I switched the PIR to pin 12 and now everything works!! I don't know why I didn't try that before, but thank you!
Here is the code that works now, just need to figure out how to get back to the looping "idle" audio once the PIR trigger ends. Part of the fun
//
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
#define PIN_BUSY 3
SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
int PIRmotion = 12; // choose the input pin (for PIR sensor)
int PIRstate = LOW; // we start, assuming no motion detected
int PIRval = 0; // variable for reading the pin status
void setup () {
Serial.begin (115200);
Serial.println("Setup");
pinMode(PIN_BUSY, INPUT);
pinMode(PIRmotion, INPUT); // declare sensor as input
Serial.println("Setting up software serial");
mySoftwareSerial.begin(9600);
Serial.println("check dfplayer");
if (!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3.
Serial.println(F("DFPlayer error."));
//while (true);
}
Serial.println("Setting up software serial —- done");
//—-Mp3 play—-
Serial.println("Setup mp3 player settings");
myDFPlayer.volume(20);
myDFPlayer.setTimeOut(500); //Set serial communictaion time out 500ms
// test mp3
playMp3(); Serial.println("test play done");
}
void loop () {
checkMotion();
}
void playMp3() {
Serial.println("—– playMp3 —–");
// play
myDFPlayer.loop(0001);
delay(500);
Serial.println("play done");
}
void checkMotion() {
PIRval = digitalRead(PIRmotion); // read input value
if (PIRval == HIGH) { // check if the input is HIGH
if (PIRstate == LOW) { // we have just turned on
Serial.println("Motion detected!");
// We only want to print on the output change, not state
PIRstate = HIGH;
Serial.println("—– playMp3 —–");
myDFPlayer.play(0002);
delay(500);
}
} else {
if (PIRstate == HIGH) {
// we have just turned off
Serial.println("Motion ended!");
// We only want to print on the output change, not state
PIRstate = LOW;
}
}
}
One quick question I had is if I can reassign pin 10 + 11 (RX - TX) to other pins on an Arduino UNO. I'm planning on adding a megamoto plus shield to drive a 12v Linear actuator and might need pins 10+11 for that.
Again, very grateful for your help solving this PIR issue!