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