/****************************************************************
Pumping water from bottom drainage tank using floating sensors.
*****************************************************************/
// Global Variables
int bottom_switch = 2; //bottom float switch
int top_switch = 3; //top float switch
int pump = 11; //pump motor
int current_bottom, previous_bottom; // top float switch
int current_top, previous_top; //bottom float switch
boolean current_pump, previous_pump; //pump
// Definition
bool process();
//INITIAL
void setup() {
// put your setup code here, to run once:
pinMode(bottom_switch, INPUT_PULLUP);
pinMode(top_switch, INPUT_PULLUP);
pinMode(pump, OUTPUT);
previous_top = HIGH;
previous_bottom = HIGH;
}
//LOOP
void loop() {
//INPUT
current_bottom = digitalRead(bottom_switch);
current_top = digitalRead(top_switch);
//execution
current_pump = process();
//OUTPUT
digitalWrite(pump, current_pump);
//UPDATE STATE
previous_bottom = current_bottom;
previous_top = previous_top;
previous_pump = current_pump;
}
/***********************************************************************************
Pump ON if top floating level change state from HIGH to LOW
Pump OFF if bottom floating level change status from LOW to HIGH)
If both conditions are not fulfilled than the current process state is returned.
************************************************************************************/
bool process() {
if (previous_top * current_top) //SW3 & !PREVIOUS_SW3
return HIGH;
else if (previous_bottom * !current_bottom)
return LOW;
else
return current_pump;
}