Hello,
I'm trying to create an installation that, when triggered by a PIR sensor, plays a specific track. When it's not triggered, it loops another track.
The problem I am running into is that it seemingly cannot play the second track. Any advice?
- I am using an Arduino UNO and a DF Player
- The mp3 files are formatted as 0001.mp3 and 0002.mp3 in a folder titled "mp3"
- It currently successfully loops track 1 but will not play track 2 when triggered
- I have confirmed that the PIR sensor is working
- I have successfully gotten it to trigger audio, but not the track I want. If it triggers audio, it won't loop the audio when no motion is detected.
#include <DFRobotDFPlayerMini.h>
#include <SoftwareSerial.h>
int pirPin = 8;
int motionStatus = 0;
int pirState = 0;
int rxPin = 10;
int txPin = 11;
SoftwareSerial fxSerial (rxPin, txPin);
DFRobotDFPlayerMini fxPlayer;
void setup() {
pinMode(pirPin, INPUT);
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
Serial.begin(9600);
fxSerial.begin(9600);
fxPlayer.begin(fxSerial);
fxPlayer.volume(30);
delay(3000);
}
void loop () {
motionStatus = digitalRead(pirPin);
if(motionStatus == HIGH) {
if(pirState == LOW) {
Serial.println("Motion detected");
pirState = HIGH;
fxPlayer.play(2);
delay(5000);
}
}
else {
if(pirState == HIGH) {
Serial.println("Motion ended");
pirState = LOW;
fxPlayer.loop(1);
}
}
}