PH controller

I'm trying to combine an existing relay program, with a PH sensor, this this getting complicated Uggg.
I want the PH controller to fire up in case 6 then again in the rest of the cases, then the whole thing to restart in a week.
so close yet so far, I need help in a big way....

here's what I have

//  Some macros for defining time intervals in milliseconds
#define seconds_in_ms(s) ((s)*1000UL)
#define minutes_in_ms(m) ((m)*60UL*1000UL)
#define hours_in_ms(h)   ((h)*60UL*60UL*1000UL)
#define days_in_ms(d)    ((d)*24UL*60UL*60UL*1000UL)
#define weeks_in_ms(w)   ((w)*7UL*24UL*60UL*60UL*1000UL)
#define PHIN 14

unsigned long CycleStartTime = 0;
unsigned long LastTaskTime = 0;
unsigned long CurrentTaskInterval = 0;
unsigned Task = 0;
 

void setup() {
 
  pinMode(PHIN,INPUT);
  Serial.begin(9600);
  Serial.println("Welcome SW-02-010A");
   pinMode(2, OUTPUT);   //PH down
   pinMode(3, OUTPUT);  //B nutrient solution
   pinMode(4, OUTPUT);  //A nutrient solution
   pinMode(5, OUTPUT);  //Drain pump
   pinMode(6, OUTPUT);   //Solenoid Valve
   pinMode(7, OUTPUT);  //Nutrient Heater

  digitalWrite(2, HIGH);     //PH down off
  digitalWrite(3, HIGH);  //B nutrient solution off
  digitalWrite(4, HIGH);  //A nutrient solution off
  digitalWrite(5, HIGH);  //Drain pump off
  digitalWrite(6, HIGH);   //Solenoid Valve off
  digitalWrite(7, HIGH);  //Nutrient Heater off
}
  
void loop() 
{
  int phRaw;
  float phTmp, phOut, phMiliVolts;
  
  if(Serial.available())
  {
  phRaw = readADC(PHIN, 16666);    //Our averaged raw ADC value over ~ a single 60hz cycle
  float phTmp = (5*(float)phRaw)/1023;   //Convert reading into a voltage based on a 5v reference
  float phMiliVolts = phTmp * 1000;      //convert to milivolts
  phTmp = (2500-phMiliVolts)/5.25; //Since our circuit contains an initial gain, we must remove it here
  float phOut = 7-(phTmp/59.16);            //ph is the numbers of "steps" from 7 (a step = mV/steps_per_mV)
  Serial.print("ADC1 Raw: ");
  Serial.println(phRaw);
  Serial.print("ADC1 Milivolts: ");
  Serial.println(phMiliVolts);
  Serial.print("pH: ");
  Serial.println(phOut);
  delay(1000); //1 sec
}
  unsigned long currentTime = millis();

  // If the time has not yet come to perform a task
  if (currentTime - LastTaskTime < CurrentTaskInterval)
    return;   // Nothing to do

  LastTaskTime = currentTime;

  switch (Task++)
  {
  case 0:
    CycleStartTime = currentTime;  // Remember this time

   digitalWrite(5,LOW);   // set the drain pump on
    
    CurrentTaskInterval = minutes_in_ms(15);
    break;


  case 1:
    digitalWrite(5,HIGH);   // set the drain pump off
    digitalWrite(6, LOW); // set the Solenoid Valve on
    digitalWrite(2, LOW);  // set the PH down on
    digitalWrite(3, LOW); // set the B nutrient solution on
    digitalWrite(4, LOW); // set the A nutrient solution on
    CurrentTaskInterval = seconds_in_ms(7);
 break;
 

case 2:
    digitalWrite(2, HIGH);  // set the PH down off
    digitalWrite(6, LOW); // set the Solenoid Valve on 
    digitalWrite(3, LOW); // set the B nutrient solution on
    digitalWrite(4, LOW); // set the A nutrient solution on
    CurrentTaskInterval = minutes_in_ms(6);

break;
 

case 3:
digitalWrite(3, HIGH); // set the B nutrient solution off
digitalWrite(6, LOW); // set the Solenoid Valve on
digitalWrite(4, LOW); // set the A nutrient solution on
    CurrentTaskInterval = minutes_in_ms(5.03);


break;

case 4:
digitalWrite(4, HIGH); // set the A nutrient solution off
digitalWrite(6, LOW); // set the Solenoid Valve on

    CurrentTaskInterval = minutes_in_ms(5);


break;


case 5:
digitalWrite(6, HIGH); // set the Solenoid Valve off
digitalWrite(7, LOW); // set the Nutrient Heater on

CurrentTaskInterval = days_in_ms(1);

break;

case 6:
digitalWrite(6, LOW); // set the Solenoid Valve on/ top up
delay (5000);
digitalWrite(6, HIGH); // set the Solenoid Valve off/ top up
}


  unsigned int readADC(int channel, unsigned reading_time)
  {
  double d;
  int i;
  unsigned long t0_us;
  d = 0.0;
  i = 0;
  t0_us = micros();
  while((micros()-t0_us)<reading_time){
    i++;
    d += analogRead(channel);
  }
  d /= i;
  return (unsigned int)(d);  
}


CurrentTaskInterval = days_in_ms(1);

break;

case 7:
digitalWrite(6, LOW); // set the Solenoid Valve on/ top up
delay (5000);
digitalWrite(6, HIGH); // set the Solenoid Valve off/ top up
CurrentTaskInterval = days_in_ms(1);

break;

case 8:
digitalWrite(6, LOW); // set the Solenoid Valve on/ top up
delay (5000);
digitalWrite(6, HIGH); // set the Solenoid Valve off/ top up
CurrentTaskInterval = days_in_ms(1);

break;

case 9:
digitalWrite(6, LOW); // set the Solenoid Valve on/ top up
delay (5000);
digitalWrite(6, HIGH); // set the Solenoid Valve off/ top up
CurrentTaskInterval = days_in_ms(1);

break;

case 10:
digitalWrite(6, LOW); // set the Solenoid Valve on/ top up
delay (5000);
digitalWrite(6, HIGH); // set the Solenoid Valve off/ top up
CurrentTaskInterval = days_in_ms(1);

break;

case 11:
digitalWrite(6, LOW); // set the Solenoid Valve on/ top up
delay (5000);
digitalWrite(6, HIGH); // set the Solenoid Valve off/ top up
CurrentTaskInterval = days_in_ms(1);

break;


// Start over again in one week from the START of the cycle
    Task = 0;
    LastTaskTime = CycleStartTime;
    CurrentTaskInterval = weeks_in_ms(1);
    break;
  }

