#include <Wire.h>
#include <DHT.h>
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
// Initialize the DHT sensor
#define DHTPIN 2 // what digital pin we're connected to
#define DHTTYPE DHT22 // DHT 22
DHT dht(DHTPIN, DHTTYPE);
// Initialize the servo
Servo servoMotor;
const int servoPin = 9;
// Set the temperature thresholds
const float tempThresholdHigh = 30.0; // Temperature to open water pump (high threshold)
const float tempThresholdLow = 25.0; // Temperature to close water pump (low threshold)
// Initialize the LCD for I2C
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD I2C address
void setup() {
// Start the DHT sensor
dht.begin();
// Start the servo and attach it to its pin
servoMotor.attach(servoPin);
// Initialize the LCD
lcd.init();
// Turn on the backlight.
lcd.backlight();
}
void loop() {
// Read the humidity and temperature
float h = 30;//dht.readHumidity();
float t = 40;//dht.readTemperature();
// Check if any reads failed
if (isnan(h) || isnan(t)) {
lcd.print("Failed to read from DHT sensor!");
return;
}
// Control the servo based on the temperature
if (t > tempThresholdHigh) {
// Temperature too high, open water pump
servoMotor.write(180);
} else {
// Temperature too low, close water pump
servoMotor.write(0);
}
// Print the humidity and temperature to the LCD
lcd.clear(); // Clear the LCD screen before printing new values
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(t);
lcd.print((char)223); // Degree symbol
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Hum: ");
lcd.print(h);
lcd.print("%");
delay(2000); // Delay before next reading
}
/*
#include <Wire.h>
#include <DHT.h>
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
// Initialize the DHT sensor
#define DHTPIN 2 // what digital pin we're connected to
#define DHTTYPE DHT22 // DHT 22
DHT dht(DHTPIN, DHTTYPE);
// Initialize the servo
Servo servoMotor;
const int servoPin = 9;
// Set the temperature thresholds
const float tempThresholdHigh = 30.0; // Temperature to open water pump (high threshold)
const float tempThresholdLow = 25.0; // Temperature to close water pump (low threshold)
// Map temperature range to servo angle range
const int servoAngleLowTemp = 0; // Servo angle for low temperature
const int servoAngleHighTemp = 180; // Servo angle for high temperature
// Initialize the LCD for I2C
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD I2C address
void setup() {
// Start the DHT sensor
dht.begin();
// Start the servo and attach it to its pin
servoMotor.attach(servoPin);
// Initialize the LCD
lcd.init();
// Turn on the backlight.
lcd.backlight();
}
void loop() {
// Read the humidity and temperature
float h = dht.readHumidity();
float t = dht.readTemperature();
// Check if any reads failed
if (isnan(h) || isnan(t)) {
lcd.print("Failed to read from DHT sensor!");
return;
}
// Map the temperature to servo angle
int servoAngle = map(t, tempThresholdLow, tempThresholdHigh, servoAngleLowTemp, servoAngleHighTemp);
// Control the servo based on the temperature
servoMotor.write(servoAngle);
// Print the humidity and temperature to the LCD
lcd.clear(); // Clear the LCD screen before printing new values
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(t);
lcd.print((char)223); // Degree symbol
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Hum: ");
lcd.print(h);
lcd.print("%");
delay(2000); // Delay before next reading
}*/