program why delay is switching relay on and off

#include <LiquidCrystal_I2C.h>

#include <Wire.h>
LiquidCrystal_I2C lcd(0x27,16,2);  //
/*
*Smart Plant Watering System main module
*SmartPlantWateringSystemUsingArduinouno.ino    create on: 28/10/2018
*Copyright (C) 2007 Free Software Foundation, Inc. <arduinounomagic@gmail.com>
*
*For more detail please visit:https://www.arduinounomagic.com/2018/10/smart-plant-watering-system-using.html
*
*for more projects please visit://www.arduinounomagic.com
*/

#define WATERPUMP 13 //pump connected to pin 13
#define SENSOR  8 //soil sensor digital pin connected to pin 8
#define LDR  A0//light dependent resistor is connected to A0
#define PORTNUMBER  9600 // opens serial port, sets data rate to 9600 bps


void setup()
{
   Serial.begin(PORTNUMBER); 
   pinMode(WATERPUMP,OUTPUT); //Set pin 13 as OUTPUT pin
   pinMode(SENSOR,INPUT); //Set pin 8 as input pin, to receive data from Soil moisture sensor.
   pinMode(LDR,INPUT);

   digitalWrite(WATERPUMP,HIGH);//pump should be off initally
      pinMode(6, OUTPUT);//green
  pinMode(7, OUTPUT);//red
 lcd.init();                      // initialize the lcd 
 
  // Print a message to the LCD.
  lcd.backlight();
  lcd.print("Watering System");
   // lcd.init();
  //lcd.backlight();

}

void loop() 
{ 
  
  int val = digitalRead(SENSOR); //stores the value received from Soil moisture sensor in variable val 
  int LDRValue=analogRead(LDR);//stores the value received from LDR in variable LDRValue 
  
   if(LDRValue <= 300)
  {
    // if its dark then doesn't matter whether moisture is low or high, pump should not be off
     Serial.print("its dark, so pump will off. LDR value is: ");
     Serial.println(LDRValue);//Print LDR value
     digitalWrite(WATERPUMP,HIGH);//pump will off CHANGED FROM HIGH
     return; 
     lcd.setCursor(0, 0); 
  lcd.print("PUMP OFF"); 
  lcd.setCursor(0, 1);
  lcd.print("NIGHT TIME");
  digitalWrite(6, LOW); //green
digitalWrite(7, HIGH); //red
 // digitalWrite(9, HIGH);   

 





       
  }

  if( val == LOW)//low
  {
      
     Serial.print("its not dark and moisture is high so pump should turn on, LDR value is: ");
 //    Serial.println(LDRValue);//print LDR value
     Serial.print("\n moisture value is: ");
     Serial.println(val);//print soil moisture sensor value
     digitalWrite(WATERPUMP,HIGH); //pump goes on

            lcd.init();
  lcd.backlight();  

lcd.setCursor(0, 0); 
  lcd.print("Soil is Wet"); 
  lcd.setCursor(0, 1);
  lcd.print("Pump is Off");
     digitalWrite(6, LOW); //green
digitalWrite(7, HIGH); //red
  //digitalWrite(9, LOW);
   digitalWrite(13, LOW); //PUMP OFF WAS LOW





     
  }
  if( val == HIGH)//HIGH
  {
      Serial.print("its not dark and moisture is low so pump will off, LDR value is: ");
 //     Serial.println(LDRValue);//print LDR value
      digitalWrite(WATERPUMP,LOW);//and pump goes on 
  lcd.init();
       lcd.setCursor(0, 0); 
  lcd.print("Soil is Dry"); 
  lcd.setCursor(0, 1);
  lcd.print("Pump is on");


    digitalWrite(6, HIGH); //green
digitalWrite(7, LOW); //red
  digitalWrite(13, HIGH); //PUMP ON





      
  }
  delay(40000); //Wait for few second and then continue the loop.

}

hi
the code posted above ,original code says delay(400)which it turns relay on/off every 4 secounds if i change it to 40000 as ie is above it turns relay on/off every 4 minutes ,if i remove the line altogether it reverts bac to on/off every 4 secounds WHY does it do this ,i am looking at a moisture sensor and if dry turn relay on which in turn turns a valve on ,is there a way to turn on the valve if sensor is dry and off when it is wet by sensor condition only without putting a delay in the code IE looks at sensor if dry turn on valve for water and keep in that state untill wet when valve turns off ,im a bit lost
colin

What does your circuit look like. How did you wire the sensor etc?

Delay will wait for a number of ms. The fact that the relay switches is due to the conditions.

Note that you have a return in the first if that will prevent some code to be executed afterwards.

Don’t call init on your lcd outside the setup, initializing it once isenough.