Brewery - two-stage pump controller

Hi Guys n Gals,

Last year I designed and built a Brewery Controller, the brewery uses an Under Back* to automate this process I was using an sonar sensor to detect the level of ale and turn a pump on and off. Due to the amount of bubbles on the surface the readings were unreliable (some days would work fine other days would pack in) and the head brewer has been doing this process manually since.

Under Back
liquid flows in from the top, when the level reaches the top a pump is turned on, once the level reaches the bottom the pump is turned off.

I'm now going with a more simple approach of using two stainless steel float switches.

Logic as the Under back fills up ...

Bottom float switch HIGH > Pump LOW > Top float switch HIGH > Pump HIGH > Top float switch LOW > Pump HIGH > Bottom float switch LOW > Pump LOW.

I've designed a very basic circuit using two mini push buttons with pull-up resistors and an LED.

The code below works as expected but I wanted to know if there was a better way of doing it? (I'm no coder!!!!!!)

int pump = 12;                           // Pump relay is connected to pin 12
int highLevel = 11;                      // High level float switch is connected to pin 11
int lowLevel = 10;                       // Low level float switch is connected to pin 10
int valHigh;                             // Variable for reading highLevel pin status
int valLow;                              // Variable for reading lowLevel pin status

void setup() {
  pinMode(pump, OUTPUT);                 // Set the Pump pin as output
  pinMode(highLevel, INPUT);             // Set the High level float switch pin as input 
  pinMode(lowLevel, INPUT);              // Set the Low level float switch pin as input
}
  void loop(){
    
   valLow = digitalRead(lowLevel);       // Read Low level float switch input value and store it in valLow
    if (valLow != LOW) {                 // Check if the Low level float switch is floating
     valHigh = digitalRead(highLevel);   // Read High level float switch input value and store it in valHigh
      if (valHigh != LOW) {              // Check if the High level float switch is floating
       digitalWrite(pump, LOW);          // Turn Pump off
      }
    } 
    
   valLow = digitalRead(lowLevel);       // Read Low level float switch input value and store it in valLow
    if (valLow != HIGH) {                // Check if the Low level float switch is floating
     valHigh = digitalRead(highLevel);   // Read High level float switch input value and store it in valHigh
      if (valHigh != HIGH) {             // Check if the High level float switch is floating 
       digitalWrite(pump, HIGH);         // Turn Pump on
      }
    }
}

Thanks for looking,
Ryan

Assuming HIGH means floating

void loop() {
	if (digitalRead(highLevel)) {
		digitalWrite (pump,ON)
	}
	if ( ! digitalRead(lowLevel)) {
		digitalWrite(pump,OFF)
	}
}

You can use my state machine library to clean up the logic. It is found here:
http://playground.arduino.cc/Code/SMlib

Here is the code:

#include <SM.h>

int pump = 12;// Pump relay is connected to pin 12
int highLevel = 11;// High level float switch is connected to pin 11
int lowLevel = 10;// Low level float switch is connected to pin 10

SM PumpController(Filling);//start with pump turned off

void setup() {
  pinMode(pump, OUTPUT);// Set the Pump pin as output
}

void loop(){
  EXEC(PumpController);
}//loop()

State Filling(){
  digitalWrite(pump, 0);//turn pump off
  if(digitalRead(highLevel)) PumpController.Set(Emptying);//tank is full, change state
}//Filling

State Emptying(){
  digitalWrite(pump,1);//turn pump on
  if(digitalRead(lowLevel)) PumpController.Set(Filling);//tank is empty, change state
}//Emptying

Ryan, might I suggest that you include checking for the impossible in case a switch or both get jammed? Like if the bottom switch reads level low while the top switch reads level high, sound an alarm and light an error led. Assuming that mechanical and electrical parts always work flawlessly is a great way to end up with a mess some day.

Your tanks don't have the external clear (usually glass) tubes you can check fill height with?

Hi Guys,

Thank you for the responses, I've decided to go with lar3ry's example as it is the least amount of code with a few tweaks to make it compile :stuck_out_tongue:

void loop() {
        // Arduino forum member lar3ry 
	if (digitalRead(highLevel)) {
		digitalWrite (pump,HIGH);
	}
	if ( ! digitalRead(lowLevel)) {
		digitalWrite(pump,LOW);
	}

It's not exactly following the logic because if the top float switch is on without the bottom float switch being on the pump will still turn on, I'm happy with this approach though as the bottom float switch will always be on before the top float switch anyway.

I am going to take GoForSmoke's advice and incorporate an alarm system too just to be on the safe side, to answer your question... The under back is like a 20L Pot rather than a tank so you can see in quite easily from above as it's only knee height.

nilton61 Thank you very much not had chance to fully ready your link yet but looks very promising and will be looking in to this in the future :slight_smile:

Regards
Ryan