Pool Salt Water Chlorine Generator

here is part 2, beginning with the end of part 1

//-----------------------------------------------------------------

void display_time () {          // Function to display the time to console
//Serial.print(hours);
//Serial.print(":");
Serial.print(minutes);
Serial.print(":");
Serial.println(seconds);
}

//----------------------------------------------------------------

void increment_time() {             //Second counting timer function
    seconds++;                      // We're counting UP
    if (seconds > 59) {                  // If seconds have rolled over
        seconds = 0;                  //    Reset seconds
        minutes++;                  //    and increment the minutes
        if (minutes > 1439) {            // Minutes in a Day have rolled over
            minutes = 0;            //    Reset minutes
                boolean CycleOver = false;    // Reset for saved cycle count        
}
}
   
    tick++;                             // indicate that the time has been updated
}

//----------------------------------------------------------------

void SetTime(){
UpbuttonState1 = digitalRead(UpButtonPin); // read the state of the pushbutton value:
if (UpbuttonState1 == LOW){
  delay(debounce); //debounce
  UpbuttonState2 = digitalRead(UpButtonPin); // check if the pushbutton is pressed, if it is, the buttonState is LOW:
    if (UpbuttonState2 ==LOW ) {     
      if (Rcount <= 8)    //MAX Time is 9
        {Rcount++;}
      else {Rcount = 1;}  //MIN Time is 1
      }
  else {
      ;  }
    } 
else {
    ; 
}
  
DnbuttonState1 = digitalRead(DnButtonPin);
if (DnbuttonState1 == LOW){
  delay(debounce);   //debounce
  DnbuttonState2 = digitalRead(DnButtonPin);  // check if the pushbutton is pressed, if it is, the buttonState is HIGH:
    if (DnbuttonState2== LOW) {     
      if (Rcount >= 2)  //MIN Time is 1
      {Rcount--;}
       else {Rcount = 9;}  //MAX Time is 9
    } 
  else {
      ;}
 }
 else {
      ;}

//Now Display count value on LED
  digitalWrite (LpwrPin, LOW); //for the display
  digitalWrite (RpwrPin, LOW); //for the display
   drawDigit(Rcount);
  digitalWrite (RpwrPin, HIGH); //for the display
   delay(1);                  // waits for a second
    clearAllSegments();
  digitalWrite (RpwrPin, LOW); //for the display
   delay(1);
  digitalWrite (LpwrPin, HIGH); //for the display
   drawDigit(Lcount);
   delay(1);                  // waits for a second
  digitalWrite (LpwrPin, LOW); //for the display
  clearAllSegments();
}

//=============================================================
// 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!!!!
  MoRead = EEPROM.read(1);                //Read what Cell Power Mode to use
  delay(100);
  CyRead = EEPROM.read(3);              //Read how many cycles have been run before ?? eeprom.read(2) didn't work??
  delay(100);
  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 prints to the Serial Monitor Window on the cansole for Debugging
        //purposes. Program changes based on mihutes will occur with the next minute change,
        //it does not look at seconds, so 1m01s and 1m 59s are not greater then 1, but 2m is.
        Serial.println(""); // This prints to the console for debugging
        Serial.print("Time since power ON ");         //    reset indicator that we have a new time
      display_time();  //    and show it off!
        Serial.print("Saved Cell Mode is ");
        Serial.println(MoRead);
        Serial.print("Saved Cycle Count is ");
        Serial.println(CyRead);
        Serial.print("Run Time = ");
        Serial.println(hours);
        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 ** change 60 to 6 below to speed up**
          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  ** change 60 to 6 above to speed up**
        }
        
        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();
   }
                                                        //**change 60 to 6 below to speed up**
if (minutes == (StartDelay)) {CycleTime = Rcount * 60; // Start-up delay is over, Now use Input Count for Cycle Time
      if (Rcount != ctRead)                   //Was Count for Hours of Operation changed?
      {EEPROM.write(0, int(Rcount));      // Then Write NEW count to the EEPROM
       delay(100);}
}

if (minutes > (StartDelay) && minutes < ((CycleTime) + (StartDelay))) {            
               
                if (MoRead == 1) {Mode1();
              }
                else {Mode2();
              }
  }            //Generate Chlorine in Last Saved Mode for this cycle

if (minutes >= ((CycleTime) + (StartDelay)))    //Generation cycle is over
 {
   Mode0();
   Mode = 3;    //Cycle completed, go back to waiting
   //One time limiter goes here
   while (CycleOver == false)   // Use to add  to saved cycle count
      {
      CyRead=CyRead + 1;                    //And add 1 to Cycle Count read fromm EEPROM
      if (CyRead >= Cycles)    //Next Time self clean
          {
            if (MoRead ==2)
              {
            MoRead = 1;}
          else {MoRead = 2;
                }
           EEPROM.write(1, (MoRead));    //Write the Mode to use next time to memory
           delay(100);
           CyRead = zero;
           EEPROM.write(3, (zero));    //Set the cycles run since self cleaning to 0  
           delay(100);
           }
     else {EEPROM.write(3, (CyRead));  // Write the updated cycle count to memory
     delay(100); 
          }
 CycleOver = true;    // Used to only add once
 }
 }
}
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 rectifier
 }
//______________________________________________________________
//         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
  }