Problem with onChange function and millis delays in switch

Hi ec32021,
Many thanks for your input.
I've tried your changes the best that I can achieve now is to step through the switch case by case by switching the cloud switch on and off??????
Here is the complet code as you requested. I have commented out the other routines in the loop so as not to confuse.
Greatfully Nick:-)


/* 
  Sketch generated by the Arduino IoT Cloud Thing "Untitled 2"
  https://create.arduino.cc/cloud/things/0fb7db16-7499-4f12-8d3b-033a81cb8d3b 

  Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes are made to the Thing

  String message;
  float controller_Humid;
  float controller_Temp;
  float humidDHT2;
  float inlet_Pressure;
  float pH_sensor;
  float sensor_01;
  float sensor_02;
  float sensor_03;
  float sensor_04;
  float tempDHT2;
  CloudSchedule poolScheduleEvening;
  CloudSchedule poolScheduleMorning;
  bool filter_Active;
  bool filter_Override;
  bool humid_Warning;
  bool pool_deck;
  bool shower;
  bool sW1_PoolDeck;
  bool sW2_LowerDeck;
  bool sW3_TikiBar;
  bool sW4_Pool;
  bool uV_Reactor;

  Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
  which are called when their values are changed from the Dashboard.
  These functions are generated with the Thing and added at the end of this sketch.
*/

#include "thingProperties.h"
#include <Arduino.h>
#include "EthernetENC.h"
#include "DHT.h"
#include "Wire.h" // Library for I2C communication
#include "LiquidCrystal_I2C.h" // Library for LCD
#include <OneWire.h>              //for DS18b20
#include <DallasTemperature.h>     //for DS18b20         
#include <Streaming.h>

#define DHTPin1 4
#define DHTPin2 3
#define DHTTYPE1 DHT22
#define DHTTYPE2 DHT22


LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 20, 4);

DHT dht1(DHTPin1, DHTTYPE1);
DHT dht2(DHTPin2, DHTTYPE2);

// Sensor at pin 2 for DS18B20
OneWire bus(2);
DallasTemperature sensors(&bus);         //**************************

// Addresses of DS18B20s
DeviceAddress sensor_1 = { 0x28, 0xFF, 0x64, 0x1D, 0xF1, 0xC9, 0x48, 0x47 }; //Inlet:
DeviceAddress sensor_2 = { 0x28, 0xFF, 0x64, 0x1D, 0xF1, 0xF7, 0xC1, 0xA6 }; //Skimer:
DeviceAddress sensor_3 = { 0x28, 0xFF, 0x0A, 0x01, 0xA3, 0x17, 0x04, 0x8B }; //Pool Floor:
DeviceAddress sensor_4 = { 0x28, 0xFF, 0xC7, 0x1B, 0xA2, 0x17, 0x05, 0x93 }; //Filter Room:

/* As installed for the pool
Ambi = 0x28, 0x05, 0x17, 0xa2, 0x1b, 0xa7, 0xff
Filter = 0x28, 0x05, 0x17, 0xa2, 0x45, 0xbb, 0xff
Floor = 0x28, 0x01, 0x20, 0x58, 0xfe, 0x5e, 0x4a
Inlet = 0x28, 0x01, 0x20, 0x58, 0xf8, 0x4a, 0x6c
Skim = 0x28, 0x01, 0x20, 0x5b, 0xc3, 0x99, 0x9b
*/
///************Pressure**********////
int pressureInput = A3; //select the analog input pin for the pressure transducer
float V, Pin;
float P_total  = 0;

///**************PH***************///
float calibration_value = 16.61;
int phval = 0; 
unsigned long int avgval; 
int buffer_arr[10],temp;
float ph_act = 0.0;

///***************Shower Variables*******************************///
const long WAIT_1Sec = 1000; //1Sec
const long WAIT_1Min = 60000; //1Min
const long WAIT_5Min = 300000; //5Mins

static unsigned long previousSTime = 0;
static unsigned long startSTime = 0; 
unsigned long currentSTime;// = millis();
static int Count = 0;
unsigned long Run = 1; 

