//includes all necessary library
#include "DHT.h"
#include<Wire.h>
#include<LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
//Defines all pins
#define DHTPIN 8
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
//Defines all variables
int trig = 9;
int echo = 10;
long duration;
int distance;
float T;
float RH;
float velocity;
void setup() {
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
lcd.init();
lcd.backlight();
dht.begin();
Serial.begin(9600);//starts the serial communication
}
void loop() {
ultrasonic();
lcd_print();
dht_speed();
}
//Read the temperatur and Humidity from DHT22 sensor
//and calculate the velocity(c = 331.4 + (0.6 x T) + (0.0124 x RH)) of sound
void dht_speed() {
RH = dht.readHumidity();
T = dht.readTemperature();
velocity = 331.4 + (0.6*T) + (0.0124 * RH);
delay(1000);
}
//Calculate the distance from ultrasonic sensor
void ultrasonic() {
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
duration = pulseIn(echo, HIGH);
distance = (velocity*0.0001 * duration) / 2;
}
//Print velocity of sound and distance on the LCD display
void lcd_print() {
lcd.setCursor(0, 0); //sets the location at which subsequent text write
lcd.print("Velocity:");
lcd.print(velocity);
lcd.print("m/s");
lcd.setCursor(0, 1);
lcd.print("Distance: ");
lcd.print(distance);
lcd.print("CM");
}