// Include DHT Libraries from Adafruit
// Dependant upon Adafruit_Sensors Library
#include "DHT.h";
// Include NewPing Library
#include "NewPing.h"
// include the library code:
#include <LiquidCrystal_I2C.h>
// Define Constants
#define DHTPIN 7 // DHT-22 Output Pin connection
#define DHTTYPE DHT11 // DHT Type is DHT 22 (AM2302)
#define TRIGGER_PIN 10
#define ECHO_PIN 13
#define MAX_DISTANCE 400
// Define I2C Address - change if reqiuired
const int i2c_addr = 0x27;
LiquidCrystal_I2C lcd(i2c_addr, 16,2);
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
// Define Variables
float hum; // Stores humidity value in percent
float temp; // Stores temperature value in Celcius
float duration; // Stores HC-SR04 pulse duration value
float distance; // Stores calculated distance in cm
float soundsp; // Stores calculated speed of sound in M/S
float soundcm; // Stores calculated speed of sound in cm/ms
int iterations = 5;
// Initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
Serial.begin (9600);
dht.begin();
// Print a message to the LCD.
lcd.setCursor(2, 0);
lcd.print("MEASUREMENTS:");
}
void loop()
{
delay(1000); // Delay so DHT-22 sensor can stabalize
hum = dht.readHumidity(); // Get Humidity value
temp= dht.readTemperature(); // Get Temperature value
// Calculate the Speed of Sound in M/S
soundsp = 331.4 + (0.606 * temp) + (0.0124 * hum);
// Convert to cm/ms
soundcm = soundsp / 10000;
duration = sonar.ping_median(iterations);
// Calculate the distance
distance = (duration / 2) * soundcm;
// Send results to Serial Monitor
Serial.print("Sound: ");
Serial.print(soundsp);
Serial.print(" m/s, ");
Serial.print("Humid: ");
Serial.print(hum);
Serial.print(" %, Temp: ");
Serial.print(temp);
Serial.print(" C, ");
Serial.print("Distance: ");
if (distance >= 400 || distance <= 2) {
Serial.print("Out of range");
}
else {
Serial.print(distance);
Serial.print(" cm");
delay(500);
}
lcd.setCursor(0, 1);
lcd.print("D:");
lcd.print(distance);
lcd.setCursor(9, 1);
lcd.print("T:");
lcd.print(temp);
Serial.println(" ");
}