Hello Everyone,
I found this tip by Kevin Bacon on youtube, It's a great tip. https://www.youtube.com/watch?v=--KxxMaiwSE&t=965s
I added this tip to a simple state machine script, that I wrote, to turn on my sump pump via a tiny water sensor.
that I found on Amazon
My master sump pump is in the same sump pump hole "small hole diameter", as my second backup sump pump, and the big float that turns on the master sump pump would sometimes get hung up on the backup sump pump, and then the pump would run continuously. So to fix that issue I used a small water sensor to replace the huge floating ball.
It works great, I hope this helps someone
CREDITS:
//Kevin Bacon Tip 224-Superior-Serial.print-statements/Simple_Example at main · RalphBacon/224-Superior-Serial.print-statements · GitHub
Constructive comments are welcomed.
// DECLARATIONS
//Kevin Bacon Tip https://github.com/RalphBacon/224-Superior-Serial.print-statements/tree/main/Simple_Example
//This allows all debug statements to be switched on or off with one setting.
#define DEBUG 0 //all debug controlled here, set DEBUG 0 to DEBUG 1 to turn on debug, set DEBUG 1 to DEBUG 0 to turn off debug
#if DEBUG == 1
#define debug(x) Serial.print(x)
#define debugln(x) Serial.println(x)
#else
#define debug(x)
#define debugln(x)
#endif
// Define pin connections
const int waterSensorPin = 2; // Water sensor connected to digital pin 2
const int relayPin = 8; // Relay module connected to digital pin 8
// Enum to represent the states of the system
enum PumpState
{
IDLE, // Waiting for water detection
PUMP_ON, // Pump is running
PUMP_OFF // Pump turned off after running for a duration
};
// Variables to keep track of the state, timing, and sensor
PumpState Call_Case_Code_Block = IDLE;
unsigned long pumpStartTime = 0; //used with millis
const unsigned long pumpDuration = 30000; // 30 seconds in milliseconds, used with millis
// BEGIN void SETUP
void setup()
{
// Initialize the water sensor pin as an input
pinMode(waterSensorPin, INPUT);
// Initialize the relay pin as an output
pinMode(relayPin, OUTPUT);
// Ensure the relay is off initially
digitalWrite(relayPin, LOW);
debugln("Setup complete."); //debug statements
}
// BEGIN void LOOP
void loop()
{
// Read the water sensor value
int waterDetected = digitalRead(waterSensorPin);
// Switch between states
switch (Call_Case_Code_Block)
{
case IDLE:
// In IDLE, we are waiting for water detection
if (waterDetected == HIGH)
{
// Water detected, move to PUMP_ON state
Call_Case_Code_Block = PUMP_ON;
digitalWrite(relayPin, HIGH); // Turn on the pump
pumpStartTime = millis(); // First initialization of millis, Record the start time, and on each loop if (waterDetected == HIGH); reset the pumpStartTime
debugln("water detected calling case PUMP_ON"); //debug statements
}
break;
case PUMP_ON:
// The pump is running, check if the duration has passed
if (millis() - pumpStartTime >= pumpDuration)
{
// Time to turn off the pump
Call_Case_Code_Block = PUMP_OFF;
digitalWrite(relayPin, LOW); // Turn off the pump
debugln("30 seconds has passed turning the pump off, calling case PUMP_OFF "); //debug statements
}
break;
case PUMP_OFF:
// The pump is off, return to IDLE state once the water is no longer detected
if (waterDetected == LOW)
{
// Water no longer detected, go back to IDLE
Call_Case_Code_Block = IDLE;
debugln("water no longer detected, calling case IDLE "); //debug statements
}
break;
default:
//nothing to do here
break;
}
}
// END void loop