Ro water need help

Hello,
I am trying to auto make Ro water and since I have no experience with Arduino, the code seems a little hard.

So far I this is what I want to accomplish and what I have.

/*
dual float switch, 3 solenoids

the circuit:
Upper float switch LOW, nothing happens.
Lower float switch LOW, opens solenoid 1 and 2
to flush RO membrane for 3 min.
Then solenoid 1 closes and solenoid 2 stays open for 1 minute
to divert TDS water to waste.
Then solenoid 2 closes and solenoid 3 opens to
make good water. Water continues to run until both float switches
are LOW.
Then solenoid 3 closes, solenoid 1 and 2 open and run for 5 mins
to flush membrane. Then all silenoids are closed.

*/

// constants won't change. They're used here to
// set pin numbers:
const int UpperFloat = 2; // digital pin of upper float switch
const int LowerFloat = 3; // digital pin of lower float switch
const int Solenoid1 = 5; // digital pin of DI solenoid 1 main water solinoid
const int Solenoid2 = 6; // digital pin of RO drain solenoid 2
const int Solenoid3 = 5; // digital pin of clean water solenoid 3

// variables will change:
int buttonState1 = 0; // variable for reading the pushbutton status
int buttonState2 = 0; // variable for reading the pushbutton status

void setup() {
// initialize the Solenoid pin as an output:
pinMode(Solenoid1, OUTPUT);
pinMode(Solenoid2, OUTPUT);
pinMode(Solenoid3, OUTPUT);
// initialize the Float switch as an input:
pinMode(UpperFloat, INPUT_PULLUP);
pinMode(LowerFloat, INPUT_PULLUP
);
}