so close yet so far

No, you are not close.

  unsigned int readADC(int channel, unsigned reading_time)
  {
  double d;
  int i;
  unsigned long t0_us;
  d = 0.0;
  i = 0;
  t0_us = micros();
  while((micros()-t0_us)<reading_time){
    i++;
    d += analogRead(channel);
  }
  d /= i;
  return (unsigned int)(d);  
}

You can't define a function INSIDE another function.

// Start over again in one week from the START of the cycle
    Task = 0;
    LastTaskTime = CycleStartTime;
    CurrentTaskInterval = weeks_in_ms(1);
    break;

You can't have code in a switch statement that is not part of a case block.

  switch (Task++)

Why is Task being incremented on every pass through loop?

1)"You can't define a function INSIDE another function." Ok so whats a simple solution, write it into a new case block?
2)"You can't have code in a switch statement that is not part of a case block." Ok I buy that, but the original program was like that, and worked I can fix it too
3)"Why is Task being incremented on every pass through loop?" IDK maybe because I never noticed it B4 and suppose I can fix that too

Thanks for the help, this is a little over my head,it started out simple and got complicated.

Ok so whats a simple solution, write it into a new case block?

Defining a function is separate from calling a function. Look at how loop() and setup() are written. loop() is not inside setup() and setup() is not inside loop. Even main(), that calls init(), setup() and loop() in an endless loop() is a separate function.

void setup()
{
  // Some stuff goes here
}

void loop()
{
  // Some stuff goes here
  unsigned int val = readADC(somePin, someTime);
}

unsigned int readADC(int channel, unsigned reading_time)
{
   // Some stuff goes here
}

Ok I buy that, but the original program was like that, and worked I can fix it too

I doubt that the original program was like that.

Ok still don't get it, i understand loop and setup ect.
I still don't see where I went wrong, are you saying it should be defined in setup I don't think it needs to.
especially since I don't want it to fire up until at least case 6
which is why I think maybe I need a new case point for it to fire up into
lost in space here
sorry I'm better with the wires then the computers

Ok maybe I see it, I'm missing the line "unsigned int val = readADC(somePin, someTime);"
Is that it, or am I still on pluto? pin should be A0 and time not sure forever I guess
still not sure where to put this

Do you have a link to the original code?