// Water Tank Simulation by D.Weerasinghe COTM
// This Simulation has 4 Level Sensors LLL,LL, HL, HHL pins D7 to D10 (Outputs)
// D6 is overflow indicator (simulated output)
// D2 is Pump run pin (input)
// D3 is Input pin simulates water not flowing (pump dry run) state (Input Hig to simulate dry run)
// D13 is pump running status indicator(output)
// D11 is pump Flow sensor (output)
#define pumpRun 2
#define pumpInd 13
#define HHLPin 10
#define HLPin 9
#define LLPin 8
#define LLLPin 7
#define OvFlwPin 6
#define pumpFlowsensor 11
#define noPumpFlow 3
#define printTimeout 5000
#define lvlupdatTimeout 5000
#define tankLevelRefresdly 300
#define maxTanklevel 1000
#define pumpFlowSensorDelay 2000
#define lowLevel 0.2
#define lowlLowLevel 0.1
#define highLevel 0.8
#define highHighLevel 0.9
#define pumpRate 12
#define flowRate 10
#define printPumpState 0 // 1 for true 0 for false
#define graphMode 1 // 0 to enable/view Consumption rate, 1 to disable/hideview consumption rate
//boolean motorStart = false;
//boolean motorStop = false;
boolean pumpRunning = false;
boolean pump_State = false;
boolean pump_Flow = false;
boolean overflow = false;
boolean LLL = false;
boolean LL = false;
boolean HL = false;
boolean HHL = false;
boolean Pump_Flow_Start = false;
boolean Pump_Flow_Sensor = false;
int tankLevel = 0;
int tankPrvLevel = 0;
int consumptionRate = 0;
int consumptionvalve =0;
int outflowrate = 0;
int prvConsumpRate = 0;
int pumpflow = 0;
unsigned long printTicks;
unsigned long lvlupdatTicks;
unsigned long PumpflowStart;
String pumpState = "Pump Stopped";
void setup() {
// put your setup code here, to run once:
pinMode(pumpRun, INPUT);
pinMode(pumpInd, OUTPUT);
for(int piN=6; piN<12; piN++ ){
pinMode(piN, OUTPUT);
}
Serial.begin(9600);
printTicks = millis();
lvlupdatTicks = millis();
}
void loop() {
// put your main code here, to run repeatedly:
pump_State = digitalRead(pumpRun);
pump_Flow = not(digitalRead(noPumpFlow));
consumptionvalve = map(analogRead(A0),0,1023,0,flowRate);
delay(5);
if (pump_State == true){
digitalWrite(pumpInd, HIGH);
pumpState = "Pump Running";
}
else{
digitalWrite(pumpInd, LOW);
pumpState = "Pump Stopped";
}
if(printPumpState) Serial.println(pumpState);
if(prvConsumpRate != consumptionRate ){
prvConsumpRate = consumptionRate;
if(graphMode == 0){
Serial.print("Consump rate : ");
Serial.println(consumptionRate);
}
}
if(tankPrvLevel != tankLevel ){
Serial.print("Tank Level : ");
Serial.println(tankLevel);
tankPrvLevel = tankLevel;
}
if(pump_State && pump_Flow ){
if(!Pump_Flow_Start){
Pump_Flow_Start = true;
PumpflowStart = millis();
}
}
else
{
pumpflow = 0;
Pump_Flow_Start = false;
Pump_Flow_Sensor = false;
}
if((Pump_Flow_Start) && (millis() - PumpflowStart > pumpFlowSensorDelay )){
pumpflow = pumpRate;
Pump_Flow_Sensor = true;
}
tankLevel += pumpflow;
tankLevel -= consumptionRate;
if(tankLevel > maxTanklevel){
tankLevel = maxTanklevel;
overflow = true;
}
else overflow = false;
if(tankLevel < 1){
tankLevel = 0;
consumptionRate = 0;
}
else consumptionRate = consumptionvalve;
delay(tankLevelRefresdly);
if(millis()-printTicks > printTimeout){
if(printPumpState)Serial.println(pumpState);
printTicks = millis();
}
if(millis()-lvlupdatTicks > lvlupdatTimeout){
Serial.print("Tank Level : ");
Serial.println(tankLevel);
lvlupdatTicks = millis();
}
LLL= false;
LL= false;
HL= false;
HHL= false;
if (tankLevel > lowlLowLevel*maxTanklevel){
LLL= true;
if(tankLevel > lowLevel*maxTanklevel){
LL= true;
if(tankLevel > highLevel*maxTanklevel){
HL= true;
if(tankLevel > highHighLevel*maxTanklevel){
HHL= true;
}
}
}
}
digitalWrite(LLLPin,LLL);
digitalWrite(LLPin,LL);
digitalWrite(HLPin,HL);
digitalWrite(HHLPin,HHL);
digitalWrite(OvFlwPin,overflow);
digitalWrite(pumpFlowsensor,Pump_Flow_Sensor);
}