I was working on modifications to the program and found that it wasn't working as I thought it was, so I spent a lot of time fixing and debugging the cycle counter. I hope that no one else struggled with this! I thought that the loop only ran when there was a tick,oops! The count kept climbing in between ticks and so I tried using a seconds test, but that didn't work either! I got it to work like I wanted by using a boolean check. For some odd reason the EEPROM memory location (2) didn't work for me either, switching it to (3) worked. I also added more comments, and tried to clean up the code. As an aid to debugging I inserted several console print statements so that it is easy to tell what is going on. It will be easy to add a LCD to the unit, and also add voltage and current readings to infer salinity for a salt display, that's for another day, though!
Here is the new, tested code, in 2 parts -
/*Count Cycles
Intex 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 to self clean after the number of cycles set by "int Cycles =".
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 display and 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.
Change Log
4-27 changed Idle, Forward and Reverse to Mode0, Mode1 and Mode2
4-28 changed to cycle count for reversing
5-12 changed/added boolean cycle over test to fix erratic counting
and added more comments, cleaned up code, tested several times by changing
from 60 to 6 in 3 places **noted in comments** to speed up runs
*/
//==================================================================
//START with definitions and User settings
//==================================================================
#include "EEPROM.h" //so we can save the StartDelay and Run times
#include "MsTimer2.h"// so we can have timer control
//-------------------------------------------------------------------
// USER CHANGEABLE OPERATIONAL SETTINGS
//-------------------------------------------------------------------
int StartDelay = 60; //***StartUpDelay Time in Minutes*** I like an hour so that pump has stirred things up
int Cycles = 5; //Number of cycles to run before self cleaning
// INTEX used 20 hours so try to match that by dividing 20 by your needed run time
// eg. with a 1 hour run time, 20/1=20 so Cycles=20
// with a 4 hour run time, 20/4=5 so Cycles=5
// with a 5 hour run time, 20/5=4 so Cycles=4
//-------------------------------------------------------------------
//-------------------------------------------------------------------
volatile unsigned char tick;
int Mode = 1; //Cell Power Mode 0 = None, 1 = Forward, 2 = Reverse(Self-Cleaning)
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
int zero = 0; // used to reset cycle counter count
boolean CycleOver = false; // Use to add to saved cycle count
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 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; // variables 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
int CyRead = 0; // Count of number of cycles already run
int MoRead = 0; //Stored Power Mode to opererate cell in
//=========================================================================
//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);
}
//----------------------------------------------------------------