Problems merging 2 codes (blinking light and dfplayer)

I have copied the wrong sketch in - i did have the "merged" one... will upload later. Thaks :slight_smile:

This is my best shot so far.
The serial monitor says DFPlayer mini online, but the only thing that happends is that the LED13 keeps light up when something is in the distance.

const byte led1 = 13;                 // LED pin NORMAL PIN2 BUT 13 FOR TEST
const byte trigger= 6;              // PIR sensor pin SENSOR REPLACED BY TRIGGER
bool isActivated = false;

#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
#include <PCM.h>                    //ADDED FROM DISTANCE CODE

#define MIN_DISTANCE 30             //ADDED FROM DISTANCE CODE
int echo = 5; // Pin to echo        //ADDED FROM DISTANCE CODE                         
int trigger = 6; // Pin for trigger //ADDED FROM DISTANCE CODE
int led1 = 13; // led onboard for test  //ADDED FROM DISTANCE CODE
unsigned long responsetime;             //ADDED FROM DISTANCE CODE
unsigned long distance;                 //ADDED FROM DISTANCE CODE

SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;

const unsigned long pauseTime = 6000;   // how long between PIR detections
const unsigned long blinkTime = 100;    // how fast to blink led

unsigned long startTime; // time PIR triggered
unsigned long lastTime; // last time we toggled led

void setup() {
  delay (5000);             //DELAY TIME FOR DEVICE TO START UP TO AVOID ERRORS
  pinMode(led1, OUTPUT);      // set led pin as output
  pinMode(trigger, INPUT);    // set sensor pin as input  SENSOR REPLACED BY TRIGGER

  pinMode (trigger, OUTPUT);  //ADDED FROM DISTANCE CODE                   
  pinMode (echo, INPUT);      //ADDED FROM DISTANCE CODE
  pinMode (led1, OUTPUT);     //ADDED FROM DISTANCE CODE

  mySoftwareSerial.begin(9600);
  Serial.begin(115200);

  Serial.println(F("\nInitializing DFPlayer..."));

  //Use softwareSerial to communicate with MP3
  if (!myDFPlayer.begin(mySoftwareSerial)) {
    Serial.println(F("Unable to begin:"));
    Serial.println(F("1.Please recheck the connection!"));
    Serial.println(F("2.Please insert the SD card!"));
    while (true);
  }
  Serial.println(F("DFPlayer Mini online."));

  //Set volume value (From 0 to 30)
  myDFPlayer.volume(30);
}

void loop() {

  digitalWrite (trigger, HIGH); // We send a 10 microsecond pulse           //ADDED FROM DISTANCE CODE
  delayMicroseconds (10);                                                   //ADDED FROM DISTANCE CODE                     
  digitalWrite (trigger, LOW);                                              //ADDED FROM DISTANCE CODE                
  responsetime = pulseIn (echo, HIGH); // And we wait for a pulse back      //ADDED FROM DISTANCE CODE
  distance = responsetime / 58; // Distance calculation in cm               //ADDED FROM DISTANCE CODE

  if (distance <MIN_DISTANCE) {
    digitalWrite (led1, HIGH);        
    delay (35);
    digitalWrite (led1, LOW);
  
  if ( isActivated ) {
    // blink led
    if ( millis() - lastTime >= blinkTime ) {
      lastTime = millis();
      digitalWrite(led1, !digitalRead(led1));
    }

    // check if we have been active long enough
    if ( millis() - startTime >= pauseTime ) {
      isActivated = false; // force PIR to trigger again or not
    }
  }

  if ( isActivated == false ) {
    // check PIR
    int pirSensor = digitalRead(sensor);
    if (pirSensor == HIGH) {
      isActivated = true;
      startTime = millis();
      Serial.println("Sensor Activated");
      Serial.println("DFPlayer Working...");
      myDFPlayer.play(1);
    }
  }
}
}

Get rid of the PIR. You can get the compiler to help you with this again. Delete this line:

const byte sensor = 6;              // PIR sensor pin SENSOR REPLACED BY TRIGGER

Compile and fix the resulting errors by deleting the PIR related stuff it finds.

Then take this if and it's body and move it so it executes when distance is less than the desired minimum:

