#include <LiquidCrystal.h>
// Define LCD pins
const int rs = 27;
const int en = 26;
const int d4 = 25;
const int d5 = 33;
const int d6 = 32;
const int d7 = 14;
// Define ultrasonic sensor pins
const int trigPin = 4;
const int echoPin = 5;
// Define LED pin
const int ledPin = 16;
// Define buzzer pin
const int buzzerPin = 17;
// Define slide switches pins
const int manualSwitchPin = 18;
const int autoSwitchPin = 19;
// 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;
// Threshold for determining switch state
const int switchThreshold = 500; // Adjust this value as needed
// Variables to store the switch states
bool autoSwitchState = LOW;
bool manualSwitchState = LOW;
// Previous state of the manual switch
bool prevManualSwitchState = LOW;
// Initialize LCD
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Initialize 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("%");
// 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
if (autoSwitchState == HIGH || (manualSwitchState == HIGH && waterLevel > switchThreshold)) {
Serial.println("Automatic mode: Valve is CLOSED");
} else if (manualSwitchState == HIGH) {
Serial.println("Manual mode: Valve is CLOSED");
}
}
// Display water level and manual switch status on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Water Level:");
lcd.setCursor(0, 1);
lcd.print("Level: ");
lcd.print(waterLevel);
lcd.print("%");
lcd.setCursor(10, 1);
if (manualSwitchState == HIGH) {
lcd.print("Manual ON ");
} else {
lcd.print("Manual OFF");
}
// Delay before next reading
delay(1000);
}