// Niña Gabriel Villaruel
// dht22 sensor
#include "DHT.h"
#define DHTPIN 4 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);
// servo motor
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// analog temperature sensors
const float BETA = 3950; // should match the Beta Coefficient of the thermistor
int temp1 = A1;
int temp2 = A2;
int temp3 = A3;
// 20x4 lcd
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);
void setup() {
Serial.begin(9600);
// analog temperature sensor
pinMode(temp1,INPUT);
pinMode(temp2,INPUT);
pinMode(temp3, INPUT);
// dht22 sensor
dht.begin(); //initialize
// servo motor
myservo.attach(9); // attaches the servo on pin 9 to the servo object
// 20x4 lcd
lcd.init(); // initialize the lcd
lcd.backlight();
}
void loop() {
delay(1000);
lcd.clear();
// RH reading
float h = dht.readHumidity();
// temperature reading
int analogValue1 = analogRead(temp1);
int analogValue2 = analogRead(temp2);
int analogValue3 = analogRead(temp3);
// temperature conversion
float celsius1 = 1 / (log(1 / (1023. / analogValue1 - 1)) / BETA + 1.0 / 298.15) - 273.15;
float celsius2 = 1 / (log(1 / (1023. / analogValue2 - 1)) / BETA + 1.0 / 298.15) - 273.15;
float celsius3 = 1 / (log(1 / (1023. / analogValue3 - 1)) / BETA + 1.0 / 298.15) - 273.15;
// averaging temperature
float avet = (celsius1 + celsius2 + celsius3) / 3.0;
// show lcd values
lcd.setCursor (12,2);
lcd.print("RH");
lcd.setCursor (12,3);
lcd.print(h);
lcd.print(" %");
lcd.setCursor(0,0);
lcd.print("T1 ");
lcd.print(celsius1);
lcd.print(" C");
lcd.setCursor (0,1);
lcd.print("T2 ");
lcd.print(celsius2);
lcd.print(" C");
lcd.setCursor (0,2);
lcd.print("T3 ");
lcd.print(celsius3);
lcd.print(" C");
lcd.setCursor (12,0);
lcd.print("Tave");
lcd.setCursor (12,1);
lcd.print(avet);
lcd.print(" C");
// show warning and servo movement in conditions
// conditions
if(h>=70 && h<=90 && avet>=22 && avet<=28) //condition 1
{
myservo.write(0);
lcd.setCursor(0,3);
lcd.print("OPTIMAL");
}
else if(h>90 && avet>=22 && avet<=28) // condition 2
{
myservo.write(180);
lcd.setCursor(0,3);
lcd.print("RH HIGH!");
}
else if(h<70 && avet>=22 && avet<=28) // condition 3
{
myservo.write(90);
lcd.setCursor(0,3);
lcd.print("RH LOW!");
}
else if(h>=70 && h<=90 && avet<22) // condition 4
{
myservo.write(90);
lcd.setCursor(0,3);
lcd.print("TEMP LOW!");
}
else if(h>=70 && h<=90 && avet>28) // condition 5
{
myservo.write(180);
lcd.setCursor(0,3);
lcd.print("TEMP HIGH!");
}
else if(h<70 && avet<22 || h>90 && avet>28) // condition 6
{
myservo.write(30);
lcd.setCursor(0,3);
lcd.print("CRITICAL!");
}
}