#include <LiquidCrystal.h>
// Define LCD pins
const int rs = 12;
const int en = 11;
const int d4 = 5;
const int d5 = 4;
const int d6 = 3;
const int d7 = 2;
// Define ultrasonic sensor pins
const int trigPin = 9;
const int echoPin = 10;
// Define LED pin
const int ledPin = 7;
// Define buzzer pin
const int buzzerPin = 6;
// Define slide switches pins
const int manualSwitchPin = A1;
const int autoSwitchPin = A0;
// Define environmental factors
float temperature = 25.0; // Temperature in Celsius
float pH = 7.0; // pH level
// Maximum distance (in cm) the ultrasonic sensor can measure
const int maxDistance = 400;
// Threshold for triggering the buzzer (water level below which alarm triggers)
const int buzzerThreshold = 30;
// Maximum threshold for water level in manual mode
const int manualModeMaxThreshold = 80; // Adjust as needed
// Threshold for determining switch state
const int switchThreshold = 500; // Adjust this value as needed
// Initialize LCD
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
// Variables to store the switch states
bool autoSwitchState = LOW;
bool manualSwitchState = LOW;
// Previous state of the manual switch
bool prevManualSwitchState = LOW;
// Variable to store water quality assessment
bool waterQualityGood = false;
void setup()
{
// Initialize serial communication
Serial.begin(9600);
// Set up LCD
lcd.begin(16, 2);
// Set up ultrasonic sensor pins
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// Set up LED and buzzer pins
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
// Set up slide switch pins
pinMode(manualSwitchPin, INPUT);
pinMode(autoSwitchPin, INPUT);
// Initialize previous manual switch state
prevManualSwitchState = digitalRead(manualSwitchPin);
}
void loop()
{
// Variables to store the distance and water level
float distance;
int waterLevel;
// Read the switch states
autoSwitchState = digitalRead(autoSwitchPin);
manualSwitchState = digitalRead(manualSwitchPin);
// Check if manual switch state has changed
if (manualSwitchState != prevManualSwitchState)
{
if (manualSwitchState == HIGH)
{
Serial.println("Manual mode: Manual switch is ON");
}
else
{
Serial.println("Manual mode: Manual switch is OFF");
}
// Update previous manual switch state
prevManualSwitchState = manualSwitchState;
}
// Trigger ultrasonic sensor to send pulse
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Measure the pulse travel time to calculate distance (in cm)
distance = pulseIn(echoPin, HIGH) / 58.0; // Divide by conversion factor
// Map distance to water level percentage
waterLevel = map(distance, 0, maxDistance, 0, 100);
// Ensure water level stays within 0-100 range
waterLevel = constrain(waterLevel, 0, 100);
// Print water level to serial monitor
Serial.print("Water Level: ");
Serial.print(waterLevel);
Serial.println("%");
// Display water level on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Water Level:");
lcd.setCursor(0, 1);
lcd.print(waterLevel);
lcd.print("%");
// Check if water level is below the buzzer threshold
if (waterLevel <= buzzerThreshold)
{
// Activate LED and buzzer for alarm
digitalWrite(ledPin, HIGH);
tone(buzzerPin, 1000); // Buzzer sound
// Open the valve if in manual mode and water level is low
if (manualSwitchState == HIGH)
{
Serial.println("Manual mode: Valve is OPEN");
}
else if (autoSwitchState == HIGH)
{
Serial.println("Automatic mode: Valve is OPEN");
}
else
{
Serial.println("Automatic mode: Valve is CLOSED");
}
}
else
{
// Turn off LED and buzzer
digitalWrite(ledPin, LOW);
noTone(buzzerPin);
// Close the valve if in automatic mode or if water level is above maximum threshold in manual mode
if (autoSwitchState == HIGH || (manualSwitchState == HIGH && waterLevel > manualModeMaxThreshold))
{
Serial.println("Automatic mode: Valve is CLOSED");
}
else if (manualSwitchState == HIGH)
{
Serial.println("Manual mode: Valve is CLOSED");
}
}
// Prompt user to assess water quality
if (Serial.available() > 0)
{
char response = Serial.read();
if (response == '1')
{
waterQualityGood = true;
Serial.println("Water quality is GOOD");
}
else if (response == '0')
{
waterQualityGood = false;
Serial.println("Water quality is POOR");
}
else
{
Serial.println("Invalid input. Please enter '1' for good or '0' for poor water quality.");
}
}
// Delay before next reading
delay(1000);
}