#include "DHT.h"
#include <LiquidCrystal_I2C.h>
#define fanPin 9
#define lampPin 10
#define buttonfanPin 11
#define buttonpumpPin 12
#define buttonlampPin 13
#define buttonmodePin 7
// define ไม่ใช้แมเมอรี่ ฝังใน Arduino เป็นค่าคงที่
DHT dht(2, DHT22);
LiquidCrystal_I2C lcd(0x27, 20, 4);
// ถ้าใช้ #define จะเป็นการ fix pumpin8 ค่าคงที่ที่กำหนดใน Arduino
// #define pumpPin 8; ' ไม่ใช้พื้นที่หน่วยความจำโปรแกรมใด ๆ บนชิป
//ใช้ #define หรือ ใช้ int ก็ได้
int pumpPin = 8 ;
int h ;
int t ;
int buttonfan ;
int buttonpump ;
int buttonlamp ;
int buttonmode ;
int bfanState = HIGH;
int bpumpState = HIGH;
int blampState - HIGH;
int bmodeState = HIGH;
// millis
unsigned long curTime = 0;
unsigned long pPre = 0;
int pTime = 1000;
int pState = LOW ;
unsigned long p2Pre = 0;
int p2Time = 1000;
int p2State = LOW ;
unsigned long fanPre = 0;
int fanTime = 1000;
int fanState = LOW ;
unsigned long fan2Pre = 0;
int fan2Time = 1000;
int fan2State = LOW ;
unsigned long lampPre = 0;
int lampTime = 1000;
int lampState = LOW ;
unsigned long lamp2Pre = 0;
int lamp2Time = 1000;
int lamp2State = LOW ;
//--------------------------------------
void setup() {
Serial.begin(115200);
pinMode(pumpPin, OUTPUT); // relay 1 คุมปั้มน้ำ
pinMode(fanPin, OUTPUT); // relay 2 คุมพัดลม
pinMode(lampPin, OUTPUT); // relay 3 คุมฮิตเตอร์
pinMode(buttonfanPin, INPUT_PULLUP);
pinMode(buttonpumpPin, INPUT_PULLUP);
pinMode(buttonlampPin, INPUT_PULLUP);
pinMode(buttonmodePin, INPUT_PULLUP);
// Pullup ข้างในมีต่อไฟ 3v.ต่อไฟให้เลย ไม่ต้องไปต่อในวงจร
dht.begin();
lcd.init();
lcd.backlight();
}
//+++++++++++ M A I N L O O P +++++++++++++++
void loop() {
curTime = millis();
//manual (); //Manual button + lcd print
// temphum(); //Auto : pumb 40-80, fan 20-40, lamp 20-40
} // end loop ++++++++++
//+++++++++++++++++++++++++++++++++++++++++++++
void manual () {
// ปกติ button = 1 ถ้ามีการกด = 0
//Fan
buttonfan = digitalRead(buttonfanPin);
//Serial.print("buttonfan : ");
//Serial.println(buttonfan);
//delay(200);
if (buttonfan == 0) {
digitalWrite(fanPin, bfanState);
bfanState = !bfanState;
}
//Pump
buttonpump = digitalRead(buttonpumpPin);
//Serial.print("buttonpump : ");
//Serial.println(buttonpump);
//delay(200);
if (buttonpump == 0) {
digitalWrite(pumpPin, bpumpState);
bpumpState = !bpumpState;
}
//Lamp
buttonlamp = digitalRead(buttonlampPin);
//Serial.print("buttonlamp : ");
//Serial.println(buttonlamp);
//delay(200);
if (buttonlamp == 0) {
digitalWrite(lampPin, blampState);
blampState = !blampState;
}
//444444444444444444444444444444444444
// lcd print
//444444444444444444444444444444444444
lcd.setCursor(0, 0);
lcd.print("Mode Manual ");
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print("Fan : ");
lcd.print(bfanState);
if (bfanState == 1) {
lcd.print(" OFF") ;
}else{lcd.print(" ON ")
;}
lcd.print(" ");
lcd.setCursor(0, 2);
lcd.print("Pump : ");
lcd.print(bpumpState);
if (bpumpState == 1) {
lcd.print(" OFF") ;
}else{lcd.print(" ON ")
;}
lcd.print(" ");
lcd.print(" ");
lcd.setCursor(0, 3);
lcd.print("Lamp : ");
lcd.print(blampState);
if (blampState == 1) {
lcd.print(" OFF") ;
}else{lcd.print(" ON ")
;}
lcd.print(" ");
}
//4444444444444444444444444444444444444444444444
//llllllllllaaaaaaaaaaaammmmmmmmmmmmppppppppppppp//
void temphum(){
// DHT22
h = dht.readHumidity();
t = dht.readTemperature();
lcd.setCursor(0, 0);
lcd.print("Hum: ");
lcd.print(h);
lcd.print(" ");
lcd.print("Temp: ");
lcd.print(t);
lcd.print(" ");
//fanPin แม้จะเป็นOutput ก็สามารถใช้ Read ได้ "แอบอ่าน"
//Output ใช้ digitalWrite , Input ใช้ digitalRead
lcd.setCursor(0, 1);
//if(fanState == HIGH){
if(digitalRead(fanPin) == HIGH) {
lcd.print("Fan: ON");
}else{
lcd.print("Fan: OFF");
}
lcd.print(" ");
if(digitalRead(lampPin) == HIGH) {
lcd.print("Lamp: ON");
}else{
lcd.print("Lamp: OFF");
}
lcd.print(" ");
lcd.setCursor(0, 2);
if(digitalRead(pumpPin) == HIGH) {
lcd.print("Pump: ON");
}else{
lcd.print("Pump: OFF");
}
lcd.print(" ");
//llllllllllllllllllllllllllllllllllllllllll
//TTTTTTTTeeeeeeeemmmmmmmmpppppppppppp
// lamp : คุมอุณหภูมิให้อยู่ในช่วง องศา
// ถ้าอุณหภูมิต่ำต้องเพิ่มความร้อน เปิดฮิตเตอร์ ถ้าอุหภฺมิสูงต้องเพิ่มความเย็น เปิดพัดลม
if (t < 20) {
//1111111111111111111111111111111
if (curTime - lampPre >= lampTime) {
lampPre = curTime;
//lampTime ใส่เงื่อนไขให้ติดดับไม่เท่ากัน
if (lampState == LOW) { lampState = HIGH;
lampTime = 3000;
} else { lampState = LOW;
lampTime = 500;
}
digitalWrite(lampPin, lampState);
}
//1111111111111111111111111111111
} else if(t>=20 && t<=40){
//2222222222222222222222222222222
if (curTime - lamp2Pre >= lamp2Time) {
lamp2Pre = curTime;
//lamp2Time ใส่เงื่อนไขให้ติดดับไม่เท่ากัน
if (lamp2State == LOW) { lamp2State = HIGH;
lamp2Time = 500;
lcd.setCursor(0, 1);
} else { lamp2State = LOW;
lamp2Time = 3000;
lcd.setCursor(0, 1);
}
digitalWrite(lampPin, lamp2State);
}
//2222222222222222222222222222222222
}else{
digitalWrite(lampPin, LOW);
}
//333333333333333333333333333333333333
//llllllllllaaaaaaaaaaaammmmmmmmmmmmppppppppppppp//
//TTTTTTTTeeeeeeeemmmmmmmmpppppppppppp
// Temp 20-40 : คุมอุณหภูมิให้อยู่ในช่วง 20-40 องศา
// ถ้าอุณหภูมิต่ำต้องเพิ่มความร้อน เปิดฮิตเตอร์ ถ้าอุหภฺมิสูงต้องเพิ่มความเย็น เปิดพัดลม
// t >40 fan on + lam off , t<20 fan off + lam on
if (t > 40) {
//1111111111111111111111111111111
if (curTime - fanPre >= fanTime) {
fanPre = curTime;
//fanTime ใส่เงื่อนไขให้ติดดับไม่เท่ากัน
if (fanState == LOW) { fanState = HIGH;
fanTime = 3000;
} else { fanState = LOW;
fanTime = 500; }
digitalWrite(fanPin, fanState);
}
//1111111111111111111111111111111
} else if(t>=20 && t<=40){
//2222222222222222222222222222222
if (curTime - fan2Pre >= fan2Time) {
fan2Pre = curTime;
//fan2Time ใส่เงื่อนไขให้ติดดับไม่เท่ากัน
if (fan2State == LOW) { fan2State = HIGH;
fan2Time = 500;
} else { fan2State = LOW;
fan2Time = 3000; }
digitalWrite(fanPin, fan2State);
}
//2222222222222222222222222222222222
}else{
digitalWrite(fanPin, LOW);
}
//333333333333333333333333333333333333
//ttttttteeeeeeeeeeeeemmmmmmmmmmmpppppppppp//
// hhhhhhhhhhhuuuuuuuuuuuuuummmmmmmmmmmmmm//
if (h < 40){
//1111111111111111111111111111111
if (curTime - pPre >= pTime) {
pPre = curTime;
//pTime ใส่เงื่อนไขให้ติดดับไม่เท่ากัน
if (pState == LOW) { pState = HIGH;
pTime = 3000;
} else { pState = LOW;
pTime = 500; }
digitalWrite(pumpPin, pState);
}
//1111111111111111111111111111111
} else if(h>=40 && h<=80){
//2222222222222222222222222222222
if (curTime - p2Pre >= p2Time) {
p2Pre = curTime;
//p2Time ใส่เงื่อนไขให้ติดดับไม่เท่ากัน
if (p2State == LOW) { p2State = HIGH;
p2Time = 500;
} else { p2State = LOW;
p2Time = 3000; }
digitalWrite(pumpPin, p2State);
}
//2222222222222222222222222222222222
// จำเป็นต้องเช็คเพิ่ม เพื่อเพิ่มความเสถียร
// เพราะถ้า ค่าเป็น0 แล้วกระโดดเป็น100 จะติดค้าง
else {
digitalWrite(pumpPin, LOW);
}
//333333333333333333333333333333333333
}
}
//hhhhhhhhhhhuuuuuuuuuuuuuummmmmmmmmmmmmm//
// ความชื่้น Hum <40 pump on , Hum >80 pump off
/*
//===============================================
// เงื่อนไขนี้ ติดดับเท่ากัน หรือ ติดตลอด ไม่ละเอียด ไม่สามารถใข้งานได้จริง
//if (h < 40 ) {
//digitalWrite(pumpPin , HIGH);
//} else if(h > 80){
// digitalWrite(pumpPin , LOW);}
//==============================================
//+++++++++++++++++++++++++++++++++++++++++++++
// นำ millis ไม่ช่วยให้การทำงานของปั้ม ละเอียดขึ้น
// เช็คทุก 1000 ทำแล้วหยุดเช็ค 1000
// millis
//nsigned long curTime = 0;
//unsigned long pPre = 0;
//int pTime = 1000;
// if (millis() - previousMillis >= TIME_INTERVAL) {
// previousMillis = millis();
*/