#include <LiquidCrystal_I2C.h>
#include "DHT.h"
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
#define DHTPIN 2 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);
#define LED_RED 13 // defining where the led is connected
#define LED_BLUE 11
void setup()
{
Serial.begin(9600);
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Exercise 2");
delay(1000);
lcd.clear();
delay(1000);
lcd.setCursor(0,0);
lcd.print("Ideal temp: " + String(val));
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Humidity: " + String(val));
delay(1000);
lcd.clear();
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 80);
Serial.print("Ideal temp: " + String(val));
lcd.setCursor(0,0);
lcd.print("Ideal temp:" + String(val));
delay(1000);
Serial.print("Humidity: " + String(val));
lcd.setCursor(0,0);
lcd.print("Humidity:" + String(val));
delay(1000);
dht.begin();
pinMode(LED_RED, OUTPUT); // check
pinMode(LED_BLUE, OUTPUT); // check
}
void loop() {
// Wait a few seconds between measurements.
delay(2000);
// 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;
}
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("%Ideal Temperature: "));
Serial.print(t);
Serial.print(F("°C "));
Serial.print(f);
lcd.setCursor(0,0);
lcd.println("Temp: " + String(t));
delay(5000);
if(t > val) {
Serial.println("The Heater is switched off");
digitalWrite(LED_RED, LOW);
}
if(t < val) {
Serial.println("The DC Heater is switched on");
digitalWrite(LED_BLUE, LOW);
}
if(t > 2 + val) {
Serial.println("The DC motor is switched off");
digitalWrite(LED_BLUE, HIGH);}
}