Initiate mills timer after if statement

Good day,

Some background: I have build a little controller with an Arduino nano that will automatically switch on a small pump to circulate water from my swimming pool to a solar heating system and back to the pool. There are 3 temp sensors, one for the outside temp, one for the pool water and one for the water returning from the heating system.
The idea is that the pump should switch on if the sun is shining and if the outside temp is higher than 24 degrees C, it should remain on for 20 seconds and test the difference between the pool water temp and the water returning from the heating system, if the difference is more than 7 degC then the pump should remain on until it becomes less than 7 degC. Thereafter the system should wait for 2 hours before testing if the initial conditions are met.

Here is my code (except the setup) that sort of works but I am sure there is a better way of doing this:

#include <LiquidCrystal.h>
#include <Wire.h>                                                                                                                  
#include <OneWire.h>                                                                                                            
#include <DallasTemperature.h>                                                                                             


LiquidCrystal lcd(12,11,5,4,3,2);  

OneWire dsTemp(7); 
DallasTemperature sensors(&dsTemp); 


//ACTUAL TEMPERATURES
float tempPool;                 // the temperature of the pool water entering the system
float tempHeated;               // the temperature of the heated water
float tempOutside;              // the temp of the outside air/sun
float tempDifference;

int lightSensorPin = A1;      // input pin for the light sensor
int pumprelayPin = 6;         // digital pin 6
int manualPumpSwitchPin = 8;  //digital pin 8


// This will store the last known state of the button
int oldButtonState = LOW;
int x = 1;
int loopCount = 0;


int lightSensorValue = 0;     // variable to store the value coming from the sensor

bool pumpIsOn = false;
bool sunIsShining = false;
bool manualSwitch = false;

unsigned long previousMillis = 0;
unsigned long previousMillis1 = 0;
unsigned long interval = 20000;
unsigned long interval1 = 7200000;

byte degree[8]       = { B00010, B00101, B00010, B00000, B00000, B00000, B00000, B00000 };
byte percentage_1[8] = { B10000, B10000, B10000, B10000, B10000, B10000, B10000, B10000 };
byte percentage_2[8] = { B11000, B11000, B11000, B11000, B11000, B11000, B11000, B11000 };
byte percentage_3[8] = { B11100, B11100, B11100, B11100, B11100, B11100, B11100, B11100 };
byte percentage_4[8] = { B11110, B11110, B11110, B11110, B11110, B11110, B11110, B11110 };
byte percentage_5[8] = { B11111, B11111, B11111, B11111, B11111, B11111, B11111, B11111 }; 

byte pump_on1[8] = {    B00000, B00000, B00000, B00000, B00000, B11000, B11000, B11000 }; //Block rotation
byte pump_on2[8] = {    B11000, B11000, B11000, B00000, B00000, B11000, B11000, B11000 };
byte pump_on3[8] = {    B11011, B11011, B11011, B00000, B00000, B11000, B11000, B11000 }; 
byte pump_on4[8] = {    B11011, B11011, B11011, B00000, B00000, B11011, B11011, B11011 }; 
byte pump_on5[8] = {    B00100, B00100, B00100, B11111, B11111, B00100, B00100, B00100 };
byte pump_on6[8] = {    B11111, B11111, B11111, B11111, B11111, B11111, B11111, B11111 };



//-----------------------------------------------------------------------------------------//              
void setup(){

       sensors.begin();
       pinMode(manualPumpSwitchPin, INPUT);
       pinMode(pumprelayPin, OUTPUT);
      
       digitalWrite(pumprelayPin, HIGH);
      
       //Serial.begin(9600);
                   
       lcd.begin (20,4);  // lcd screen type
       lcd.createChar(6, degree);
       lcd.createChar(1, percentage_1);
       lcd.createChar(2, percentage_2);
       lcd.createChar(3, percentage_3);
       lcd.createChar(4, percentage_4);
       lcd.createChar(5, percentage_5);
       lcd.createChar(7, pump_on1);
       lcd.createChar(8, pump_on2);
       lcd.createChar(9, pump_on3);
       lcd.createChar(10, pump_on4);
       lcd.createChar(11, pump_on5);
       lcd.createChar(12,  pump_on6);
        

       lcd.setCursor(0,0);
       lcd.print ("       SYSTEM       ");
       lcd.setCursor(0,1);
       lcd.print ("         IS         ");
       lcd.setCursor(0,2);
       lcd.print ("    INITIALISING    ");
       lcd.setCursor(0,3);
       for (int i=0; i<20; ++i ){for(int j=0; j<5;j++)
                                {lcd.setCursor(i,3);
                                 lcd.write(j);
                                 delay(10);}}} 
