//Main Program LOOP
//==================================================================
void loop(){
if (tick) { // If a tick has occurred
tick = 0;
Serial.println(""); // This prints to the console for debugging
Serial.print("Time is "); // reset indicator that we have a new time
display_time(); // and show it off!
Serial.print("Flow Status = ");
if (FlowState == 0) {Serial.println("GOOD FLOW");}
if (FlowState == 1) {Serial.println("BAD FLOW");}
Serial.print("Cell Power Mode is ");
if (Mode == 0) {Serial.println("WAITING");}
if (Mode == 1) {Serial.println("FORWARD"); // Make Chlorine, show Run Time remaining
digitalWrite (RpwrPin, HIGH); //Turn ON the display
hours=(Rcount -((minutes - (StartDelay)) / 60)); //Calculate how many Run Time Hours are left
drawDigit(hours); //Show remaining Run Time Hours
}
if (Mode == 2) {Serial.println("REVERSE");
hours=(Rcount -((minutes - (StartDelay)) / 60)); //Calculate how many Run Time Hours are left
drawDigit(hours); //Show remaining Run Time Hours
}
if (Mode == 3) {Serial.println("DONE");
digitalWrite (RpwrPin, LOW); //Turn Off the display
}
}
// While there is GOOD FLOW run the program
FlowState = digitalRead(FlowPin); //Check for GOOD FLOW
if (FlowState == 0){
//If minutes < delay, then check for button input to change run time
if (minutes < (StartDelay)) {
Mode = 0;
SetTime();
}
if (minutes == (StartDelay)) {CycleTime = Rcount * 60;} // Start-up delay is over, Now use Input Count for Cycle Time
if (minutes > (StartDelay) && minutes < ((CycleTime / 2) + (StartDelay))) {
if (Rcount != ctRead) //Was Count for Hours of Operation changed?
{EEPROM.write(0, int(Rcount)); // Then Write NEW count to the EEPROM
delay(20);}
ForwardPower();
} //Generate Chlorine in Normal Mode for ?? minutes
if (minutes > ((CycleTime / 2) + (StartDelay)) && minutes < ((CycleTime) + (StartDelay))) {
ReversePower();
} // Generate Chlorine in Reverse Mode for ?? minutes
if (minutes > ((CycleTime) + (StartDelay))) {
Idle();
Mode = 3;
}
}
else {
Idle();
Mode = 0; //BAD FLOW sit Idle WAITING
digitalWrite (RpwrPin, LOW); // And Turn Off the LED display
}
}
//===========================================================
// Cell Relay Power Control Functions to Generate Chlorine
//===========================================================
void Idle(){
digitalWrite(K1Pin, HIGH); // Connects +5V & +24V power to K2 & K3
digitalWrite(K2Pin, LOW); // CELL IDLE - Connects Cell (+) to +24V
digitalWrite(K3Pin, LOW); // CELL IDLE - Connects Cell (-) to +24V
//when done turn the fan off
digitalWrite(FanPin, LOW); // sets the FAN off, after cooling the rectif
}
void ForwardPower(){ // Generate Chlorine by putting power to cell
Mode = 1;
// Apply Positve polarity power to cell
digitalWrite(K3Pin, HIGH); // NORMAL POLARITY - Connects Cell (-) to GROUND
//Turn the fan ON to keep things cool
digitalWrite(FanPin, HIGH); // sets the FAN on to cool the rectifier
}
void ReversePower(){ //REVERSE polarity for SELF CLEANING
Mode = 2;
digitalWrite(K2Pin, HIGH); // REVERSE POLARITY - Connects Cell (+) to GROUND
//Turn the fan ON to keep things cool
digitalWrite(FanPin, HIGH); // sets the FAN on to cool the rectifier
}