Missing one line to make relay work!

Hi Guys. Now im in need of help again...im have looked totally blind to this now.
The other day it did make it work, but it didnt save my work, and now it doesnt work.

My code does this: When dist.sensor is activated it waits 5 secs. and then it should activate a relay (low trigger), a strobe LED, and dfplayer.
It all works except for the relay. It only turns on for 1 second then the rest of the code continues.
My logic tells me to put relaypin LOW after the 5000 delay, but it only gives it 1 sec. activated.
The relay should stop just before the led stays on for 3000 at the end of code.

I know its not the best code to combine without delay and delays, but it worked the other day.
Can you guide me?

const byte led13 = 13;               //Internal LED to how when a motion is detected and code is active
const byte led1 = 2;                 // strobe-LED pin PIN2
const byte trigger = 6;             // TRIGGER pin from distance sensor
const byte echo = 5;                // Pin to echo    
const byte RELAY_PIN = 3;  // the Arduino pin, which connects to the IN pin of relay

bool isActivated = false;


#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
                                         

#define MIN_DISTANCE 80             //Set distance in cm that sensor reacts to

unsigned long responsetime;             
unsigned long distance;                 

SoftwareSerial mySoftwareSerial(10, 11); // RX, TX pins from dfplayer mini
DFRobotDFPlayerMini myDFPlayer;

const unsigned long pauseTime = 8000;   // how long between PIR detections, and how long the scene should play
const unsigned long blinkTime = 75;    // how fast to blink strobe-led

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

void setup() {
  digitalWrite(RELAY_PIN, HIGH);      //Deactivates relay so its not staying activated
  delay (5000);             //DELAY TIME FOR DEVICE TO START UP TO AVOID ERRORS
  pinMode(led1, OUTPUT); 
   pinMode(led13, OUTPUT);      
  pinMode(trigger, INPUT); 
     
  pinMode(RELAY_PIN, OUTPUT);
  
  pinMode (trigger, OUTPUT);  
  pinMode (echo, INPUT);      
  pinMode (led1, OUTPUT);     


  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 (RELAY_PIN, HIGH);	//Deactivates relay at startup
  digitalWrite (trigger, HIGH); // We send a 10 microsecond pulse           
  delayMicroseconds (10);                                                   
  digitalWrite (trigger, LOW);                                              
  responsetime = pulseIn (echo, HIGH); // And we wait for a pulse back      
  distance = responsetime / 58; // Distance calculation in cm               





  if (distance < MIN_DISTANCE  && isActivated == false) {
    // first detection of something
    isActivated = true;                                                     //when af motion is detected
digitalWrite (led13, HIGH);
delay(5000);				//waits 5 seconds before action

digitalWrite(RELAY_PIN, LOW);   //activates relay
 
    
    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 dist sensor to trigger again or not
    digitalWrite(RELAY_PIN, HIGH);						//Deactivates relay
    digitalWrite(led1, HIGH);                                          //Keeps LED on led for some seconds
    delay(3000);                                                      //delays where the led is on                                       
    digitalWrite(led1, LOW);                                          //Turns the LED off to make shure it doesnt stays on until next reading



   delay(20000);                                                       //delay before it can start loop over again
   digitalWrite (led13, LOW);                               //turns off led13 to show code has ended
    }
  }
}```

At the start of your loop() routine, add this line:

Serial.println("Turning relay off");

Upload the sketch and you should see exactly what's turning your relay off in the serial monitor.

Thanks. But it then just prints: Turning relay off"

void loop() {
  Serial.println("Turning relay off");
digitalWrite (RELAY_PIN, HIGH);  //Deactivates relay at startup
  digitalWrite (trigger, HIGH); // We send a 10 microsecond pulse           
  delayMicroseconds (10);                                                   
  digitalWrite (trigger, LOW);                                              
  responsetime = pulseIn (echo, HIGH); // And we wait for a pulse back      
  distance = responsetime / 58; // Distance calculation in cm    

Well - now its working...dont know what the diff. is...maybe a bad connection or something.

const byte led13 = 13;               //Internal LED to how when a motion is detected and code is active
const byte led1 = 2;                 // strobe-LED pin PIN2
const byte trigger = 6;             // TRIGGER pin from distance sensor
const byte echo = 5;                // Pin to echo    
const byte RELAY_PIN = 3;  // the Arduino pin, which connects to the IN pin of relay

bool isActivated = false;


#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
                                         

#define MIN_DISTANCE 80             //Set distance in cm that sensor reacts to

unsigned long responsetime;             
unsigned long distance;                 

SoftwareSerial mySoftwareSerial(10, 11); // RX, TX pins from dfplayer mini
DFRobotDFPlayerMini myDFPlayer;

const unsigned long pauseTime = 10000;   // how long between PIR detections, and how long the scene should play
const unsigned long blinkTime = 35;    // how fast to blink strobe-led

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

void setup() {
  digitalWrite(RELAY_PIN, HIGH);      //Deactivates relay so its not staying activated
  delay (5000);             //DELAY TIME FOR DEVICE TO START UP TO AVOID ERRORS
  pinMode(led1, OUTPUT); 
   pinMode(led13, OUTPUT);      
  pinMode(trigger, INPUT); 
     
  pinMode(RELAY_PIN, OUTPUT);
  
  pinMode (trigger, OUTPUT);  
  pinMode (echo, INPUT);      
  pinMode (led1, OUTPUT);     


  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           
  delayMicroseconds (10);                                                   
  digitalWrite (trigger, LOW);                                              
  responsetime = pulseIn (echo, HIGH); // And we wait for a pulse back      
  distance = responsetime / 58; // Distance calculation in cm               





  if (distance < MIN_DISTANCE  && isActivated == false) {
    // first detection of something
    isActivated = true;                                                     //when af motion is detected
digitalWrite (led13, HIGH);
delay(5000);        //waits 5 seconds before action

digitalWrite(RELAY_PIN, LOW);   //activates relay
 
    
    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 dist sensor to trigger again or not
    digitalWrite(RELAY_PIN, HIGH);            //Deactivates relay
    digitalWrite(led1, HIGH);                                          //Keeps LED on led for some seconds
    delay(3000);                                                      //delays where the led is on                                       
    digitalWrite(led1, LOW);                                          //Turns the LED off to make shure it doesnt stays on until next reading



   delay(20000);                                                       //delay before it can start loop over again
   digitalWrite (led13, LOW);         //turns off led13 to show code has ended
    }
  }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.