/*
unsigned long now;
unsigned long then = 0;
unsigned long intervals[5] = {0, 2000, 4000, 6000, 2000};
int current_interval_index = 0;
*/




int page_counter=1 ;       //To move beetwen pages
//Variables for auto scroll
unsigned long previousPagetime = 0;
unsigned long interval = 10000; //Desired wait time 10 seconds

int temp_interval = 2000; //interval between two temperature check
int temp_time_mark; //store the millis of the last temperature check

//**********************counter for loop execution*******************
const long eventTime_HumTemp = 1000; //in ms
const long eventTime_SensTemp = 1000; //in ms
const long eventTime_Press = 1000; //in ms
const long eventTime_Ph = 1000; //in ms

unsigned long previousTime_HTemp = 0;
unsigned long previousTime_Temp = 0;
unsigned long previousTime_Press = 0;
unsigned long previousTime_Ph = 0;


// Arduino Digital I/O pin numbers for UNO R3
enum {Relay1=13, Relay2=5, Relay4=6, Relay6=7, Relay8=8, Relay10=9, Relay12=10,
       Relay14=11, Relay16=12};   //*****Setting A4 & A5 looses the screen!!!!!!!!! obviously clashes with i2c connections

int relays[] = {Relay1,Relay2,Relay4,Relay6,Relay8,Relay10,Relay12,Relay14,Relay16};
// Number of relays in the array
enum { maxRelayCount = sizeof relays / sizeof relays[0] };
enum { RELAY_OFF = HIGH}; 


void dosensors(){          
  if ( millis() - temp_time_mark >= temp_interval) {
    sensors.requestTemperatures(); // Send the command to get temperatures
  }
    sensors.requestTemperatures();  
    float temp_Inlet = round(sensors.getTempC(sensor_1) * 10.0) / 10;
    float temp_Skim = round(sensors.getTempC(sensor_2) * 10.0) / 10;
    float temp_Floor = round(sensors.getTempC(sensor_3) * 10.0) / 10;
    float temp_Filter = round(sensors.getTempC(sensor_4) * 10.0) / 10;
    sensor_01 = temp_Inlet;
    sensor_02 = temp_Skim;
    sensor_03 = temp_Floor;
    sensor_04 = temp_Filter;
    delay(1000); 
}

void HumTemp(){ 
    humidDHT2 = dht1.readHumidity(); //Ambient
    tempDHT2 = dht1.readTemperature();
    controller_Humid = dht2.readHumidity(); //Controller
    controller_Temp = dht2.readTemperature();
    delay(1000);
}

void getPressure(){  
  const float  OffSet = 0.51 ;
    Pin = analogRead(0);
    V = Pin * 5.00 / 1024;     //Sensor output voltage
    inlet_Pressure = (((V - OffSet) * 250) * 0.1450377377);             //Calculate water pressure in psi
    inlet_Pressure = fabs(inlet_Pressure);
}

void getPH(){ 
   for(int i=0;i<10;i++) 
   { 
   buffer_arr[i]=analogRead(A1);
   delay(30);
   }
   for(int i=0;i<9;i++)
   {
   for(int j=i+1;j<10;j++)
   {
   if(buffer_arr[i]>buffer_arr[j])
   {
   temp=buffer_arr[i];
   buffer_arr[i]=buffer_arr[j];
   buffer_arr[j]=temp;
   }
   }
   }
   avgval=0;
   for(int i=2;i<8;i++)
   avgval+=buffer_arr[i];
   float volt=(float)avgval*5.0/1024/6;
   ph_act = -5.70 * volt + calibration_value;
   pH_sensor = ph_act;
   delay(100);
}

