#include <EEPROM.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
long duration, inches;
int set_val;
const int potPin = A1; // Analog pin connected to the potentiometer
const int potThreshold = 400; // Threshold value for potentiometer indicating low flow
const int restartThreshold = 50; // Threshold value for restarting pump in automatic mode
const unsigned long debounceDelay = 50; // Debounce delay for switches
void setup() {
Serial.begin(9600); // Initialize serial communication
lcd.begin(16, 2);
lcd.print("WATER LEVEL:");
lcd.setCursor(0, 1);
lcd.print("PUMP: OFF");
pinMode(3, OUTPUT); // Added pinMode for pin 3
pinMode(8, OUTPUT);
pinMode(9, INPUT);
pinMode(12, OUTPUT);
pinMode(potPin, INPUT); // Set potentiometer pin as input
set_val = EEPROM.read(0);
if (set_val > 150) set_val = 150;
}
void loop() {
digitalWrite(3, LOW);
delayMicroseconds(2);
digitalWrite(8, HIGH);
delayMicroseconds(10);
digitalWrite(8, LOW);
duration = pulseIn(9, HIGH);
inches = microsecondsToInches(duration);
int percentage = (set_val - inches) * 100 / set_val;
lcd.setCursor(12, 0);
if (percentage < 0) percentage = 0;
lcd.print(percentage);
lcd.print("% ");
bool pump = true; // Default to pump on
// Dry run protection based on potentiometer
int potValue = analogRead(potPin);
Serial.print("Potentiometer value: ");
Serial.println(potValue); // Print potentiometer value for debugging
if (potValue < potThreshold) { // If potentiometer value indicates low flow
pump = false; // Stop the pump
lcd.setCursor(0, 1);
lcd.print("Dry run protection ");
delay(2000); // Delay to show the message for 2 seconds
lcd.clear(); // Clear the LCD after displaying message
lcd.setCursor(0, 0); // Set cursor back to start
lcd.print("WATER LEVEL:"); // Reprint the label
} else if (percentage >= restartThreshold && percentage < 100) { // Restart pump if water level falls below restart threshold and not at 100%
pump = true;
lcd.setCursor(0, 1);
lcd.print(" "); // Clear the line
} else if (percentage >= 100) { // Turn off pump when water level reaches or exceeds 100 percent
pump = false;
lcd.setCursor(0, 1);
lcd.print("Water level at 100% ");
delay(2000); // Delay to show the message for 2 seconds
lcd.clear(); // Clear the LCD after displaying message
lcd.setCursor(0, 0); // Set cursor back to start
lcd.print("WATER LEVEL:"); // Reprint the label
}
digitalWrite(12, !pump);
// Debugging output for pump control
Serial.print("Pump control: ");
Serial.println(pump);
lcd.setCursor(5, 1);
if (pump)
lcd.print("ON ");
else
lcd.print("OFF");
delay(500);
}
long microsecondsToInches(long microseconds) {
return microseconds / 74 / 2;
}