#include <avr/wdt.h>
#include "DHT.h"
#include <LiquidCrystal_I2C.h>
#define DHTPIN A1 // what pin we're connected to
#define DHTTYPE DHT11
LiquidCrystal_I2C lcd(0x27, 16, 2);
DHT dht(DHTPIN, DHTTYPE);
// PARAMETERS
int hON = 65; // Turn Off the fan at humidity
int hOFF = 65; // Turn Off the fan at humidity
unsigned long MinOFFTime = 300000; // minimum time after switch Off to turn back ON again
const int pResistor = A0;
int val_lumina;
const int BUTTON_PIN = 6; // the number of the pushbutton pin
const int LONG_PRESS_TIME = 1000; // 1000 milliseconds
// Variables will change:
int lastState = LOW; // the previous state from the input pin
int currentState; // the current reading from the input pin
unsigned long pressedTime = 0;
unsigned long releasedTime = 0;
int releu = 7;
// VARIABLES
unsigned long lastmeasurement = 0;
unsigned long switchedonmillis = 0;
unsigned long switchedoffmillis = 0;
float t = 0;
float h = 0;
float hone = 0;
float htwo = 0;
float hthree = 0;
byte counter = 0;
float haverage = 0;
byte fan = 0;
void setup() {
fanoff(); // turn off fan
Serial.begin(9600);
pinMode(releu, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
dht.begin();
lcd.init(); // initialize the LCD
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" VERIFICAM ");
lcd.setCursor(0, 1);
lcd.print(" SENZORII");
measurement();
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp. : ");
lcd.print(t);
lcd.print(" ");
lcd.print((char)223);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Umid. : ");
lcd.print(h);
lcd.print(" %");
delay(2000);
}
void measurement() {
if (counter >= 3) {
counter = 0;
}
counter++;
h = dht.readHumidity();
t = dht.readTemperature();
if (isnan(t) || isnan(h)) {
Serial.println("Failed to read from DHT");
}
Serial.println(counter);
if (counter == 1) {
hone = h;
}
if (counter == 2) {
htwo = h;
}
if (counter == 3) {
hthree = h;
}
haverage = (hone + htwo + hthree) / 3;
lastmeasurement = millis();
}
void fanon() {
digitalWrite(releu, LOW);
Serial.println("FAN ON");
fan = 1;
delay(30000);
}
void fanoff() {
digitalWrite(releu, HIGH);
Serial.println("FAN OFF");
fan = 0;
}
void LCD() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp. : ");
lcd.print(t);
lcd.print(" ");
lcd.print((char)223);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Umid. : ");
lcd.print(h);
lcd.print(" %");
}
void verificalumina() {
val_lumina = analogRead(pResistor);
if (val_lumina > 45) {
lcd.backlight();
} else {
lcd.noBacklight();
delay(50);
}
Serial.println(String("Intensitate lumina : ") + val_lumina);
}
void buton(){
// read the state of the switch/button:
currentState = digitalRead(BUTTON_PIN);
if(lastState == HIGH && currentState == LOW) // button is pressed
pressedTime = millis();
else if(lastState == LOW && currentState == HIGH) { // button is released
releasedTime = millis();
long pressDuration = releasedTime - pressedTime;
if( pressDuration > LONG_PRESS_TIME )
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp. : ");
lcd.print(t);
lcd.print(" ");
lcd.print((char)223);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print(" AERISIM!");
fanon(); // turn on
delay(600000);
fanoff(); // turn on
}
// save the the last state
lastState = currentState;
}
void loop() {
buton();
if ((millis() > 15001) && ((millis() - 15000) > lastmeasurement)) {
measurement();
Serial.print("Umiditate: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperatura: ");
Serial.print(t);
Serial.println(" *C");
Serial.println(String("Val. umid. 1: ") + hone);
Serial.println(String("Val. umid. 2: ") + htwo);
Serial.println(String("Val. umid. 3: ") + hthree);
Serial.println(String("Media ultimelor 3 masuratori de umiditate: ") + haverage);
verificalumina();
LCD();
if (fan == 0) // if it's OFF
{
// CHECK IF IT WAS OFF FOR AT LEAST 120 seconds AND IF it was turned OFF at all
if (haverage >= hON) {
fanon();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" AERISIM!");
lcd.setCursor(0, 1);
lcd.print("Umid. : ");
lcd.print(h);
lcd.print(" %");
}
}
// if it's working
if (fan == 1) {
if (haverage < hOFF) // KEEP IT FOR MINIMUM 60 seconds
{
fanoff();
}
}
}
}