//-----------------------------------------------------------------------------------------//


void loop(){
  
 manualPumpOn();
 
 unsigned long currentMillis = millis();

 sensors.requestTemperatures(); // send the request to get the temperatures
 tempOutside = sensors.getTempCByIndex(0); // populate the sensor temperatures into the variables
 tempPool = sensors.getTempCByIndex(1);                        
 tempHeated = sensors.getTempCByIndex(2);  

 tempDifference = tempHeated - tempPool;

 
lightSensorValue = analogRead(lightSensorPin); // read the value from the sensor

  if (lightSensorValue > 650){
  sunIsShining = true;

  }
  else
  {
    sunIsShining = false;
  }
 
if (manualSwitch == false){
  
    hotDay();

  if (currentMillis - previousMillis >= interval) {
        previousMillis = currentMillis;
        checkTemp();         
         }

}

populateLCD(); // Populate the LCD with the required info


} // end of loop

 
//////////////////////////// This will check if it is a hot day //////////////////////////

void hotDay(){
    
    if ((tempOutside > 24) && (sunIsShining) && (pumpIsOn == false)) {    //if it is hotter than 24 degrees outside and if it is light outside then switch on the pump                           
        digitalWrite(pumprelayPin,LOW); // Switch on the pump
        pumpIsOn = true;
      
     }

}


// checkTemp() should check if the difference in temp is  >7 and leave the pump on if no then switch it off

void checkTemp (){
 unsigned long currentMillis1 = millis();
         
      if(tempDifference > 7){
           digitalWrite(pumprelayPin,LOW); //leave the pump on, clear the loop counter and clear all averages
           
          
          }
      else 
         { 
          digitalWrite(pumprelayPin,HIGH); //  switched off the pump
         
          if (currentMillis1 - previousMillis1 >= interval1)  { // interval1 is 2 hours, thus wait for 2 hours before letting hotDay() know that the pump is actually off, thus activating hot day every 2 hours
              previousMillis1 = currentMillis1;
              
               pumpIsOn = false;    
         }
         }
 
}



void manualPumpOn(){
  
 int newButtonState = digitalRead(manualPumpSwitchPin);

  // Has the button gone high since we last read it?
  if (newButtonState == LOW && oldButtonState == HIGH) {

        if (x == 0) {
          // Toggle on
          digitalWrite(pumprelayPin,LOW); //leave the pump on
         manualSwitch = true;
         pumpIsOn = true;
          x = 1;
         delay(50);
    
        } else {
          // Toggle off
          digitalWrite(pumprelayPin,HIGH); //switch the pump off
          manualSwitch = false;
          pumpIsOn = false;
          x = 0;
        }
  }

  oldButtonState = newButtonState;

}


 void populateLCD()
 {
  
    lcd.setCursor(0,0);
    lcd.print("  PUMP CONTROLLER ");
 
 
  if (pumpIsOn == true){
    for(int j=7; j<12;j++){
      lcd.write(j);
      delay (400);
      lcd.setCursor(18,0);
    }
  }
  
  
 lcd.setCursor(0,1);
  lcd.print("Temp Outside: ");
  lcd.print(tempOutside,1);
  lcd.write(6);
  lcd.print("C");
   
 lcd.setCursor(0,2);
  lcd.print("Pool Water:   ");
  lcd.print(tempPool,1);
  lcd.write(6);
  lcd.print("C");
   
 lcd.setCursor(0,3);
  lcd.print("Return Water: ");
  lcd.print(tempHeated,1);
  lcd.write(6);
  lcd.print("C");   
  lcd.print("  ");                 
 delay(10);
 }

You need to post the complete program so I can see how variables are defined and initialized.

...R

Updated as requested

You have your control logic and your control outputs all jumbled up. Outside setup() I counted 5 lines with

digitalWrite(pumprelayPin

Your program would be much easier to understand (for you as well as us) if you create a small function like this which is called from loop()

void activatePump() {
  if (pumpIsOn == true) {
     // digitalWrite ... pump ON
  }
  else {
    // digitalWrite ... pump OFF
  }
}

Then the rest of your code can concentrate on deciding whether the variable pumpIsOn should be true or false

For the timing issue I think you need a line

pumpStartMillis = millis();

which gets called when you start the pump and which gets used to detect when the 20 secs is up with

if (currentMIllis - pumpStartMillis >= minPumpRunMillis) {

Note that using meaningful variable names also makes code very much easier to understand. You even have a variable named "x". How can anyone know what that is for? Marking the spot?

...R