Here's part 2 -
//=============================================================
// Setup Routine Information
//=============================================================
void setup() // run once, when the sketch starts
{
Serial.begin(9600); // start up serial communications
pinMode(FlowPin, INPUT); // sets the digital pin as input
pinMode(UpButtonPin, INPUT);
pinMode(DnButtonPin, INPUT);
pinMode(K1Pin, OUTPUT); // sets the digital pin as output
pinMode(K2Pin, OUTPUT); // sets the digital pin as output
pinMode(K3Pin, OUTPUT); // sets the digital pin as output
pinMode(FanPin, OUTPUT); // sets the digital pin as output
pinMode(RpwrPin, OUTPUT);
pinMode(LpwrPin, OUTPUT);
for (int i = 0; i < 8; i++)
pinMode(ledPin[i], OUTPUT); // sets the digital pin as output
digitalWrite(UpButtonPin, HIGH); //Turn on the pull up resistors
digitalWrite(DnButtonPin, HIGH); //Turn on the pull up resistors
digitalWrite(FlowPin, HIGH); //Turn on the pull up resistors
display_time(); // Show initial time
Mode0(); //Set relays OFF
// Get ct from the EEPROM
ctRead = EEPROM.read(0);
delay(100);
Rcount = ctRead; //**ERROR** MUST comment out the first time if a value has not ever been stored before!!!!
MsTimer2::set(1000, increment_time);// every second increment_time() is called
MsTimer2::start();
}
//==================================================================
//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) + (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);}
MoRead = EEPROM.read(1); //Read what Cell Power Mode to use
if (MoRead == 1) {Mode1();
}
else {Mode2();
}
} //Generate Chlorine in Last Saved Mode for this cycle
if (minutes > ((CycleTime) + (StartDelay))) {
Mode0();
Mode = 3; //Cycle completed, go back to waiting
CyRead = EEPROM.read(2); //Read how many cycles have been run before
CyRead ++; //And add 1 to it
if (CyRead >= Cycles) //Next Time self clean
{if (MoRead ==2)
{MoRead = 1;}
else {MoRead = 2;}
EEPROM.write(1, int(MoRead)); //Write the Mode to use next time to memory
EEPROM.write(2, 0); //Set the cycles run since self cleaning to 0
}
else {EEPROM.write(2, (CyRead));} // Write the updated cycle count to memory
}
else {
Mode0();
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 Mode0() { // CELL is IDLED - No Power
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
}
//______________________________________________________________
// CHLORINE GENERATION POWER MODES
//______________________________________________________________
void Mode1() { // FORWARD 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 Mode2() { //REVERSE polarity for SELF CLEANING
Mode = 2; //Apply REVERSE Polarity power to cell
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
}
I hope that you find this useful!
I welcome any comments/suggestions on how to improve this project, what I am after is a low cost, easy to use, effective Electronic Chlorine Generation system for use in residential pools.