Hi,
After a couple of years I decided to finally build a little Arduino project with the parts I collected
This little project is build with the following:
- Arduino Nano
- DFPlayes Mini Mp3 Player
- Mini Pir
- Speaker
I conected everything toghether and programmed the Uno. The sound is playing after the PIR activates. So far so good...
My problem is that the PIR can be activated again while the sound is still playing so the MP3 sound starts over.
I cannot find a solution for this problem and hopes someone can give me the tip what I should change in the attached code.
What it must do is playing a MP3 after PIR detection and the PIR can only activated again 10 seconds after the MP3 is ready playing.
Thanks in advance
John
//
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
#define PIN_BUSY 3
SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
int PIRmotion = 12; // Input pin for PIR sensor
int PIRstate = LOW; // Start situation PIR, 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(10);
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;
}
}
}