[HELP] online monitoring of a switch

part 2

if (moistureSensorValue < 238) {
    // in case of soggy soil:
    // system messages
    wdt_reset();
    lcd.clear();
    RightHour();
    lcd.setCursor(0, 3);
    lcd.print(string_table[4]);
    // lights up the correct LED
    digitalWrite(drysoilLEDPin, LOW);
    digitalWrite(moistsoilLEDPin, LOW);
    digitalWrite(soggyLEDPin, HIGH);
    delay(1000);
  }

  // between time 8:00:00 to 9:59:59 (morning check)
  if ((H >= 8) && (H <= 9) && (moistureSensorValue >= 750))
  {
    while (moistureSensorValue >= 750 && levelSensorValue <= 600) {
      // system messages
      wdt_reset();
      lcd.clear();
      RightHour();
      lcd.setCursor(0, 2);
      lcd.print(string_table[8]);
      lcd.setCursor(0, 3);
      lcd.print(string_table[5]);

      // turn the pump on
      digitalWrite(pumpPin, LOW);
      delay(150);
      //digitalWrite(pumpPinLow, HIGH);
      digitalWrite(pumpLEDPin, HIGH);
      delay(5000);
      // if the soil is not moist so far
      // check sensors again
      SensorRead();
      lcd.clear();
    }
    // turn the pump off
    digitalWrite(pumpPin, HIGH);
    // digitalWrite(pumpPinLow, HIGH);
    digitalWrite(pumpLEDPin, LOW);
    lcd.clear();
  }

  // between time 17:00:00 to 18:59:59 (afternoon check)
  else if ((H >= 17) && (H <= 18) && (moistureSensorValue >= 750))
  {

    while (moistureSensorValue >= 750 && levelSensorValue <= 600) {
      // system messages
      wdt_reset();
      lcd.clear();
      RightHour();
      lcd.setCursor(0, 2);
      lcd.print(string_table[8]);
      lcd.setCursor(0, 3);
      lcd.print(string_table[5]);

      // turn the pump on
      digitalWrite(pumpPin, LOW);
      delay(150);
      //digitalWrite(pumpPinLow, HIGH);
      digitalWrite(pumpLEDPin, HIGH);
      delay(5000);
      // if the soil is not moist so far
      // check sensors again
      SensorRead();
      lcd.clear();
    }
    // turn the pump off
    digitalWrite(pumpPin, HIGH);
    //digitalWrite(pumpPinLow, HIGH);
    digitalWrite(pumpLEDPin, LOW);
    lcd.clear();
  }
  else if (moistureSensorValue > 845)
  {

    while (moistureSensorValue >= 700 && levelSensorValue <= 600) {
      // system messages
      wdt_reset();
      lcd.clear();
      RightHour();
      lcd.setCursor(0, 2);
      lcd.print(string_table[8]);
      lcd.setCursor(0, 3);
      lcd.print(string_table[5]);

      // turn the pump on
      digitalWrite(pumpPin, LOW);
      delay(150);
      //digitalWrite(pumpPinLow, HIGH);
      digitalWrite(pumpLEDPin, HIGH);
      delay(5000);
      // if the soil is not moist so far
      // check sensors again
      SensorRead();
      lcd.clear();
    }
    // turn the pump off
    digitalWrite(pumpPin, HIGH);
    //digitalWrite(pumpPinLow, HIGH);
    digitalWrite(pumpLEDPin, LOW);
    lcd.clear();
  }
  
  //Sensor function
  SensorRead();

  //LCD Backlight button
  Val = digitalRead(LCDBacklight);
  while (Val == 1)
  {
    z = 0;
    Serial.println(z);
    Val = digitalRead(LCDBacklight);
  }
}

// Real Time Clock Function
void RightHour()
{
  DateTime Now = RTC.now();
  String clock_date = "  Date: ";
  String clock_hour = "   Time: ";

  int _day = Now.day();
  int _month = Now.month();
  int _year = Now.year();

  clock_date += fixZero(_day);
  clock_date += "/";
  clock_date += fixZero(_month);
  clock_date += "/";
  clock_date += _year;

  int _hour = Now.hour();
  int _minute = Now.minute();
  int _second = Now.second();

  clock_hour += fixZero(_hour);
  clock_hour += ":";
  clock_hour += fixZero(_minute);
  clock_hour += ":";
  clock_hour += fixZero(_second);

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(clock_date);
  lcd.setCursor(0, 1);
  lcd.print(clock_hour);

    //LCD Backlight button
  Val = digitalRead(LCDBacklight);
  while (Val == 1)
  {
    z = 0;
    Serial.println(z);
    Val = digitalRead(LCDBacklight);
  }

  delay(500);
}


String fixZero(int i)
{
  String ret;
  if (i < 10) ret += "0";
  ret += i;
  return ret;
}

void watchdogSetup(void)
{
  cli();

  wdt_reset();

  WDTCSR |= (1 << WDCE) | (1 << WDE);

  WDTCSR = (1 << WDIE) | (1 << WDE) | (1 << WDP3) | (0 << WDP2) | (0 << WDP1) | (1 << WDP0);

  sei();

}

void lcdbacklightsetting() {

  //LCD Backlight button
  Val = digitalRead(LCDBacklight);
  while (Val == 1)
  {
    z = 0;
    Serial.println(z);
    Val = digitalRead(LCDBacklight);
  }

  Serial.println(".....................................Val is:");
  Serial.println(Val);
  Serial.println("...........................................");

  if ((moistureSensorValue < 750) && (moistureSensorValue >= 238) && (levelSensorValue <= 600) && (z >= 8)) {
    lcd.noBacklight();
    z = 9;
  }
  else if ((moistureSensorValue < 750) && (moistureSensorValue >= 238) && (levelSensorValue <= 600) && (z < 8)) {
    lcd.backlight();
    z = z + 1;
  }
  else {
    lcd.backlight();
    z = 0;
  }
}

ISR(WDT_vect)
{
  while (1);
}


void SensorRead () {
  if ((moistureSensorValue < 750) && (levelSensorValue <= 600) && (x >= 20) && (x < 101)) {
    digitalWrite(HSensorVCC, LOW);
    digitalWrite(LSensorVCC, LOW);
    x = x + 1;
  }
  else if ((x < 20) || (moistureSensorValue >= 750) || (levelSensorValue > 600)) {
    digitalWrite(HSensorVCC, HIGH);
    digitalWrite(LSensorVCC, HIGH);
    levelSensorValue = analogRead(levelSensorPin);
    moistureSensorValue = analogRead(moistureSensorPin);
    x = x + 1;
  }
  else {
    x = 0;
  }

    //LCD Backlight button
  Val = digitalRead(LCDBacklight);
  while (Val == 1)
  {
    z = 0;
    Serial.println(z);
    Val = digitalRead(LCDBacklight);
  }
}