I am building a project that works off a motion sensor that will trigger mp3 files on a DF MP3 player. I have everything connected but not sure where to start when it comes to coding. basically I need it to play 1 track when the señsor is triggered then a second later play the second track. the once the sensor is triggered again it will play 3 and a second later play track 4 and then 1 second later play trek 5. like I said It looks like its all connected right but I have no clue how to start the coding. Please help
Are you saying that your MP3 tracks are all less than a second long or that you expect to have 2 or 3 of them playing at the same time?
It always helps to give precise details. There are several DF players and lots of different motion detectors. Which ones are you using?
There are many example projects for DF players usually with buttons to select the track. If you search on the name of the one you're using you'll find them. So that will be a good start. There are also plenty of examples of code which uses a motion detector (usually PIR) to trigger things, usually switching on an LED and/or an alarm buzzer.
Get each of those separate parts working and then it shouldn't be too difficult to put them together. Have a go and post your best attempt here and we can help you get it working.
Steve
ok so I found some code but it doesn't work. the code is meant to turn on LEDs and sound but I figured it would work as long as I have the sound correct. I have a PIR sensor with a DFmp3player from keeyees attached to the Arduino UNO. on the MP3 the RX is connected to pin 11 with 1 k resistor and Tx is in pin 10 with 1 k resistor and the sensor is in pin 12. here is the code:
//Example of using the PIR-sensor, great for Halloween pranks and scares in your yard or porch.
//Watch the video for full walk through and examples:
//part 1. pir-sensor and lights: The PIR-sensor - The best Halloween Arduino Sensor [Anything Arduino] (ep19) - YouTube
//part 2. sound: Simple Arduino MP3 Sound Player [Anything Arduino] (ep20) - YouTube
int pirSensor=12;
int previousPin = 11;
int pausePin = 10;
int statusLed=4;
int led = 2; // the pin that the LED is attached to
void setup(){
pinMode(pirSensor, INPUT);
pinMode(statusLed, OUTPUT);
pinMode(previousPin, OUTPUT);
pinMode(pausePin, OUTPUT);
pinMode(led, OUTPUT);
}
void loop(){
if (digitalRead(pirSensor) == HIGH) {
//Sound!
digitalWrite(pausePin, HIGH);
delay(100);
digitalWrite(previousPin, LOW);
delay(100);
digitalWrite(previousPin,HIGH);
delay(2000);
//Glowing Eyes effect
digitalWrite(statusLed, LOW);
delay(100);
digitalWrite(statusLed, HIGH);
analogWrite(led, 0);
//Here we put whatever code is when Pir-sensor is activated
for (int i=0; i<255; i++) {
Serial.println(i);
analogWrite(led, i);
delay(5);
}
digitalWrite(statusLed, LOW);
delay(100);
digitalWrite(statusLed, HIGH);
delay(100);
digitalWrite(statusLed, LOW);
delay(100);
digitalWrite(statusLed, HIGH);
delay(100);
digitalWrite(statusLed, LOW);
delay(100);
digitalWrite(statusLed, HIGH);
delay(100);
digitalWrite(statusLed, LOW);
delay(2000);
for (int i=255; i>=0; i--) {
Serial.println(i);
analogWrite(led, i);
delay(5);
}
digitalWrite(statusLed, HIGH);
analogWrite(led, 0);
//End glowing eyes effect
//End Sound!
delay(2000);
digitalWrite(previousPin, HIGH);
delay(100);
digitalWrite(pausePin, LOW);
delay(100);
digitalWrite(pausePin, HIGH);
delay(1000);
}
else {
digitalWrite(statusLed, HIGH);
analogWrite(led, 0);
}
}
That code isn't going to help very much because it isn't written for the DFPlayer Mini that you have. That is driven completely differently. But if you connect an LED and 220 Ohm resistor to pin 2 it will at least tell you if the PIR is working.
To get the sound working you need some different code. For some reasonably simple code install library DFPlayerMini_Fast and try the example program that comes with it. As written it only plays track1 but it should give you some ideas of how to run the player (which is not exactly the simplest thing to use).
Steve