#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 DS18B20 temperature sensor pin
const int temperatureSensorPin = 13; // D13 for OneWire communication
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(temperatureSensorPin);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
// Define photoresistor (LDR) sensor pins
const int ldrDigitalPin = 8;
const int ldrAnalogPin = A2;
// 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;
// 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;
void setup()
{
// Initialize serial communication
Serial.begin(9600);
// Set up LCD
lcd.begin(16, 2);
// Start up the DS18B20 temperature sensor
sensors.begin();
// 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;
}
// Measure the temperature from DS18B20 sensor
sensors.requestTemperatures();
temperature = sensors.getTempCByIndex(0);
// Print temperature to serial monitor
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
// Read analog value from photoresistor (LDR) sensor
int ldrValue = analogRead(ldrAnalogPin);
// Print LDR value to serial monitor
Serial.print("LDR Value: ");
Serial.println(ldrValue);
// Trigger ultrasonic sensor to send pulse
// (Code for ultrasonic sensor not needed as it's not integrated here)
// Perform the rest of your logic based on water level, LDR value, temperature, etc.
// ...
// Delay before next reading
delay(1000);
}