void loop(){
// read the state of the float switches:
buttonState1 = digitalRead(UpperFloat);
buttonState2 = digitalRead(LowerFloat);

if (buttonState1 == LOW && buttonState2 == LOW) {
digitalWrite(Solenoid1, HIGH);
digitalWrite(Solenoid2, HIGH);
digitalWrite(Solenoid3, LOW);
delay(10000); // FLUSH FOR 3 MIN
digitalWrite(Solenoid1, LOW);
digitalWrite(Solenoid2, HIGH);
digitalWrite(Solenoid3, LOW);
delay(10000); // 1 minute to divert TDS water

HERE IS THE ISSUE, I WANT ONLY SOLENOID 3 TO STAY HIGH UNTIL BOTH FLOAT SWITCHES
ARE LOW, (not timed)THEN

digitalWrite(Solenoid1, HIGH);
digitalWrite(Solenoid2, HIGH);
digitalWrite(Solenoid3, LOW);
delay(10000); // WAIT FOR FIVE MINUTES FOR FINAL FLUSH

then all solenoids LOW

Any help will be appreciated.

Managing Hi and lo float switches in a tank that needs to empty and fill is a classic pump control problem and you can apply the same approach used for that.

In short, I think your problem is to latch the operation of solenoid 3 while resetting (switching off) solenoids 1 & 2.

Pump controls use a common latching circuit (external to you software to accomplish this with one chip (4093 - quad 2 input NAND) set up to latch and release (you will only need that function on your lower switch (although you could use it on both and simplify the code).

There is a swack-load of help on google if you search for "4093 pump control". What you implement will depend on your external circuit design, so you could post detail of that or go google and see what looks like it will work. Happy to help if you want further. I've been building and repairing these things for years!

Have a look at this chart and please let me know if it is correct as far as you can tell. I need to know what state the float switches are in during each process, so I can assess what state change needs to happen at the event boundary where your problem is.

Your explanation is very good and makes understanding the process easy, thanks. Once I know the switch settings clear, I can give you a complete state chart and we can make your code work. I think we may be able to avoid the external latching hardware I mentioned earlier, but it does depend on the switch states at the event boundaries. Please look carefully at them and complete the missing parts...

State chart.doc (49.5 KB)

steve_mcdonald:
Have a look at this chart and please let me know if it is correct as far as you can tell. I need to know what state the float switches are in during each process, so I can assess what state change needs to happen at the event boundary where your problem is.

Your explanation is very good and makes understanding the process easy, thanks. Once I know the switch settings clear, I can give you a complete state chart and we can make your code work. I think we may be able to avoid the external latching hardware I mentioned earlier, but it does depend on the switch states at the event boundaries. Please look carefully at them and complete the missing parts...

Steve,
For some reason I did not check back in the forum. See attachment with updated chart. In regards to the hardware, what I have is a UNO and a 4ch relay shield

I hope you are still around after all this time and can help me.

Thank you

State chart.doc (51 KB)

Hi,
Still around! I will look in detail at the state chart you updated in the next day o r so if I can. I am going to be off grid for a couple of weeks on holiday after tomorrow, so forgive me if you don't get an answer for a little while.

I've added some notes to the state chart.

Please check if I've correctly understood what is happening at the end of event 5. If not, please correct.

Everything else is clear, except for what triggers event 4. During the previous state, the lower float is lo and remains lo. The upper float is either hi or lo and goes hi. If it is lo and goes hi we can capture that. If it was hi we have nothing to capture, so what would trigger the next state change?

RO Water State chart.doc (55 KB)

Hi,
I've written code that compiles and may do what you need, but I don't understand what the Upper Float switch is doing. We never seem to read it or respond to it. Let me know what I'm missing here...

/* dual float switch, 3 solenoids

the circuit:
Upper float switch LOW, nothing happens.
Lower float switch LOW, opens solenoid 1 and 2
to flush RO membrane for 3 min.
Then solenoid 1 closes and solenoid 2 stays open for 1 minute
to divert TDS water to waste.
Then solenoid 2 closes and solenoid 3 opens to
make good water. Water continues to run until both float switches
are LOW.
Then solenoid 3 closes, solenoid 1 and 2 open and run for 5 mins
to flush membrane. Then all solenoids are closed.

 */

// constants won't change. They're used here to set pin numbers:
const byte UpperFloat = 2;        // digital pin of upper float switch
const byte LowerFloat = 3;        // digital pin of lower float switch
const byte Solenoid1 =  5;        // digital pin of DI solenoid 1 main water solenoid
const byte Solenoid2 =  6;        // digital pin of RO drain solenoid 2
const byte Solenoid3 =  5;        // digital pin of clean water solenoid 3
const unsigned int delay1 = 1000; // set up 1 minute delay
const unsigned int delay3 = 3000; // set up 3 minutes delay
const unsigned int delay5 = 5000; // set up 5 minutes delay

// variables will change:
bool upperFloatState = 0;         // variable for reading the float status
bool lowerFloatState = 0;         // variable for reading the float status

void setup() {
  // initialize the Solenoid pins as output:
  pinMode(Solenoid1, OUTPUT);
  pinMode(Solenoid2, OUTPUT);
  pinMode(Solenoid3, OUTPUT);
  // initialize the Float switches as input:
  pinMode(UpperFloat, INPUT_PULLUP);
  pinMode(LowerFloat, INPUT_PULLUP);

// Set initial state
  digitalWrite(Solenoid1, LOW);
  digitalWrite(Solenoid2, LOW);
  digitalWrite(Solenoid3, LOW);
}

void loop(){
  // Test switches - do nothing until lower float goes LOW
  while(lowerFloatState == HIGH) {
  }
  flushRO();
  divertTDS();
  makeWater();
  flushMembrane();
}

void flushRO(){          
  // Flush RO membrane 3 minutes
  digitalWrite(Solenoid1, HIGH);
  digitalWrite(Solenoid2, HIGH);
  digitalWrite(Solenoid3, LOW);
  delay(delay3);
}

void divertTDS(){        
  // Divert TDS water to waste 1 minute
  digitalWrite(Solenoid1, LOW);
  digitalWrite(Solenoid2, HIGH);
  digitalWrite(Solenoid3, LOW);
  delay(delay1);
  }

void makeWater(){
  // make good water until both inputs LOW
  while (!(upperFloatState == LOW && lowerFloatState == LOW)){
    digitalWrite(Solenoid1, LOW);
    digitalWrite(Solenoid2, LOW);
    digitalWrite(Solenoid3, HIGH);
  }
}

void flushMembrane(){
/*Then solenoid 3 closes, solenoid 1 and 2 open and run for 5 mins
to flush membrane. Then all solenoids are closed.
*/
    digitalWrite(Solenoid1, HIGH);
    digitalWrite(Solenoid2, HIGH);
    digitalWrite(Solenoid3, LOW);
    delay(delay5);
    digitalWrite(Solenoid1, LOW);
    digitalWrite(Solenoid2, LOW);
    digitalWrite(Solenoid3, LOW);
}