#include <LiquidCrystal.h>
// Pin assignments
const int buttonAttentionPin_WaterSensorLow = 9;
const int buttonAttentionPin_WaterSensorHigh = 10;
const int ledAttentionPin_PumpOut = 3;
const int ledAttentionPin_PumpIn = 2;
const int StartButtonSwitch = 13;
#define SPEAKER_PIN 8
int motorState = LOW; // Motor state: LOW (Off), HIGH (On)
int cycleCount = 0; // Number of cycles completed
int totalCycles = 1; // Total number of cycles to perform
// Variables for storing button and sensor states
bool StartButtonPressed;
bool waterSensorLow;
bool waterSensorHigh;
LiquidCrystal lcd(12, 11, 7, 6, 5, 4);
void setup() {
// Initialize serial communication for debugging
Serial.begin(9600);
lcd.begin(16, 2);
// Configure sensor pins as inputs
pinMode(buttonAttentionPin_WaterSensorLow, INPUT_PULLUP); // Water sensor low
pinMode(buttonAttentionPin_WaterSensorHigh, INPUT_PULLUP); // Water sensor high
pinMode(StartButtonSwitch, INPUT_PULLUP); // Start button
// Configure LED pins as outputs
pinMode(ledAttentionPin_PumpOut, OUTPUT);
pinMode(ledAttentionPin_PumpIn, OUTPUT);
// Turn off both LEDs initially
digitalWrite(ledAttentionPin_PumpOut, LOW);
digitalWrite(ledAttentionPin_PumpIn, LOW);
}
void loop() {
// Read button and sensor states
StartButtonPressed = digitalRead(StartButtonSwitch); // Check for button press (LOW)
waterSensorLow = digitalRead(buttonAttentionPin_WaterSensorLow);
waterSensorHigh = digitalRead(buttonAttentionPin_WaterSensorHigh);
delay(350);
// Check for error conditions where both sensors are either LOW or HIGH
if ((waterSensorLow && waterSensorHigh) || (!waterSensorLow && !waterSensorHigh)) {
handleErrorCondition();
return; // Exit the loop and wait for reset
}
// Check if the start button is pressed
if (StartButtonPressed == LOW) { // Button pressed
cycleCount = 0; // Reset cycle count
performCycle();
} else {
Serial.println("System is stopped.");
lcd.clear();
lcd.print("System is Off");
delay(1000);
}
}
// Function to perform one full cycle of PumpOut and PumpIn
void performCycle() {
while (cycleCount < totalCycles) {
// Perform PumpOut first
if (waterSensorHigh && !waterSensorLow) {
Serial.println("Starting PumpOut");
pumpOut();
}
// After PumpOut, check and perform PumpIn
if (!waterSensorHigh && waterSensorLow) {
Serial.println("Starting PumpIn");
pumpIn();
}
// Increment the cycle count after one PumpOut + PumpIn cycle
cycleCount++;
Serial.print("Cycle Completed: ");
Serial.println(cycleCount);
afterOneCycle();
}
// Reset system after all cycles are complete
Serial.println("All cycles completed, system resetting...");
stopAndReset();
}
// Function to handle Pump In process
void pumpIn() {
Serial.println("Pump In Started.");
delay(1000);
lcd.clear();
lcd.print("P.in Mode");
while (waterSensorLow && !waterSensorHigh) { // Water is low, start pumping in
waterSensorLow = digitalRead(buttonAttentionPin_WaterSensorLow);
waterSensorHigh = digitalRead(buttonAttentionPin_WaterSensorHigh);
StartButtonPressed = digitalRead(StartButtonSwitch); // Continuously check button state
if (StartButtonPressed == HIGH) { // Button pressed, stop the pump
Serial.println("Start button pressed during Pump In. Stopping...");
stopAndReset();
return; // Exit the function
}
delay(1000); // Delay between readings
Serial.println("Water level is low. Pumping water in.");
motorState = HIGH; // Turn on the motor to pump water in
digitalWrite(ledAttentionPin_PumpIn, HIGH); // Turn on Pump In LED
delay(1000);
}
// Check if water level is high
if (!waterSensorLow && waterSensorHigh) {
Serial.println("Water level is already high. Pump in is not needed.");
motorState = LOW;
digitalWrite(ledAttentionPin_PumpIn, LOW); // Turn off Pump In LED
}
}
// Function to handle Pump Out process
void pumpOut() {
Serial.println("Pump Out Started.");
delay(1000);
lcd.clear();
lcd.print("P.out Mode");
while (waterSensorHigh && !waterSensorLow) { // Water is high, start pumping out
waterSensorLow = digitalRead(buttonAttentionPin_WaterSensorLow);
waterSensorHigh = digitalRead(buttonAttentionPin_WaterSensorHigh);
StartButtonPressed = digitalRead(StartButtonSwitch); // Continuously check button state
if (StartButtonPressed == HIGH) { // Button pressed, stop the pump
Serial.println("Start button pressed during Pump Out. Stopping...");
stopAndReset();
return; // Exit the function
}
delay(1000); // Delay between readings
Serial.println("Water level is high. Pumping water out.");
motorState = HIGH; // Turn on the motor to pump water out
digitalWrite(ledAttentionPin_PumpOut, HIGH); // Turn on Pump Out LED
delay(1000);
}
// Check if water level is low
if (!waterSensorHigh && waterSensorLow) {
Serial.println("Water level is already low. Pump out is not needed.");
motorState = LOW;
digitalWrite(ledAttentionPin_PumpOut, LOW);
}
}
// Function to stop the system and reset
void stopAndReset() {
Serial.println("Stopping and resetting the system.");
delay(1000);
digitalWrite(ledAttentionPin_PumpIn, LOW); // Turn off Pump In LED
digitalWrite(ledAttentionPin_PumpOut, LOW); // Turn off Pump Out LED
motorState = LOW; // Turn off the motor
lcd.clear();
delay(25);
lcd.print("System is Off");
delay(25);
tone(SPEAKER_PIN, 262, 250); // Use the defined SPEAKER_PIN
}
// Function to handle error conditions where both sensors are either LOW or HIGH
void handleErrorCondition() {
Serial.println("Error: Invalid water sensor states (both LOW or both HIGH). System idle.");
lcd.clear();
lcd.print("ERROR: Reset");
delay(1000);
lcd.clear();
// Idle the system until the start button is pressed again (reset by user)
while (digitalRead(StartButtonSwitch) == HIGH) { // Wait for button press (LOW)
delay(100); // Small delay to prevent fast looping
}
// Reset the system once the button is pressed
Serial.println("System reset triggered. Rebooting...");
lcd.clear();
lcd.print("System Reboot");
delay(2000); // Delay to simulate reboot
stopAndReset();
}
void afterOneCycle() {
digitalWrite(ledAttentionPin_PumpIn, LOW); // Turn off Pump In LED
digitalWrite(ledAttentionPin_PumpOut, LOW); // Turn off Pump Out LED
motorState = LOW;
lcd.clear();
delay(25);
while(StartButtonPressed == LOW){
StartButtonPressed = digitalRead(StartButtonSwitch);
lcd.clear();
lcd.print("System is Off");
delay(1000);
lcd.clear();
delay(1000);
lcd.print("Please Reset");
delay(1000);
}
}