But im having no luck, the serial monitor is registering the motion but its not triggering the sounds inless I manually press play but then the sound comes out patchy and broken (Seperate issue but any help is appreciated).
#include "SerialMP3Player.h"
#include <SoftwareSerial.h>
int pirPin = 12; // Arduino pin the PIR sensor is connected to
int RX = 3; // Arduino pin the TX pin of mp3 player is connected to
int TX = 2; // Arduino pin the RX pin of mp3 player is connected to
SerialMP3Player mp3(RX,TX);
int motionStatus = 0; // variable to store the PIR's current reading (high or low)
int pirState = 0; // variable to store the PIR's state change
int track = 3; // variable to store current track number
void setup()
{
pinMode(pirPin, INPUT); // set Arduino pin that PIR is connected to as an INPUT
pinMode(RX, INPUT); // set Arduino pin that mp3 player TX is connected to as an INPUT
pinMode(TX, OUTPUT); // set Arduino pin that mp3 player RX is connected to as an OUTPUT
delay(3000); //wait a bit
Serial.begin(9600); //serial debug
Serial.println("Starting up");
mp3.begin(9600);
mp3.sendCommand(CMD_SEL_DEV, 0, 2);
}
void loop()
{
motionStatus = digitalRead(pirPin); // read the PIR pin's current output (is it HIGH or LOW?)
// if PIR pin output is HIGH:
if (motionStatus == HIGH) {
if (pirState == LOW) {
Serial.println("Motion Detected"); // print result to the serial monitor
pirState = HIGH; // update the previous PIR state to HIGH
musicplay(); // play current track from micro SD card
delay(5000); // length in microseconds of the longest track
track++; // add 1 to the current track for the next loop
// if track number tries to exceed 5 (I have 5 total in my case)
if (track > 5) { // change this value to match your total # of tracks
track = 1; // reset track number to 1
}
}
}
// or else if PIR pin output is LOW:
else {
if (pirState == HIGH) {
Serial.println ("Motion Ended"); //print result to the serial monitor
pirState = LOW; // update the previous PIR state to LOW
}
}
}
void musicplay(){
mp3.play(track);
}
I’m unfamiliar with that library. You might consider using an alternative player: DFRobot’s DFR Player. Get that working first using one of the many basic examples, without PIR etc.
Hello, i have Tried the DFR library examples but none are compiling due to Serial1 not being declared. Has this happened in th epast or a common issue with the Library?
Hello, I am powering it directly through the arduino 5V pin which i have done in the past for another project which works fine with an ultrasonic sensor instead of the PIR
I believe it can power the pir, but the small arduino ( which board? ) regulator can't deliver the current required by the mp3. Try powering the mp3 from a different power source
A I see, its currently running on an Uno like the other project so roughly 500mah though would power be the reason its not triggering with the code? or is that more for the sound because i believe i may have fixed it (it was an export setting that wasnt properly set)
and it is playing manually with the inbuilt play button and no issues with that
Im not sure, I'll have to double check though ive removed both lines and still noting, Serial Monitor is registering the motion and counting normally to trigger each sound but nothing is playing
The code above is the actual code, I'll repost it below but its the same as above.
#include "SerialMP3Player.h"
#include <SoftwareSerial.h>
int pirPin = 12; // Arduino pin the PIR sensor is connected to
int RX = 3; // Arduino pin the TX pin of mp3 player is connected to
int TX = 2; // Arduino pin the RX pin of mp3 player is connected to
SerialMP3Player mp3(RX,TX);
int motionStatus = 0; // variable to store the PIR's current reading (high or low)
int pirState = 0; // variable to store the PIR's state change
int track = 1; // variable to store current track number
void setup()
{
pinMode(pirPin, INPUT); // set Arduino pin that PIR is connected to as an INPUT
delay(3000); //wait a bit
Serial.begin(9600); //serial debug
Serial.println("Starting up");
mp3.begin(9600);
delay(500); // wait for init
mp3.sendCommand(CMD_SEL_DEV, 0, 2);
delay(500); // wait for init
}
void loop()
{
motionStatus = digitalRead(pirPin); // read the PIR pin's current output (is it HIGH or LOW?)
// if PIR pin output is HIGH:
if (motionStatus == HIGH) {
if (pirState == LOW) {
Serial.println("Motion Detected"); // print result to the serial monitor
pirState = HIGH; // update the previous PIR state to HIGH
musicplay(); // play current track from micro SD card
delay(5000); // length in microseconds of the longest track
track++; // add 1 to the current track for the next loop
Serial.println(track);
// if track number tries to exceed 5 (I have 5 total in my case)
if (track > 5) { // change this value to match your total # of tracks
track = 1; // reset track number to 1
Serial.println(track);
}
}
}
// or else if PIR pin output is LOW:
else {
if (pirState == HIGH) {
Serial.println ("Motion Ended"); //print result to the serial monitor
pirState = LOW; // update the previous PIR state to LOW
}
}
}
void musicplay(){
mp3.play(track);
}
I did see this forum and have tried just getting it to work using the provided examples in the IDE which should play the single sound ive got on the sd card but it still doesnt seem to want to work.