/**
V1.0.1
Removed the 'Delay" functions and implimented 'Millis" for time/delay optimization.
Future imnprovement for better implementation of a specific timed LED HZ Error display
Implemented new Watchdog function that monitors timed delays
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
V1.0.0
Pin Definitons
Pump Relay = 11 ; Pressure Switch = 13 ; Error LED = 12;
Turns on pump for '5' seconds during start up at the 'Void Setup' section
Afterwards, the loop takes over and as long as the pressure switch stays ON(1) it will keep the pump switched on.
If the pressure switch is OFF(0), it will wait for '5' seconds before it shuts off the pump.
Then it will flash the ERROR LED in a '1' HZ ON/OFF signal for a "No Water Pressure", and "Maximum Retries Reached" in the Serial Monitor
If the pressure switch turns back on, then it will swtch the pump back on and resume its monitoring function. ERRONEOUS
Need to implement a manual override for switching the pump on, as the only way it will come back on after the start up sequence
is restarted(resetting the arduino) or if the pressure switch comes back on. But if theres no water flow, it never will...
Future improvements will be to re-arrange the Pin order to reflect extra sensors and modules. Such as Transceiver(NRF24L01, LoRA)
for feedback and other manual override functions,
Displays(7 Segment, OLED, etc...), Additional Relays(For additional pumps or water distribution valves), and additional error fucntions.
A ArduinoMega is a better board for this
**/
//Defining of the Pump Motor Functions
#define Pump_Motor_Relay 11
bool Release_Pump_Motor_Signal = false;
//Defining of the Pressure Switch Functions
#define Pressure_Switch 13
int Pressure_Switch_State;
bool Error_Water_Pressure = false;
bool Err_Pressure_Retries = false;
//Defining of the Error Functions
#define Error_LED 12
int Pressure_Retries = 0;
//Defining Time Functions
const long Err_Pressure_Retries_1HZ = 1000;//1,000 miliseconds
const long Err_No_Water_Pressure_1HZ = 1000;;//1,000 miliseconds
//Define Time Function States(Has the number of defined seconds passed, if so then what?)
unsigned long Err_Previous_Pressure_Retries_1HZ = 0;//Has one second passed, if TRUE, then what?
unsigned long Err_Previous_No_water_Pressure_1HZ = 0;//Has one second passed, if TRUE, then what?
void setup() {
pinMode(Pump_Motor_Relay, OUTPUT); //Arduino Pin Output 11
pinMode(Pressure_Switch, INPUT); //Arduino Pin Input 13
pinMode(Error_LED, OUTPUT); //Arduino Pin Output 12
Serial.begin(9600);//Troubleshooting purposes
digitalWrite(Pump_Motor_Relay, HIGH);//Primes pump so that the pressure switch detects water flow
delay(5000); //Switches pump on for five seconds, independent of pressure switch feedback
}
void Engaging_Pump_Motor() {
switch (Release_Pump_Motor_Signal) { //ON/OFF states of the release signal to the pump, according to state of pressure switch
case 1:
digitalWrite(Pump_Motor_Relay, HIGH); //Signal to switch OFF pump motor relay
break;
case 0:
digitalWrite(Pump_Motor_Relay, LOW); //Signal to switch OFF pump motor relay
}
}
void Monitoring_Pressure_Switch() {
Pressure_Switch_State = digitalRead(Pressure_Switch);
switch (Pressure_Switch_State) {
case 1:
//Serial.print("Pressure Available");
Err_Pressure_Retries = false;
//Pressure_Retries == 0;
Error_Water_Pressure = false;
Release_Pump_Motor_Signal = true;
break;
case 0:
//Serial.print("No Pressure Available");
Err_Pressure_Retries = true;
if (Pressure_Retries >= 5) {
//Serial.println("Error: Maximun Retries Reached");
Error_Water_Pressure = true;
Release_Pump_Motor_Signal = false;
//Err_Pressure_Retries = false;//Stops counter, as error is logged
}
break;
}
}
void Error_No_Water_Pressure() {
switch (Error_Water_Pressure) {
case 1:
/**digitalWrite(Error_LED, HIGH);
delay(1000);
digitalWrite(Error_LED, LOW);
delay(1000);**/
currentTime = millis();
break;
default:
break;
}
}
void Error_States() {
Error_No_Water_Pressure();
}
void Timing_Watchdog(){
unsigned long currentTime = millis();
//Err_Pressure_Retries_1HZ;//1,000 miliseconds// Retries to turn pump back on once every second five times. This is a essentially a five second 'Delay"
switch (Err_Pressure_Retries){
case 1:
//Serial.println(Err_Pressure_Retries_1HZ);
if( currentTime - Err_Previous_Pressure_Retries_1HZ >= Err_Pressure_Retries_1HZ){
Pressure_Retries++;
Serial.println(Pressure_Retries);
Err_Previous_Pressure_Retries_1HZ = currentTime;
}
break;
case 0:
Pressure_Retries == 0;// was Pressure_Retries = 0;
break;
}
}
void loop() {
//Serial.println(Err_Pressure_Retries);
Timing_Watchdog();
Engaging_Pump_Motor();
Monitoring_Pressure_Switch();
Error_States();
}