#include "DHT.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//HC-Sr04 Pins
#define PIN_TRIG 12
#define PIN_ECHO 11
//Declaring the lcd
LiquidCrystal_I2C lcd(0x27, 16, 2);
//DHT Pin
#define DHTPIN 2
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);
//LED Pins
#define RED 4
#define ORANGE 5
#define GREEN 6
int state=RED;
int pins[]={RED,ORANGE,GREEN};
void setup() {
// put your setup code here, to run once:
//Setting UP the pins
pinMode(RED, OUTPUT);
pinMode(ORANGE, OUTPUT);
pinMode(GREEN, OUTPUT);
//Starting the DHT Sensor
dht.begin();
//Starting the serial output
Serial.begin(115200);
Serial.println(F("Starting the Arduino !"));
//The LCD Display
lcd.begin(16,2);
lcd.print("Hello World");
lcd.backlight();
//Distance measure
pinMode(PIN_TRIG, OUTPUT);
pinMode(PIN_ECHO, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
if (isnan(temperature) || isnan(humidity)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}else{
Serial.print(F("Humidity: "));
Serial.print(humidity);
Serial.print(F("% Temperature: "));
Serial.print(temperature);
Serial.println(F("°C "));
if(temperature<15){
state=GREEN;
}else if (temperature <30){
state = ORANGE;
}else{
state = RED;
}
for(int i=0;i<3;i++){
if(pins[i]==state){
digitalWrite(pins[i],HIGH);
}else{
digitalWrite(pins[i], LOW);
}
}
//Measuring the distance
// Start a new measurement:
digitalWrite(PIN_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(PIN_TRIG, LOW);
// Read the result:
int duration = pulseIn(PIN_ECHO, HIGH);
Serial.print("Distance in CM: ");
Serial.println(duration / 58);
Serial.print("Distance in inches: ");
Serial.println(duration / 148);
//Writing to the LCD
lcd.clear();
lcd.print(temperature);
lcd.print("C ");
lcd.print(humidity);
lcd.print("%");
lcd.setCursor(0,1);
lcd.print(duration / 58);
lcd.print("cm");
}
delay(500);
}