#include <Servo.h>
#include <DHT.h>
#define DHTPIN 4 // Pin which the DHT22 is connected
#define DHTTYPE DHT22 // DHT 22 sensor
#define SERVOPIN 11 // Pin which the servo is connected
#define REDPIN 10 // Pin for the red component of the RGB LED
#define GREENPIN 9 // Pin for the green component of the RGB LED
#define BLUEPIN 6 // Pin for the blue component of the RGB LED
DHT dht(DHTPIN, DHTTYPE);
Servo myservo;
void setup() {
myservo.attach(SERVOPIN);
dht.begin();
pinMode(REDPIN, OUTPUT);
pinMode(GREENPIN, OUTPUT);
pinMode(BLUEPIN, OUTPUT);
}
void loop() {
float temp = dht.readTemperature(); // Read temperature in Celsius
float hum = dht.readHumidity(); // Read humidity percentage
if (isnan(temp) || isnan(hum)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
int servoPos = map(temp, 0, 50, 0, 180); // Map the temperature to a servo position
myservo.write(servoPos); // Move servo to position based on temperature
// Set RGB color based on humidity (you can adjust this logic as needed)
if(hum < 30) {
digitalWrite(REDPIN, LOW); // Blue for low humidity
digitalWrite(GREENPIN, LOW);
digitalWrite(BLUEPIN, HIGH);
} else if(hum >= 30 && hum < 60) {
digitalWrite(REDPIN, LOW); // Green for moderate humidity
digitalWrite(GREENPIN, HIGH);
digitalWrite(BLUEPIN, LOW);
} else {
digitalWrite(REDPIN, HIGH); // Red for high humidity
digitalWrite(GREENPIN, LOW);
digitalWrite(BLUEPIN, LOW);
}
delay(2000); // Wait for 2 seconds before reading again
}