PH controller help

I'm trying to piece two programs together a PH sensor and a relay program.
grrrr not so easy here's what i got so far full of errors of course
any help; well would really help

 //  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  A0

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
  phTmp = (5*(float)phRaw)/1023;   //Convert reading into a voltage based on a 5v reference
  phMiliVolts = phTmp * 1000;      //convert to milivolts
  phTmp = (2500-phMiliVolts)/5.25; //Since our circuit contains an initial gain, we must remove it here
  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();
  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);  
}
  // 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
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;
  } 
}

You have too many '}'s, but I can't work out which one is the extra one.

Did you mean this if to do nothing?

  if(Serial.available() )
  {

}

There were a couple uninitialized variables, and subroutine was out of place... Maybe a couple more errors. I didn't go through the flow of the code to make sure the logic was in order, however it at least compiles now, and should get you headed in the right direction.

//  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
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;
  } 
}


  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);  
}

I am in your debit, so bad; Ive been trying 4 a couple hours to get this working.
oh ya one other small thing would help, I need a if then like statement, "if the PH is X then do Y"
I owe you guys a beer.

perkunas:
I am in your debit, so bad; Ive been trying 4 a couple hours to get this working.
oh ya one other small thing would help, I need a if then like statement, "if the PH is X then do Y"
I owe you guys a beer.

Thanks, and good luck!

if(pH == X)
  {
    Serial.println(Y);  //insert whatever command you want here
  }

Looks like the the flow is wrong
I'm trying to integrate it into case 6 this looks like it's going to start multitasking and that's a no no from what I can tell there is more that needs to be done in the program. So i think a simple solution is to have the PH run for just like 5 min. Then move on, the problem I can see is the code unsigned_time maybe it has to be rewritten for like 5min. or am I off base here?

}


  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);  
}