#include <LiquidCrystal_I2C.h>
#include <Wire.h>
// Set up the LCD with I2C address 0x27 and a 16x2 display
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Pin Definitions
const int tankFullPin = 7; // Pin for tank full sensor
const int tankEmptyPin = 6; // Pin for tank empty sensor
const int pumpPin = 13; // Pin for pump control
const int buttonPin = 2; // Pin for manual button (optional for pump control)
void setup() {
lcd.begin(16, 2); // Initialize the LCD
lcd.backlight(); // Turn on the LCD backlight
// Set the sensor pins as input
pinMode(tankFullPin, INPUT);
pinMode(tankEmptyPin, INPUT);
pinMode(pumpPin, OUTPUT);
// Set up the button pin as input
pinMode(buttonPin, INPUT_PULLUP); // Using internal pull-up resistor
// Start serial communication for manual input via Serial Monitor
Serial.begin(9600);
// Display initial message on the LCD
lcd.print(" Hello");
delay(100); // Short delay to show the initial message
}
void loop() {
// Read the tank sensor states
int tankFull = digitalRead(tankFullPin);
int tankEmpty = digitalRead(tankEmptyPin);
// Check if Serial input is available (manual control via Serial Monitor)
if (Serial.available() > 0) {
char input = Serial.read();
if (input == 'f') { // 'f' for tank full
lcd.clear();
lcd.print(" Tank is full");
digitalWrite(pumpPin, LOW); // Turn off the pump
} else if (input == 'e') { // 'e' for tank empty
lcd.clear();
lcd.print(" Water Running");
digitalWrite(pumpPin, HIGH); // Turn on the pump
} else {
lcd.clear();
lcd.print(" Invalid Input");
}
delay(500); // Wait for a moment to avoid multiple inputs
return; // Return to the loop to prevent further serial handling
}
// If both sensors indicate the tank is full (both HIGH)
if (tankFull == HIGH && tankEmpty == HIGH) {
lcd.clear();
lcd.print(" Tank is full");
digitalWrite(pumpPin, LOW); // Turn off the pump
delay(2000); // Wait for 2 seconds
// Wait until the tankFull sensor becomes LOW
while (tankFull == HIGH) {
lcd.clear();
lcd.print(" Tank gg Full");
delay(100); // Short delay
tankFull = digitalRead(tankFullPin); // Re-read tankFull
}
return; // Exit the loop and start over
}
// If both sensors indicate that the tank is empty (both LOW)
else if (tankFull == LOW && tankEmpty == LOW) {
lcd.clear();
lcd.print(" Water Running");
delay(100); // Short delay
digitalWrite(pumpPin, HIGH); // Turn on the pump
}
// If one sensor is HIGH and the other is LOW, do nothing
else {
return;
}
// Additional: Manual control with a button (optional)
int buttonState = digitalRead(buttonPin); // Read the button state
if (buttonState == LOW) { // Button pressed
lcd.clear();
lcd.print(" Manual ON");
digitalWrite(pumpPin, HIGH); // Turn on the pump manually
delay(1000); // Pump stays on for 1 second (for testing)
} else { // Button not pressed
lcd.clear();
lcd.print(" Pump OFF");
digitalWrite(pumpPin, LOW); // Turn off the pump manually
}
}