#include <LiquidCrystal.h>
#include <OneWire.h>
#include <DallasTemperature.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;
// 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
// Water quality thresholds based on temperature
const float goodQualityThreshold = 50.0; // Adjust as needed
const float mediumQualityThreshold = 75.0; // Adjust as needed
// Water quality levels
enum WaterQuality {
GOOD,
MEDIUM,
POOR
};
// Water quality sensor setup
#define ONE_WIRE_BUS 8 // Pin connected to the DS18B20 sensor
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
// 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
WaterQuality waterQuality = GOOD;
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);
// Start up the library for the sensors
sensors.begin();
}
void loop()
{
// 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("Automatic mode: Manual switch is OFF");
}
// Update previous manual switch state
prevManualSwitchState = manualSwitchState;
// If manual switch is turned off, close the valve in automatic mode
if (manualSwitchState == LOW && autoSwitchState == HIGH)
{
Serial.println("Automatic mode: Valve is CLOSED");
}
}
// Trigger ultrasonic sensor to send pulse
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Variables to store the distance and water level
float distance;
int waterLevel;
// 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);
// Display water level on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Water Level:");
lcd.setCursor(0, 1);
lcd.print(waterLevel);
lcd.print("%");
// 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 water quality is good
if (waterQuality == GOOD)
{
Serial.println("Valve is OPEN");
}
else
{
Serial.println("Water quality not good, valve remains CLOSED");
}
}
else
{
// Turn off LED and buzzer
digitalWrite(ledPin, LOW);
noTone(buzzerPin);
// Close the valve if water quality is not good
if (waterQuality != GOOD)
{
Serial.println("Water quality not good, valve remains CLOSED");
}
else
{
Serial.println("Valve is CLOSED");
}
}
// Read temperature from DS18B20 sensor
sensors.requestTemperatures();
float temperature = sensors.getTempCByIndex(0);
Serial.print("Temperature: ");
Serial.println(temperature);
// Assess water quality based on temperature
if (temperature <= goodQualityThreshold)
{
waterQuality = GOOD;
}
else if (temperature <= mediumQualityThreshold)
{
waterQuality = MEDIUM;
}
else
{
waterQuality = POOR;
}
// Display water quality assessment in the serial monitor
switch (waterQuality)
{
case GOOD:
Serial.println("Water quality: GOOD");
break;
case MEDIUM:
Serial.println("Water quality: MEDIUM");
break;
case POOR:
Serial.println("Water quality: POOR");
break;
}
delay(1000);
}