getting stuck in if/else loops

im building an arduino garden monitor with my kids while we are on lock down.
and getting stuck in an if loop the code currently works correctly but if i move lightstate into a high/low variable (to make calibrating day/night easier than changing it in multiple locations) i get stuck in the that if loop and my code doesnt work and never activates the watering pump code is on pastebin i can paste it here if it helps
Garduino code

What do you mean stuck in an "if loop"?
An "if" is not a "loop".

"lightstate into a high/low variable".
What do you mean by that?
Is that the code you posted, or some other version?
I don't see anything that could be described as a "high/low variable" - whatever that might mean?

It might help if you post the output you are getting.
Also, what is supposed to happen when lightstate == 450 ?

Edit: Also, clearing the LCD immediately after displaying something seems somewhat pointless...

        lcd.print("Soil Wet");
        lcd.clear();

Welcome to the Forum. Please read these two posts:

General Guidance and How to use the Forum
and
Read this before posting a programming question ...

Please do paste it here, inside code tags as explained in the links above.

pcbbc:
What do you mean stuck in an "if loop"?
An "if" is not a "loop".

"lightstate into a high/low variable".
What do you mean by that?
Is that the code you posted, or some other version?
I don't see anything that could be described as a "high/low variable" - whatever that might mean?

i linked the wrong code i linked a previous (working version of the code)

// Garduino with LCD, Soil Sensor and LDR for sunlight detection
// LDR connects to AO 
// Soil Sensor on Digital 8
// Pump connects via mosfet/relay to pin 13
// Backlight Anode to pin 6 to enable backlight control 
// LCD rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2

#include <LiquidCrystal.h>
const int lightsensor = A0;   // Light Sensor Pin 
//const int pHsensor = A1; // pH Sensor Pin
const int soilsensor = 8;   // Soil Sensor Pin
const int pumpPin = 13;       // Water Pump Pin
const int BLPin = 6; // LCD Backlight Pin
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

// variables will change:
int soilstate = 0;         // variable for reading the sensor status
int lightstate = 0;         // variable for sun light
int daynight = 0; // day night variable
//int pH = 0; // pH actual value
//int rawpH = 0; // raw pH from sensor
void setup() {
  pinMode(BLPin, OUTPUT); // initialize lcd backlight pin
  pinMode(pumpPin, OUTPUT);   // initialize the pump pin as an output:
  pinMode(soilsensor, INPUT);   // initialize the soil sensor pin as an input:
  lcd.begin(16, 2);   // set up the LCD's number of columns and rows:
  Serial.begin(9600); //configure serial to talk to computer
   }

void loop() {
  soilstate = digitalRead(soilsensor);   // read the state of the soil sensor value:
  lightstate = analogRead(lightsensor);   // read the state of the light sensor value:
  //rawpH = analogRead(pHsensor);
  //pH = 
  if (lightstate < 450){
    digitalWrite(daynight, HIGH);
  } else {
    digitalWrite(daynight, LOW);
  }
  Serial.println(lightstate);
  Serial.println(soilstate);  
    if (soilstate == HIGH && daynight == HIGH ) {// turn pump on
      digitalWrite(pumpPin, HIGH);
      digitalWrite(BLPin, HIGH);
      lcd.display();
      lcd.print("Soil Dry");
      lcd.clear();
  } else if (soilstate == LOW && daynight == HIGH) {// turn pump off
      digitalWrite(pumpPin, LOW);
      digitalWrite(BLPin, HIGH);
      lcd.display();
      lcd.print("Soil Wet");
      lcd.clear();
  } else if (soilstate == HIGH && daynight == LOW) {// turn pump and lcd off
      digitalWrite(pumpPin, LOW);
      digitalWrite(BLPin, LOW);
      lcd.noDisplay();
      lcd.clear();
  } else if (soilstate == LOW && daynight == LOW) {//turn pump and lcd off 
      digitalWrite(pumpPin, LOW);
      digitalWrite(BLPin, LOW);
      lcd.noDisplay();
      lcd.clear();
        }
}

