#include <EEPROM.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
long duration, inches;
int set_val, percentage;
bool state, pump;
// Define priority levels for different areas
const int HIGH_PRIORITY_THRESHOLD = 80; // Example: High priority threshold is 80%
const int MEDIUM_PRIORITY_THRESHOLD = 60; // Example: Medium priority threshold is 60%
void setup() {
lcd.begin(16, 2);
lcd.print("WATER LEVEL:");
lcd.setCursor(0, 1);
lcd.print("PUMP:OFF MANUAL");
pinMode(8, OUTPUT);
pinMode(9, INPUT);
pinMode(10, INPUT_PULLUP);
pinMode(11, INPUT_PULLUP);
pinMode(12, OUTPUT);
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);
percentage = (set_val - inches) * 100 / set_val;
lcd.setCursor(12, 0);
if (percentage < 0) percentage = 0;
lcd.print(percentage);
lcd.print("% ");
// Determine priority level based on water level
int priority = determinePriority(percentage);
// Control pump based on priority and manual/auto mode
if (priority >= HIGH_PRIORITY_THRESHOLD && digitalRead(11)) {
pump = true; // Turn pump ON if water level is low and in AUTO mode
} else {
pump = false; // Turn pump OFF if priority is below threshold or in MANUAL mode
}
digitalWrite(12, !pump); // Control pump
// Display pump status and priority level on LCD
lcd.setCursor(5, 1);
if (pump) lcd.print("ON ");
else lcd.print("OFF");
lcd.setCursor(9, 1);
lcd.print("Priority: ");
lcd.print(priority);
// Update set_val if button is pressed in MANUAL mode
if (!digitalRead(10) && !state && digitalRead(11)) {
state = true;
set_val = inches;
EEPROM.write(0, set_val);
}
// Toggle pump if button is pressed in MANUAL mode
if (!digitalRead(10) && !state && !digitalRead(11)) {
state = true;
pump = !pump;
}
// Reset state
if (digitalRead(10)) state = false;
delay(500);
}
long microsecondsToInches(long microseconds) {
return microseconds / 74 / 2;
}
int determinePriority(int waterLevelPercentage) {
if (waterLevelPercentage >= HIGH_PRIORITY_THRESHOLD) {
return 3; // High priority
} else if (waterLevelPercentage >= MEDIUM_PRIORITY_THRESHOLD) {
return 2; // Medium priority
} else {
return 1; // Low priority
}
}