#include "DHT.h"
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
#define pumpPin 4
#define fanPin 0
#define lampPin 2
#define buttonmodePin 32
#define buttonpumpPin 33
#define buttonfanPin 25
#define buttonlampPin 26
#define dhtPin 15
#define DHTTYPE DHT22
DHT dht(dhtPin, DHTTYPE);
int h;
int t;
unsigned long curTime = 0;
unsigned long pPre = 0;
unsigned long pfanPre = 0;
unsigned long pfan1Pre = 0;
unsigned long plampPre = 0;
unsigned long plamp1Pre = 0;
unsigned long pmodePre = 0;
int pTime = 1000;
int pfanTime = 1000;
int pfan1Time = 1000;
int plampTime = 1000;
int plamp1Time = 1000;
int pmodeTime = 50;
int ledPumpState = LOW;
int ledFanState = LOW;
int ledLampState = LOW;
int buttonFan = 0;
int buttonPump = 0;
int buttonLamp = 0;
int buttonMode = 0;
int bfanState = LOW;
int bpumpState = LOW;
int blampState = LOW;
int bmodeState = LOW;
void setup() {
Serial.begin(115200);
pinMode(pumpPin, OUTPUT);
pinMode(fanPin, OUTPUT);
pinMode(lampPin, OUTPUT);
pinMode(dhtPin, INPUT);
pinMode(buttonfanPin, INPUT_PULLUP);
pinMode(buttonpumpPin, INPUT_PULLUP);
pinMode(buttonlampPin, INPUT_PULLUP);
pinMode(buttonmodePin, INPUT_PULLUP);
dht.begin();
lcd.begin(20, 4);
lcd.init();
lcd.backlight();
}
void loop() {
curTime = millis();
h = dht.readHumidity();
t = dht.readTemperature();
buttonMode = digitalRead(buttonmodePin);
///////////////////////////////////////////////switch mode
if (curTime - pmodePre >= pmodeTime) {
pmodePre = curTime;
if (buttonMode == LOW) {
bmodeState = !bmodeState;
bpumpState = LOW;
bfanState = LOW;
blampState = LOW;
digitalWrite(pumpPin, bpumpState);
digitalWrite(fanPin, bfanState);
digitalWrite(lampPin, blampState);
}
if (bmodeState == LOW) {
lcd.setCursor(0, 0);
lcd.print("Mode : ");
lcd.print("AUTO");
lcd.print(" ");
lcdShow();
humControl();
temperaturControl();
} else {
lcd.setCursor(0, 0);
lcd.print("Mode : ");
lcd.print("MANUAL");
lcd.print(" ");
manual();
}
}
}
void lcdShow() {
lcd.setCursor(0, 1);
lcd.print("Hum :");
lcd.print(h);
lcd.print(" ");
lcd.setCursor(10, 1);
lcd.print("Temp :");
lcd.print(t);
lcd.print(" ");
lcd.setCursor(0, 2);
if (digitalRead(fanPin)) {
lcd.print("Fan :on ");
} else {
lcd.print("Fan :off ");
}
lcd.setCursor(10, 2);
if (digitalRead(lampPin)) {
lcd.print("Lamp :on ");
} else {
lcd.print("Lamp :off ");
}
lcd.setCursor(0, 3);
if (digitalRead(pumpPin)) {
lcd.print("Pump:on ");
} else {
lcd.print("Pump:off ");
}
}
void humControl() {
if (h <= 40) {
if (curTime - pPre >= pTime) {
pPre = curTime;
if (ledPumpState == LOW) {
ledPumpState = HIGH;
pTime = 100;
} else {
ledPumpState = LOW;
pTime = 100;
}
digitalWrite(pumpPin, ledPumpState);
}
} else if (h > 40 && h < 80) {
if (curTime - pPre >= pTime) {
pPre = curTime;
if (ledPumpState == LOW) {
ledPumpState = HIGH;
pTime = 1000;
} else {
ledPumpState = LOW;
pTime = 1000;
}
digitalWrite(pumpPin, ledPumpState);
}
} else {
digitalWrite(pumpPin, LOW);
}
}
void temperaturControl() {
///////////////// lamp off, fan on (t>40), lamp on, fan off (t<20)
if (t <= 20) {
if (curTime - plampPre >= plampTime) {
plampPre = curTime;
if (ledLampState == LOW) {
ledLampState = HIGH;
plampTime = 100;
} else {
ledLampState = LOW;
plampTime = 100;
}
digitalWrite(lampPin, ledLampState);
digitalWrite(fanPin, LOW);
}
} else if (t >= 40) {
if (curTime - pfanPre >= pfanTime) {
pfanPre = curTime;
if (ledFanState == LOW) {
ledFanState = HIGH;
pfanTime = 100;
} else {
ledFanState = LOW;
pfanTime = 100;
}
digitalWrite(fanPin, ledFanState);
digitalWrite(lampPin, LOW);
}
} else {
if (curTime - plamp1Pre >= plamp1Time) {
plamp1Pre = curTime;
if (ledLampState == LOW) {
ledLampState = HIGH;
plamp1Time = 2000;
} else {
ledLampState = LOW;
plamp1Time = 1000;
}
digitalWrite(lampPin, ledLampState);
}
if (curTime - pfan1Pre >= pfan1Time) {
pfan1Pre = curTime;
if (ledFanState == LOW) {
ledFanState = HIGH;
pfan1Time = 2000;
} else {
ledFanState = LOW;
pfan1Time = 1000;
}
digitalWrite(fanPin, ledFanState);
}
}
}
void manual() {
//////////////////////////////////////////mode manual
buttonFan = digitalRead(buttonfanPin);
delay(50);
if (buttonFan == 0) {
bfanState = !bfanState;
}
digitalWrite(fanPin, bfanState);
buttonPump = digitalRead(buttonpumpPin);
delay(50);
if (buttonPump == 0) {
bpumpState = !bpumpState;
}
digitalWrite(pumpPin, bpumpState);
buttonLamp = digitalRead(buttonlampPin);
delay(50);
if (buttonLamp == 0) {
blampState = !blampState;
}
digitalWrite(lampPin, blampState);
lcd.setCursor(0, 1);
if (bfanState) {
lcd.print("Fan : On ");
} else {
lcd.print("Fan : Off ");
}
lcd.setCursor(0, 2);
if (bpumpState) {
lcd.print("Pump : On ");
} else {
lcd.print("Pump : Off ");
}
lcd.setCursor(0, 3);
if (blampState) {
lcd.print("Lamp : On ");
} else {
lcd.print("Lamp : Off ");
}
}