this is the broken code. I want to be able to get the lightstate variable parsed into high/low in the daynight variable so that i can edit the value easily rather than in multiple locations (which leaves potential for mistakes to be made when adjusting. im also adding a ph sensor to monitor the plant food reservoir along with a level sensor on the reservoir to prevent pump damage which will sound a buzzer. but i cant get the daynight variable to work it just results in the lcd being off (im currently simulating in tinkercad)

int daynight = 0; // day night variable
...
    digitalWrite(daynight, HIGH);

You are setting pin 0 high. It is a serial pin. If you set it to HIGH somewhere, you will be setting pin 1 to some value, it is also a serial pin.

You may find this hard to accept, but mistakes like this are often the result of using confusing variable names. Values should be called values, states should be called states, pins should be called pins. 'soilstate' for example, suggests a binary condition, like wet or dry. 'soilValue' or 'soilReading' would be better. I see that you have already named some pin number variables correctly. So you just need to be more thorough and consistent.

You should also take advantage of C types and make your logical values bool types and use 'true' and 'false' values instead of 0 and 1.

'daynight' is ambiguous. If I say, (daynight == true) you don't know if I am saying it is day or night. If I say (itIsDay == true) then you do know. Another way is to use enums, like (daynight == DAY)

changing it so the variable isnt set to 0 makes no difference to the code and looking at the arduino reference pages shows me i was doing it right. which is whats making me think im stuck in the first if/else which is now read the sensor and if its above 450 write high to a variable. the chances of it being 450 ever are slim but it isnt an eventuality id thought of. ill probably change it to be equal to or greater

ive seen my error. i was using digital write rather than just writing the value to the variable. thanks for your help now to break it more trying to add the pH sensor code

As was suggested in reply #1, you can never be "stuck" in an if-else.

Naming variables and/or pins "daynight" is not so helpful. Does a "high" mean day, or night?
No one except you knows, and you may well forget when you come back to this code in 6 months.

I would suggest either "day" or "night" depending on which a non-zero (HIGH) value represents on the pin. Assuming a value of less than 450 indicates darkness, and therefore night...

 bool is_night = lightstate < 450;
  if (is_night){
    digitalWrite(nightPin, HIGH);
  } else {
    digitalWrite(nightPin, LOW);
  }
  if (soilstate == HIGH && is_night == true) {// turn pump on
    digitalWrite(pumpPin, HIGH);
    digitalWrite(BLPin, HIGH);
    lcd.display();
    lcd.print("Soil Dry");
    lcd.clear();
  } else if (soilstate == LOW && is_night == true) {// turn pump off
    digitalWrite(pumpPin, LOW);
    digitalWrite(BLPin, HIGH);
    lcd.display();
    lcd.print("Soil Wet");
    lcd.clear();
  } else if (soilstate == HIGH && is_night == false) {// turn pump and lcd off
    digitalWrite(pumpPin, LOW);
    digitalWrite(BLPin, LOW);
    lcd.noDisplay();
    lcd.clear();
  } else if (soilstate == LOW && is_night == false) {//turn pump and lcd off
    digitalWrite(pumpPin, LOW);
    digitalWrite(BLPin, LOW);
    lcd.noDisplay();
    lcd.clear();
  }

In fact you can actually write...

bool is_night = lightstate < 450;
digitalWrite(nightPin, is_night);

If you are clever with your variable names and use enhanced keywords, "and, or, not", you can write COBOL'ish (English-like) statements in C, like

  if (not soil_is_wet and it_is_night) {// turn pump on

am going through now renaming and bringing the names closer to their state meanings and adjusting my comments before i break the code trying to add pH sensing. I have added a low water buzzer and it sort of works (controlled by daytime (ex daynight) and waterlow variables (not in old code) im also planning on making it "offgrid" hence the references to Vbatt

// Garduino with LCD, Soil Sensor and LDR for sunlight detection
// LDR connects to AO 
// Soil Sensor on Digital 8
// Pump connects via mosfet/relay to pin 13
// Backlight Anode to pin 6 to enable backlight control 
// LCD rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2
// pH sensor signal connects to A1
// Vbatt connected through voltage divider to A2

#include <LiquidCrystal.h>
const int lightsensor = A0;   	// Light Sensor Pin 
//const int pHsensor = A1;		// pH Sensor Pin
const int soilsensor = 8;  		// Soil Sensor Pin
const int pumpPin = 13;      	// Water Pump Pin
const int BLPin = 6;			// LCD Backlight Pin
const int LVLPin = 9;			// Water Level Sensor
const int Buzzer = 10;		// Alarm Buzzer
//const int Vbatt = A2;                 // Vbatt input
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

// variables will change:
int soildry;         		   	// variable for reading the sensor status
int lightlevel;         		// variable for sun light
int daytime;					// day night variable
int waterlow;					// variable for pump enabled 
int alarm;						// alarm state caused by level sensor and Vbatt
//int pH;						// pH actual value
//int rawpH;					// raw pH from sensor
void setup() {
  pinMode(BLPin, OUTPUT); 		// initialize lcd backlight pin as an output
  pinMode(pumpPin, OUTPUT);   	// initialize the pump pin as an output:
  pinMode(soilsensor, INPUT);   // initialize the soil sensor pin as an input:
  pinMode(LVLPin, INPUT);		// initialize reservoir sensor pin as an input
  lcd.begin(16, 2);   			// set up the LCD's number of columns and rows:
  Serial.begin(9600); 			//configure serial to talk to computer
   }

void loop() {
  soildry = digitalRead(soilsensor);  	  // read the state of the soil sensor value:
  lightlevel = analogRead(lightsensor);   // read the state of the light sensor value:
  waterlow = digitalRead(LVLPin);
  //rawpH = analogRead(pHsensor);
  //pH = 
  if (lightlevel <= 450){
    daytime = HIGH;
    Serial.println("Day");
  } else {
    daytime = LOW;
    Serial.println("Night");
  }
  if (alarm == LOW){
    digitalWrite(Buzzer, LOW);
  } else {
    digitalWrite(Buzzer, HIGH);
  }
  if (daytime == HIGH && waterlow == HIGH){
  alarm = HIGH;
  Serial.println("Water Low");
  } else {
  alarm = LOW;
  digitalWrite(Buzzer, LOW);
  Serial.println("Water OK");
  }
  
  if (soildry == HIGH && daytime == HIGH ) {// turn pump on
      digitalWrite(pumpPin, HIGH);
      digitalWrite(BLPin, HIGH);
      lcd.clear();
      lcd.display();
      lcd.print("Soil Dry");
      Serial.println("Soil Dry");
      delay(100);
  } else if (soildry == LOW && daytime == HIGH) {// turn pump off
      digitalWrite(pumpPin, LOW);
      digitalWrite(BLPin, HIGH);
      lcd.clear();
      lcd.display();
      lcd.print("Soil Wet");
      Serial.println("Soil Wet");
      delay (100);
  } else if (soildry == HIGH && daytime == LOW) {// turn pump and lcd off
      digitalWrite(pumpPin, LOW);
      digitalWrite(BLPin, LOW);
      lcd.noDisplay();
      delay(100);
  } else if (soildry == LOW && daytime == LOW) {//turn pump and lcd off 
      digitalWrite(pumpPin, LOW);
      digitalWrite(BLPin, LOW);
      lcd.noDisplay();
      delay(100);
  }
}