if ( isActivated == false ) {

A

If I delete that line

const byte sensor = 6;

(I actual renamed it to const byte trigger = 6) and renamed all stuff in the code to trigger instead of sensor.
Then it says this:
Conflicting declaration "int led1" from this line:

int led1 = 13; // led onboard for test  //ADDED FROM DISTANCE CODE

And thank you for your time!

It's nothing to do with the line you removed - the same problem exists in your previous version.

The compiler is essentially saying that you have two declarations of something called led1 and if you run your eye over the first few lines of your program, you'll see that you do.

Thank you sir.

Now it looks like this.

const byte led1 = 13;                 // LED pin NORMAL PIN2 BUT 13 FOR TEST
const byte trigger= 6;              // PIR sensor pin SENSOR REPLACED BY TRIGGER
const byte echo = 5;                // Pin to echo        //ADDED FROM DISTANCE CODE   
bool isActivated = false;

#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
#include <PCM.h>                    //ADDED FROM DISTANCE CODE

#define MIN_DISTANCE 30             //ADDED FROM DISTANCE CODE
                         


unsigned long responsetime;             //ADDED FROM DISTANCE CODE
unsigned long distance;                 //ADDED FROM DISTANCE CODE

SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;

const unsigned long pauseTime = 6000;   // how long between PIR detections
const unsigned long blinkTime = 100;    // how fast to blink led

unsigned long startTime; // time PIR triggered
unsigned long lastTime; // last time we toggled led

void setup() {
  delay (3000);             //DELAY TIME FOR DEVICE TO START UP TO AVOID ERRORS
  pinMode(led1, OUTPUT);      // set led pin as output
  pinMode(trigger, INPUT);    // set sensor pin as input  SENSOR REPLACED BY TRIGGER

  pinMode (trigger, OUTPUT);  //ADDED FROM DISTANCE CODE                   
  pinMode (echo, INPUT);      //ADDED FROM DISTANCE CODE
  pinMode (led1, OUTPUT);     //ADDED FROM DISTANCE CODE

  mySoftwareSerial.begin(9600);
  Serial.begin(115200);

  Serial.println(F("\nInitializing DFPlayer..."));

  //Use softwareSerial to communicate with MP3
  if (!myDFPlayer.begin(mySoftwareSerial)) {
    Serial.println(F("Unable to begin:"));
    Serial.println(F("1.Please recheck the connection!"));
    Serial.println(F("2.Please insert the SD card!"));
    while (true);
  }
  Serial.println(F("DFPlayer Mini online."));

  //Set volume value (From 0 to 30)
  myDFPlayer.volume(2);
}

void loop() {

  digitalWrite (trigger, HIGH); // We send a 10 microsecond pulse           //ADDED FROM DISTANCE CODE
  delayMicroseconds (10);                                                   //ADDED FROM DISTANCE CODE                     
  digitalWrite (trigger, LOW);                                              //ADDED FROM DISTANCE CODE                
  responsetime = pulseIn (echo, HIGH); // And we wait for a pulse back      //ADDED FROM DISTANCE CODE
  distance = responsetime / 58; // Distance calculation in cm               //ADDED FROM DISTANCE CODE

  if (distance <MIN_DISTANCE) {
    digitalWrite (led1, HIGH);        
    delay (35);
    digitalWrite (led1, LOW);
  
  if ( isActivated ) {
    // blink led
    if ( millis() - lastTime >= blinkTime ) {
      lastTime = millis();
      digitalWrite(led1, !digitalRead(led1));
    }

    // check if we have been active long enough
    if ( millis() - startTime >= pauseTime ) {
      isActivated = false; // force PIR to trigger again or not
    }
  }

  if ( isActivated == false ) {
    // check PIR
    int pirSensor = digitalRead(trigger);
    if (pirSensor == HIGH) {
      isActivated = true;
      startTime = millis();
      Serial.println("Sensor Activated");
      Serial.println("DFPlayer Working...");
      myDFPlayer.play(1);
    }
  }
}
}

The LED lights up when hold my hand in front of sensor. I know (proberly) it have something to do with this, as delay block from anything else?

  if (distance <MIN_DISTANCE) {
    digitalWrite (led1, HIGH);        
    delay (35);
    digitalWrite (led1, LOW);

You're still checking the PIR.

But if I remove the two lines, then the led keeps beeing on and the player keeps repeating after I moved my hand over sensor.
I guesss that there need to be something to activate the blinking led and mp3 instead of the pir lines?
Appoligies for that im hard to explain - but I feel your help is getting me to understand better :slight_smile:

void loop() {

  digitalWrite (trigger, HIGH); // We send a 10 microsecond pulse           //ADDED FROM DISTANCE CODE
  delayMicroseconds (10);                                                   //ADDED FROM DISTANCE CODE                     
  digitalWrite (trigger, LOW);                                              //ADDED FROM DISTANCE CODE                
  responsetime = pulseIn (echo, HIGH); // And we wait for a pulse back      //ADDED FROM DISTANCE CODE
  distance = responsetime / 58; // Distance calculation in cm               //ADDED FROM DISTANCE CODE

  if (distance <MIN_DISTANCE) {
    digitalWrite (led1, HIGH);        
    delay (35);
    digitalWrite (led1, LOW);
  
  if ( isActivated ) {
    // blink led
    if ( millis() - lastTime >= blinkTime ) {
      lastTime = millis();
      digitalWrite(led1, !digitalRead(led1));
    }

    // check if we have been active long enough
    if ( millis() - startTime >= pauseTime ) {
      isActivated = false; // force PIR to trigger again or not
    }
  }

  if ( isActivated == false ) {
    // check PIR

      isActivated = true;
      startTime = millis();
      Serial.println("Sensor Activated");
      Serial.println("DFPlayer Working...");
      myDFPlayer.play(1);
    }
  }
}

I don't see why, please post your latest code (all of it).

I suggest you move the if ( isActivated ) stuff to the bottom of loop so it isn't controlled by distance but runs unconditionally. Also, leave the led flashing for the code that uses millis.

The complete code without pir stuff:

const byte led1 = 13;                 // LED pin NORMAL PIN2 BUT 13 FOR TEST
const byte trigger= 6;              // PIR sensor pin SENSOR REPLACED BY TRIGGER
const byte echo = 5;                // Pin to echo        //ADDED FROM DISTANCE CODE   
bool isActivated = false;

#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
#include <PCM.h>                    //ADDED FROM DISTANCE CODE

#define MIN_DISTANCE 30             //ADDED FROM DISTANCE CODE
                         


unsigned long responsetime;             //ADDED FROM DISTANCE CODE
unsigned long distance;                 //ADDED FROM DISTANCE CODE

SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;

const unsigned long pauseTime = 6000;   // how long between PIR detections
const unsigned long blinkTime = 100;    // how fast to blink led

unsigned long startTime; // time PIR triggered
unsigned long lastTime; // last time we toggled led

void setup() {
  delay (3000);             //DELAY TIME FOR DEVICE TO START UP TO AVOID ERRORS
  pinMode(led1, OUTPUT);      // set led pin as output
  pinMode(trigger, INPUT);    // set sensor pin as input  SENSOR REPLACED BY TRIGGER

  pinMode (trigger, OUTPUT);  //ADDED FROM DISTANCE CODE                   
  pinMode (echo, INPUT);      //ADDED FROM DISTANCE CODE
  pinMode (led1, OUTPUT);     //ADDED FROM DISTANCE CODE

  mySoftwareSerial.begin(9600);
  Serial.begin(115200);

  Serial.println(F("\nInitializing DFPlayer..."));

  //Use softwareSerial to communicate with MP3
  if (!myDFPlayer.begin(mySoftwareSerial)) {
    Serial.println(F("Unable to begin:"));
    Serial.println(F("1.Please recheck the connection!"));
    Serial.println(F("2.Please insert the SD card!"));
    while (true);
  }
  Serial.println(F("DFPlayer Mini online."));

  //Set volume value (From 0 to 30)
  myDFPlayer.volume(3);
}

void loop() {

  digitalWrite (trigger, HIGH); // We send a 10 microsecond pulse           //ADDED FROM DISTANCE CODE
  delayMicroseconds (10);                                                   //ADDED FROM DISTANCE CODE                     
  digitalWrite (trigger, LOW);                                              //ADDED FROM DISTANCE CODE                
  responsetime = pulseIn (echo, HIGH); // And we wait for a pulse back      //ADDED FROM DISTANCE CODE
  distance = responsetime / 58; // Distance calculation in cm               //ADDED FROM DISTANCE CODE

  if (distance <MIN_DISTANCE) {
    digitalWrite (led1, HIGH);        
    delay (35);
    digitalWrite (led1, LOW);
  
  if ( isActivated ) {
    // blink led
    if ( millis() - lastTime >= blinkTime ) {
      lastTime = millis();
      digitalWrite(led1, !digitalRead(led1));
    }

    // check if we have been active long enough
    if ( millis() - startTime >= pauseTime ) {
      isActivated = false; // force PIR to trigger again or not
    }
  }

  if ( isActivated == false ) {
    // check PIR
                              // removed this from original code: int pirSensor = digitalRead(trigger);
                              //                                   if (pirSensor == HIGH) {
      isActivated = true;
      startTime = millis();
      Serial.println("Sensor Activated");
      Serial.println("DFPlayer Working...");
      myDFPlayer.play(1);
    }
  }
}
}

Maybe there should be some kind of "distance/trigger" under the "check pir", as that is what activates the dfplayer? Or is it because of the "delay" under "if (distance <MIN DISTANCE.."stuff, that blocks it from going on to blink led/play?

You need to record your startTime when the distance sensors first detects something which is when it is less than MIN_DISTANCE and not current active.

const byte led1 = 13;                 // LED pin NORMAL PIN2 BUT 13 FOR TEST
const byte trigger = 6;             // PIR sensor pin SENSOR REPLACED BY TRIGGER
const byte echo = 5;                // Pin to echo        //ADDED FROM DISTANCE CODE
bool isActivated = false;

#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
#include <PCM.h>                    //ADDED FROM DISTANCE CODE

#define MIN_DISTANCE 30             //ADDED FROM DISTANCE CODE



unsigned long responsetime;             //ADDED FROM DISTANCE CODE
unsigned long distance;                 //ADDED FROM DISTANCE CODE

SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;

const unsigned long pauseTime = 6000;   // how long between PIR detections
const unsigned long blinkTime = 100;    // how fast to blink led

unsigned long startTime; // time PIR triggered
unsigned long lastTime; // last time we toggled led

void setup() {
  delay (3000);             //DELAY TIME FOR DEVICE TO START UP TO AVOID ERRORS
  pinMode(led1, OUTPUT);      // set led pin as output
  pinMode(trigger, INPUT);    // set sensor pin as input  SENSOR REPLACED BY TRIGGER

  pinMode (trigger, OUTPUT);  //ADDED FROM DISTANCE CODE
  pinMode (echo, INPUT);      //ADDED FROM DISTANCE CODE
  pinMode (led1, OUTPUT);     //ADDED FROM DISTANCE CODE

  mySoftwareSerial.begin(9600);
  Serial.begin(115200);

  Serial.println(F("\nInitializing DFPlayer..."));

  //Use softwareSerial to communicate with MP3
  if (!myDFPlayer.begin(mySoftwareSerial)) {
    Serial.println(F("Unable to begin:"));
    Serial.println(F("1.Please recheck the connection!"));
    Serial.println(F("2.Please insert the SD card!"));
    while (true);
  }
  Serial.println(F("DFPlayer Mini online."));

  //Set volume value (From 0 to 30)
  myDFPlayer.volume(3);
}

void loop() {

  digitalWrite (trigger, HIGH); // We send a 10 microsecond pulse           //ADDED FROM DISTANCE CODE
  delayMicroseconds (10);                                                   //ADDED FROM DISTANCE CODE
  digitalWrite (trigger, LOW);                                              //ADDED FROM DISTANCE CODE
  responsetime = pulseIn (echo, HIGH); // And we wait for a pulse back      //ADDED FROM DISTANCE CODE
  distance = responsetime / 58; // Distance calculation in cm               //ADDED FROM DISTANCE CODE

  if (distance < MIN_DISTANCE  && isActivated == false) {
    // first detection of something
    isActivated = true;
    startTime = millis();
    Serial.println("Sensor Activated");
    Serial.println("DFPlayer Working...");
    myDFPlayer.play(1);
    digitalWrite (led1, HIGH);
  }

  if ( isActivated ) {
    // blink led
    if ( millis() - lastTime >= blinkTime ) {
      lastTime = millis();
      digitalWrite(led1, !digitalRead(led1));
    }

    // check if we have been active long enough
    if ( millis() - startTime >= pauseTime ) {
      isActivated = false; // force PIR to trigger again or not
    }
  }
}

Thanks man! Now the code actual does blink LED, and play the 6 second mp3 file by motion - BUT!
It plays it randoms times. When power on, it waits until movement, but after motion it plays maybe 4 times, 3 times, 8 times, and sometimes LED stays on after it have played.
Dont know where to look after that failue?

I see this trigger is both input and output? (doesnt change anything if im deleting one of them anyways)

  pinMode(trigger, INPUT);    // set sensor pin as input  SENSOR REPLACED BY TRIGGER

  pinMode (trigger, OUTPUT);  //ADDED FROM DISTANCE CODE

The second pinMode() call is what actually gets used. If it is triggering, you can always print out a message saying it is triggered, etc.

So I should delete the first pinmode?
But still Can’t find the logic/reason in that it plays so random when motion detected :frowning:

I found out of that, if I set the

const unsigned long pauseTime = 6000;   // how long between PIR detections

to 7000 instead of 6000, then it actual works. The LED then blinks for 1 second more than the mp3 plays(the mp3 file is 6 seconds long). So it must be something to do with it sees that the dfplayer isnt finished, maybe due to some milliseconds delay to power the dfplayer off, and then it continues in a wierd loop.
But I think I can use it with 7000 instead.

You can read the state of the DF Player to see if the song is done or not. Look at the read example that comes with the library.