/* for multi tasking display*/
void display_Sensors(){
    float temp_Inlet = round(sensors.getTempC(sensor_1) * 10.0) / 10;
    float temp_Skim = round(sensors.getTempC(sensor_2) * 10.0) / 10;
    float temp_Floor = round(sensors.getTempC(sensor_3) * 10.0) / 10;
    float temp_Filter = round(sensors.getTempC(sensor_4) * 10.0) / 10;
    lcd.setCursor(0, 0);
    lcd.print("Inlet: ");
    lcd.setCursor(13, 0);
    lcd.print(temp_Inlet,1);
    lcd.print((char)223); lcd.print("C");  
    lcd.setCursor(0, 1);
    lcd.print("Skimmer: ");
    lcd.setCursor(13, 1);
    lcd.print(temp_Skim,1);
    lcd.print((char)223); lcd.print("C");  
    lcd.setCursor(0, 2);
    lcd.print("Floor: ");
    lcd.setCursor(13, 2);
    lcd.print(temp_Floor,1);
    lcd.print((char)223); lcd.print("C");  
    lcd.setCursor(0, 3);
    lcd.print("Filter Room: ");
    lcd.setCursor(13, 3);
    lcd.print(temp_Filter,1);
    lcd.print((char)223); lcd.print("C"); 
}

void display_Humid(){
  lcd.setCursor(0, 0);
  lcd.print("Ambient Temp: ");
  lcd.print(tempDHT2,1);
  lcd.print(char(223));
  lcd.print("C");

  lcd.setCursor(0, 1);
  lcd.print("Ambient Hum.: ");
  lcd.print(humidDHT2,1);
  lcd.print("%");

  lcd.setCursor(0, 2);
  lcd.print("Control Temp: ");
  lcd.print(controller_Temp,1);
  lcd.print(char(223));
  lcd.print("C");

  lcd.setCursor(0, 3);
  lcd.print("Control Hum.: ");
  lcd.print(controller_Humid,1);
  lcd.print("%");
}

void display_Pressure(){
  //lcd.setCursor(0, 0);
  //lcd.print("Voltage:");
  //lcd.print(V, 3);
  //lcd.print("V");
  lcd.setCursor(5, 0);
  lcd.print("Pressure: ");
  lcd.setCursor(0, 1);
  lcd.print("--------------------");
  lcd.setCursor(6, 2);
  lcd.print(inlet_Pressure , 1);
  lcd.setCursor(9, 2);
  lcd.print(" Psi");
  lcd.setCursor(0, 3);
  lcd.print("--------------------");
}

void display_PH(){
  lcd.setCursor(6, 0);
  lcd.print("PH Val:");
  lcd.setCursor(0, 1);
  lcd.print("--------------------");
  lcd.setCursor(7, 2);
  lcd.print(pH_sensor);
  lcd.setCursor(0, 3);
  lcd.print("--------------------");
}

void setup() {
    // Defined in thingProperties.h
  initProperties();
  // Initialize serial and wait for port to open:
  
  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
  Serial.begin(9600);
  sensors.begin();
  dht1.begin();
  dht2.begin();
  delay(1000); 
  lcd.init();
  lcd.backlight(); 
  shower = false;

  //Set pins to OFF & declare pins as OUTPUTS
  for(int i = 0; i < maxRelayCount; ++i) {
    pinMode(relays[i], OUTPUT);
    digitalWrite(relays[i], RELAY_OFF);
  }
 // Check that all relays are inactive at Reset
   delay(1000);
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
}
           
