// import servo, LCD and DHT22
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
#include "DHTesp.h"
Servo door; //initialisation of servo
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 4 //defines for LCD
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
DHTesp dhtInside;
DHTesp dhtOutside;
//some variables for work
int servo = 0;
float pos = 0.0;
int moveSensor = 1;
int pirState = LOW;
int val = 0;
int green = 3;
int inside = 7;
int outside = 6;
void setup() {
pinMode(green, OUTPUT);
pinMode(moveSensor, INPUT);
door.attach(servo); //attaching the pin which is used for control the door
door.write(pos);
dhtInside.setup(inside, DHTesp::DHT22);
dhtOutside.setup(outside, DHTesp::DHT22); //initialisation of both DHT22
lcd.init(); //initialisation of LCD
lcd.backlight();
}
void loop() {
TempAndHumidity ins = dhtInside.getTempAndHumidity();
TempAndHumidity out = dhtOutside.getTempAndHumidity(); //taking temp and humidity from every DHT22
lcd.setCursor(0, 0);
lcd.print("Temperature inside: "); //text on LCD
lcd.setCursor(6, 1);
lcd.print(String(ins.temperature, 1) + " deg."); //text with temp on lcd
lcd.setCursor(0, 2);
lcd.print("Temperature outside: ");
lcd.setCursor(6, 3);
lcd.print(String(out.temperature, 1) + " deg.");
if (digitalRead(moveSensor)) // Check for movement
{
digitalWrite(green, HIGH);
while (pos>0) // Check that the position won't go lower than 0°
{
door.write(pos); // Set the doors's position to "pos" value
pos-=1.0; // Decrement "pos" of "step" value
delay(5); // Wait 5ms for the door to reach the position
}
}
if (!digitalRead(moveSensor)) // Check for the no movement
{
digitalWrite(green, LOW);
while (pos<180) // Check that the position won't go higher than 180°
{
door.write(pos); // Set the door's position to "pos" value
pos+=1.0; // Increment "pos" of "step" value
delay(5); // Wait 5ms for the door to reach the position
}
}
}