#include #include #include "DHT.h" LiquidCrystal_I2C lcd(0x27, 16, 2); #define DHTPIN 2 // Digital pin connected to the DHT sensor #define DHTTYPE DHT11 // DHT 11 const int trigPin = 7; // Trigger Pin of Ultrasonic Sensor const int echoPin = 6; // Echo Pin of Ultrasonic Sensor int sensor = 4; // the pin that the sensor is atteched to int state = LOW; // by default, no motion detected int val = 0; DHT dht(DHTPIN, DHTTYPE); long duration; int distance; void setup() { pinMode(A0, INPUT); Serial.begin(9600); lcd.init(); lcd.backlight(); dht.begin(); pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output pinMode(echoPin, INPUT); // Sets the echoPin as an Input lcd.clear(); } void loop() { // Reading temperature or humidity takes about 250 milliseconds! // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) float h = dht.readHumidity(); // Read temperature as Celsius (the default) float t = dht.readTemperature(); // Read temperature as Fahrenheit (isFahrenheit = true) float f = dht.readTemperature(true); // Check if any reads failed and exit early (to try again). if (isnan(h) || isnan(t) || isnan(f)) { Serial.println(F("Failed to read from DHT sensor!")); return; } int analogSensor = analogRead(A0); Serial.print("Gas: "); Serial.println(analogSensor); digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = duration * 0.034 / 2; Serial.print("Distance: "); Serial.println(distance); float hif = dht.computeHeatIndex(f, h); float hic = dht.computeHeatIndex(t, h, false); Serial.print(F("Humidity: ")); Serial.print(h); Serial.print(F("% Temperature: ")); Serial.print(t); Serial.print(F("°C ")); Serial.print(f); Serial.print(F("°F Heat index: ")); Serial.print(hic); Serial.print(F("°C ")); Serial.print(hif); Serial.println(F("°F")); lcd.setCursor(0, 0); lcd.print("Temp: "); lcd.print(t); lcd.print("*C"); lcd.setCursor(0, 1); lcd.print("Humi: "); lcd.print(h); lcd.print("%"); delay(2000); lcd.clear(); delay(2000); lcd.print("Gas: "); lcd.print(analogSensor); delay(2000); lcd.clear(); delay(2000); lcd.print("Distance: "); lcd.print(distance); delay(2000); lcd.clear(); val = digitalRead(sensor); if (val == HIGH) { delay(100); if (state == LOW) { lcd.print("Motion detected! "); delay(2000); lcd.clear(); Serial.println("Motion detected!"); state = HIGH; } } else { delay(200); if (state == HIGH) { Serial.println("Motion stopped!"); state = LOW; lcd.print("Motion stopped! "); delay(2000); lcd.clear(); } } }