#include <DHT.h>
#define TRIG_PIN 16 // Pin connected to the Trig of the HC-SR04
#define ECHO_PIN 4 // Pin connected to the Echo of the HC-SR04
#define DHT_PIN 18 // Pin connected to the DHT22
#define LED1_PIN 27 // Pin connected to LED 1
#define YELLOW_LED_PIN 26 // Pin connected to Yellow LED
#define BLUE_LED_PIN 25 // Pin connected to Blue LED
#define DHTTYPE DHT22
DHT dht(DHT_PIN, DHTTYPE); // Initialize DHT sensor
void setup() {
Serial.begin(115200);
pinMode(LED1_PIN, OUTPUT);
pinMode(YELLOW_LED_PIN, OUTPUT);
pinMode(BLUE_LED_PIN, OUTPUT);
dht.begin();
}
void loop() {
// Measure distance
long duration, distance;
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH);
distance = duration * 0.034 / 2; // Convert to cm
// Measure temperature
float temperature = dht.readTemperature(); // Read temperature in Celsius
// Control LED1 based on distance
if (distance <= 100) {
digitalWrite(LED1_PIN, HIGH);
delay(500); // On for half a second
digitalWrite(LED1_PIN, LOW);
delay(500); // Off for half a second
} else {
digitalWrite(LED1_PIN, HIGH); // On when distance > 100 cm
}
// Control Yellow LED based on temperature
if (temperature > 50) {
digitalWrite(YELLOW_LED_PIN, HIGH); // On when temperature > 50°C
} else {
digitalWrite(YELLOW_LED_PIN, LOW); // Off otherwise
}
// Control Blue LED based on temperature
if (temperature < 0) {
digitalWrite(BLUE_LED_PIN, HIGH); // On when temperature < 0°C
} else {
digitalWrite(BLUE_LED_PIN, LOW); // Off otherwise
}
// Print values to Serial Monitor for debugging
Serial.print("Distance: ");
Serial.print(distance);
Serial.print(" cm, Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
delay(1000); // Wait a second before next measurement
}