void loop() {
  ArduinoCloud.update();

  unsigned long currentTime = millis();
  //currentSTime = millis();
  //now = millis();
/*
  if (currentTime - previousTime_HTemp >= eventTime_HumTemp) {
    HumTemp();
    Serial.print(controller_Temp,1);
    Serial.print(controller_Humid,1);
    Serial.print(tempDHT2,1);
    Serial.print(humidDHT2,1);
    previousTime_HTemp = currentTime;
  }

  if (currentTime - previousTime_Temp >= eventTime_SensTemp) {
    dosensors();
    Serial.print(sensor_01,0);
    Serial.print(sensor_02,0);
    Serial.print(sensor_03,0);
    Serial.print(sensor_04,0);
    previousTime_Temp = currentTime;
  }

  if (currentTime - previousTime_Press >= eventTime_Press) {
    getPressure();
    Serial.print(inlet_Pressure,2);
    previousTime_Press = currentTime;
  }

  if (currentTime - previousTime_Ph >= eventTime_Ph) {
    getPH();
    Serial.print(pH_sensor,1);
    previousTime_Ph = currentTime;
  }

  switch (page_counter) {
    case 1:{     //page 1
      display_Sensors();
    }
    break;
    case 2: { //page 2 
     display_Humid(); 
    }
    break;
    case 3: {   //page 3 
      display_Pressure();
    }
    break;
    case 4: {   //page 4
      display_PH();
    }
    break;
  }

//-----------Auto scroll function---------------//
     unsigned long currentPagetime = millis();            
     
     if (currentPagetime - previousPagetime > interval) {  
     previousPagetime = currentPagetime;                   
     lcd.clear();                                      
     if (page_counter < 4){                            
     page_counter = page_counter +1;                   
     }
     else{
      page_counter=1;                                  
     }
    } 
    
  if (poolScheduleMorning.isActive() || poolScheduleEvening.isActive()) {
    uV_Reactor = true;
    filter_Active = true;
  }else {
    uV_Reactor = false;
    filter_Active = false;
  }
 */
  ArduinoCloud.update();
   // }

}


/* ******************Not Require ****************************/

/*
  Since Message is READ_WRITE variable, onMessageChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onMessageChange()  {
  // Add your code here to act upon Message change
}

/* ******************Not Require ****************************/
/*
  Since PoolDeck is READ_WRITE variable, onPoolDeckChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onPoolDeckChange()  {
  // Add your code here to act upon PoolDeck change
}

/* ******************Not Require ****************************/
/*
  Since FilterOverride is READ_WRITE variable, onFilterOverrideChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onFilterOverrideChange()  {
  // Add your code here to act upon FilterOverride change
}

/* ******************Not Require ****************************/
/*
  Since PoolSchedule is READ_WRITE variable, onPoolScheduleChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onPoolScheduleChange()  {
  // Add your code here to act upon PoolSchedule change
  
/*
  Since PoolScheduleMorning is READ_WRITE variable, onPoolScheduleMorningChange() is
  executed every time a new value is received from IoT Cloud.

void onPoolScheduleMorningChange()  {
  // Add your code here to act upon PoolScheduleMorning change
}*/  
  
  
}
/*******************************************************************************/

