/*
Intex 6110/8110 Electronic Salt Water Chlorine Generator Control
***Program Description***
When powered ON will delay for a "StartDelay" time in minutes.
While in delay Flashing HOURS of RUN TIME can be adjusted UP or DOWN
(with the /\ and \/ buttons) until "StartDelay" is complete. Display will blank
for one minute and then display remaining HOURS of RUN TIME while
powering the Chlorine Generator Cell for the set HOURS of RUN TIME,
reversing the polarity 1/2 thru to self clean. If FLOW Switch does not
sense water flow through the cell, the display will BLANK, and Chlorine
production will halt until flow resumes.
***Intex Hardware Description***
The Input/Display board is re-used with minor modifications, while the
Main Control board is modified to retain only the relays and power parts.
The Arduino replaces the Sonix mcu, which was removed, as were most other
components not used for the relays or display. The display LEDs use 1k resistors,
the input buttons and flow detector switch use the internal pull-up resistors,
the relays are powered thru 1n1001 transistors.
Relay K1 connects (+) power from the rectifier to K2 & K3 relays NC terminals
and +12V power to their coils. Power flows from the wall plug -> the transformer,
-> the rectifier, -> relays K2 and K3, -> through Fuses F1 and F2, -> cell.
Relays K2 and K3 LOW =Cell IDLE.
K2 LOW and K3 HIGH = FORWARD (normal) polarity.
K2 HIGH and K3 LOW = REVERSE (cleaning) polarity.
FanPin controls the rectifier cooling fan = HIGH when cell is powered.
FLOW Switch is NORMALY OPEN, CLOSES with GOOD FLOW, by using internal PULL
UP resistors, GOOD FLOW = 0. BAD FLOW = 1 will blank LED displayand IDLE cell.
UP and DOWN BUTTONs allow setting RUN TIME from 1-9 hours (60-540 minutes)
which is stored in EEPROM memory, safe from power loss.
Minute counter resets at 1440 minutes (24 hours) if power is left ON.
*/
//==================================================================
//START with definitions
//==================================================================
#include "EEPROM.h" //so we can save the StartDelay and Run times
#include "MsTimer2.h"// so we can have timer control
int StartDelay = 60; //***StartUpDelay Time in Minutes*** I like an hour so that pump has stirred things up
volatile unsigned char tick;
int Mode = 0;
int seconds = 0;
int minutes = 0; // Inititialise actual values for m,s
int hours = 0; //for Run time Display
int CycleTime = 0; //This will be the Total Chlorine Generation Time
const int DnButtonPin = 17; //DOWN Pushbutton uses internal PULL UP resistor
const int UpButtonPin = 16; //UP Pushbutton uses internal PULL UP resistor
const int debounce = 80;
//int HRPinState = 0; //Variab;e for reading the HourPin Status
int FlowPin = 15; // Flow switch uses internal PULL UP resistor GOOD FLOW =0
int FlowState = 0; // variable for reading the FLOWstatus
int K1Pin = 9; //Relay k1 connected to digital pin 3 for power
int K2Pin = 10; //Relay k2 connected to digital pin 4 and Cell (+)
int K3Pin = 11; //Relay k3 connected to digital pin 5 Cell (-)
int FanPin = 12; //Relay k3 connected to digital pin 6 Rectifier Cooling Fan
int ledPin[7] = {2, 3, 4, 5, 6, 7, 8}; // 7-segment LEDs
int RpwrPin = 19; //Power for Right 7-segment LED
int LpwrPin = 18; // Power for Left 7-segment LED
int UpbuttonState1 = 1; // variable for reading the pushbutton status
int UpbuttonState2 = 1;
int DnbuttonState1 = 1;
int DnbuttonState2 = 1;
int Rcount = 4; //value for the Right LED
int Lcount = 0; //Value for the Left LED
int ctRead = 0; // Count value read from the EEPROM
//Segment to Pin assignments for the 7 segment LEDs
// Adjust this for your 7 segment LED spec.
// A, B, C, D, E, F, G
int Seg2Pin[8] = { 2, 3, 4, 5, 6, 7, 8};
//Digit to Segment You don't need modify this unless you want to change digits.
int Dec2Seg[11] =
{
//A(125), B(64), C(32), D(16), E(8), F(4), G(2)
0x07E, //Digit 0
0x00C, //Digit 1
0x0B6, //Digit 2
0x09E, //Digit 3
0x0CC, //Digit 4
0x0DA, //Digit 5
0x0FB, //Digit 6
0x00E, //Digit 7
0x0FF, //Digit 8
0x0DF, //Digit 9
};
//==================================================================
//Global Functions and Descriptions
//==================================================================
void clearAllSegments() //Turns off all LED segments
{
for (int i = 0; i < 8; i++)
{
digitalWrite(ledPin[i], HIGH);
}
}
//-----------------------------------------------------------------
void drawDigit(int digit) //Draw digit to LED outputs
{
int seg = Dec2Seg[digit];
int pin = 0;
while (seg > 0)
{
seg /= 2;
if (seg & 0x1)
digitalWrite(Seg2Pin[pin], LOW); // sets the LED on
pin++;
}
}
//-----------------------------------------------------------------
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
}
}
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
Idle(); //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