#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 switch pin for automatic mode
const int autoSwitchPin = A0;
// Define slide switch pin for manual mode
const int manualSwitchPin = A1;
// Define variable to store switch position
String switchPosition = "";
// Define variable to store auto switch state
String autoSwitchStateStr = "";
// 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;
// Initialize LCD
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
// Variable to store the automatic switch state
bool autoSwitchState = LOW;
// Variable to store the manual switch state
bool manualSwitchState = LOW;
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 automatic switch pin
pinMode(autoSwitchPin, INPUT);
// Set up manual switch pin
pinMode(manualSwitchPin, INPUT);
}
void loop() {
// Read the state of the automatic switch
autoSwitchState = digitalRead(autoSwitchPin);
// Determine auto switch state
autoSwitchStateStr = autoSwitchState == HIGH ? "On" : "Off";
// Read the state of the manual switch
manualSwitchState = digitalRead(manualSwitchPin);
// Determine switch position
if (autoSwitchState == HIGH) {
switchPosition = "Auto";
automaticMode();
} else if (manualSwitchState == HIGH) {
switchPosition = "Manual";
manualMode();
} else {
switchPosition = "Off";
}
// Print switch position to serial monitor
Serial.print("Switch Position: ");
Serial.println(switchPosition);
}
void automaticMode() {
// Variables to store the distance and water level
float distance;
int waterLevel;
// 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("Automatic Mode - Water Level: ");
Serial.print(waterLevel);
Serial.println("%");
// Print auto switch state to serial monitor
Serial.print("Auto Switch State: ");
Serial.println(autoSwitchStateStr);
// Display water level and switch position on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Auto Mode - Water Level:");
lcd.setCursor(0, 1);
lcd.print(waterLevel);
lcd.print("%");
lcd.setCursor(10, 0);
lcd.print("Switch:");
lcd.print(switchPosition);
lcd.setCursor(10, 1);
lcd.print("Auto:");
lcd.print(autoSwitchStateStr);
// 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
} else {
// Turn off LED and buzzer
digitalWrite(ledPin, LOW);
noTone(buzzerPin);
}
// Delay before next reading
delay(1000);
}
void manualMode() {
// Read the state of the automatic switch
autoSwitchState = digitalRead(autoSwitchPin);
// Print manual switch state to serial monitor
Serial.print("Manual Switch State: ");
Serial.println(manualSwitchState);
}