/*
  Since SW1PoolDeck is READ_WRITE variable, onSW1PoolDeckChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onSW1PoolDeckChange()  {
  // Add your code here to act upon SW1PoolDeck change
    if (sW1_PoolDeck == 1) {
      digitalWrite(Relay12, HIGH);
    } 
    else {
      digitalWrite(Relay12, LOW);
    }
}

/*
  Since SW2LowerDeck is READ_WRITE variable, onSW2LowerDeckChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onSW2LowerDeckChange()  {
  // Add your code here to act upon SW2LowerDeck change
      if (sW2_LowerDeck == 1) {
      digitalWrite(Relay14, HIGH);
    } 
    else {
      digitalWrite(Relay14, LOW);
    }
}

/*
  Since SW3TikiBar is READ_WRITE variable, onSW3TikiBarChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onSW3TikiBarChange()  {
  // Add your code here to act upon SW3TikiBar change
    if (sW3_TikiBar == 1) {
      digitalWrite(Relay16, HIGH);
    } 
    else {
      digitalWrite(Relay16, LOW);
    }
}

/*
  Since SW4Pool is READ_WRITE variable, onSW4PoolChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onSW4PoolChange()  {
  // Add your code here to act upon SW4Pool change
  if (sW4_Pool == 1) {
      digitalWrite(Relay1, HIGH);
    } 
    else {
      digitalWrite(Relay1, LOW);
    }
}


/*
  Since Shower is READ_WRITE variable, onShowerChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onShowerChange(){
    if(shower==1){  
      run_Shower();    
    }
}

void run_Shower() {
  currentSTime = millis();
    if ((currentSTime - previousSTime) >= Run) {  
          Count++;
            switch(Count){
                case 1:
                  lcd.clear();
                  lcd.setCursor(0, 0);
                  if(filter_Active){
                    lcd.print("Filter active turn Off"); 
                  }else{
                    lcd.print("Filter not active");
                    }
                  lcd.setCursor(0, 1);
                  lcd.print("Trafo ON ");
                  lcd.setCursor(0, 3);
                  lcd.print("Case 1");            
                  /*
                  digitalWrite(Relay4, LOW); //Filter Off
                  digitalWrite(Relay6, HIGH); //3 Way Trafo On
                  */
                  Run = WAIT_1Sec;
                  break;

                case 2:
                  lcd.clear();
                  lcd.setCursor(0, 0);
                  lcd.print("3Way Valve ON->Pump");
                  lcd.setCursor(0, 3);
                  lcd.print("Case  2"); 
                  /*
                  digitalWrite(Relay10, HIGH); //3 Way over to shower pump (Posibly) LOW???
                  */
                  //Run = WAIT_1Min;
                  Run = WAIT_1Sec;
                  break;

                case 3:
                  lcd.clear();
                  if(filter_Active){
                    lcd.setCursor(0, 0);
                    lcd.print("Filter back ON"); 
                  }
                  lcd.setCursor(0, 1);
                  lcd.print("Shower Pump ON");                  
                  lcd.setCursor(0, 3);
                  lcd.print("Case   3");  
                  /*
                  digitalWrite(Relay4, HIGH); //Filter ON
                  digitalWrite(Relay8, HIGH); //Shower Pump ON
                  */
                  //Run = WAIT_5Min;
                  Run = WAIT_1Sec;
                  break;

                case 4:
                  lcd.clear();
                  if(filter_Active){
                    lcd.setCursor(0, 0);
                    lcd.print("Filter Off"); 
                  }
                  lcd.setCursor(0, 1);
                  lcd.print("Shower Pump OFF");                                             
                  lcd.setCursor(0, 2);
                  lcd.print("3Way back <- Filter");
                  lcd.setCursor(0, 3);
                  lcd.print("Case    4"); 
                  /*
                  digitalWrite(Relay4, LOW); //Filter Off                  
                  digitalWrite(Relay8, LOW); //Shower Pump Off
                  digitalWrite(Relay10, LOW); //3 Way over to filter (Posibly) HIGH???
                  */
                  //Run = WAIT_1Min;
                  Run = WAIT_1Sec;
                  break;

                case 5:
                  lcd.clear();
                  if(filter_Active){
                    lcd.setCursor(0, 0);
                    lcd.print("Filter back ON"); 
                  }
                  lcd.setCursor(0, 1);
                  lcd.print("Trafo OFF");  
                  lcd.setCursor(0, 3);
                  lcd.print("Case     5");        
                  /*
                  digitalWrite(Relay4, HIGH); //Filter ON                  
                  digitalWrite(Relay6, LOW); //3 Way Trafo Off
                  */
                  Run = WAIT_1Sec;
                  break;    

                default:   
                  if(Count = 5){
                    shower = 0; //Turn cloud switch back to OFF
                    Count = 0;
                    Run = 0;
                    //break;
                  }
            }
            //previousSTime = currentSTime;  
          }
}


/*
  Since PoolScheduleMorning is READ_WRITE variable, onPoolScheduleMorningChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onPoolScheduleMorningChange(){
  // Add your code here to act upon PoolScheduleMorning change
  if (poolScheduleMorning.isActive()) {
    digitalWrite(Relay4, HIGH);
    digitalWrite(Relay2, HIGH);
    } else {
      digitalWrite(Relay4, LOW);
      digitalWrite(Relay2, LOW);
    }
}

/*
  Since PoolScheduleEvening is READ_WRITE variable, onPoolScheduleEveningChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onPoolScheduleEveningChange() {
  // Add your code here to act upon PoolScheduleEvening change
  if (poolScheduleEvening.isActive()) {
    digitalWrite(Relay4, HIGH);
    digitalWrite(Relay2, HIGH);
    } else {
      digitalWrite(Relay4, LOW);
      digitalWrite(Relay2, LOW);
    }
  }