งานปฏิบัติที่ 19
wiring diagram
รูปที่1
Code 1
#include <SoftwareSerial.h>
#include <DHT11.h>
//=============================================
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//LiquidCrystal_I2C lcd(0x27, 16, 2);
LiquidCrystal_I2C lcd(0x3F, 16, 2);
SoftwareSerial ArduinoSerial(3, 2); // RX, TX
//=============================================
int SW_ON = 4,SW_OFF = 5,pin=6;//กำหนดขา INPUT
int Sun = 9,CSun = 0,Relay1 = 12, Relay2 = 11; //กำหนดขา OUTPUT
int ATL = A0,ATH = A1;//กำหนดขา Analog
int VTL1 = 0,VTH1 = 0,TL = 0,TH =0,VRHL = 50,VRHH = 80;
DHT11 dht11(pin);
//=============================================
void setup()
{
lcd.begin();
Serial.begin(115200);
ArduinoSerial.begin(4800);
while (!Serial){
;
}
//=====================================================
lcd.setCursor(0, 0);lcd.print("Elec Pattayatech");
lcd.setCursor(0, 1);lcd.print("Smart Farm 2016 ");
delay(5000);
//====================================================
pinMode(Relay1, OUTPUT);pinMode(Relay2, OUTPUT);
pinMode(SW_ON, INPUT); pinMode(SW_OFF, INPUT); pinMode(Sun, INPUT);
digitalWrite(Relay1, HIGH);digitalWrite(Relay2, HIGH);
lcd.setCursor(0, 0);lcd.print(" ");
lcd.setCursor(0, 1);lcd.print(" ");
}
//=============================================================
void loop()
{
while (ArduinoSerial.available() > 0) {
float val = ArduinoSerial.parseFloat();
if (ArduinoSerial.read() == '\n') {
if (val == 10) {
digitalWrite(13, 1);
} else if (val == 11) {
digitalWrite(13, 0);
}
Serial.println(val);
}
}
delay(100);
//========ปรับค่าอุณหภูมิอ้งอิง====================================
CSun = digitalRead(Sun);
int err;
float temp, humi;
if((err=dht11.read(humi, temp))==0)
{
VTL1 = analogRead(ATL);
VTH1 = analogRead(ATH);
TL = map(VTL1,0,1023,10,30);
TH = map(VTH1,0,1023,25,45);
Serial.print("TL :");Serial.print(TL);Serial.println("*C");
Serial.print("TH :");Serial.print(TH);Serial.println("*C");
//===========================================================
int Stemp =(int)(temp);int Shumi=(int)(humi);//ตัดทศนิยม
Serial.print("temp :");Serial.print(Stemp);Serial.println("*C");
Serial.print("humidity :");Serial.print(Shumi);Serial.println("%");
Serial.print("SUN :");Serial.println(CSun);//Serial.println();
//===================================================
lcd.setCursor(0, 0);lcd.print("TL:");lcd.print(TL);
lcd.setCursor(6, 0);lcd.print("TH:");lcd.print(TH);
lcd.setCursor(12, 0);lcd.print("S1:");lcd.print(!CSun);
lcd.setCursor(0, 1);lcd.print("TN:");lcd.print(Stemp);
lcd.setCursor(6, 1);lcd.print("RH:");lcd.print(Shumi);
lcd.setCursor(12, 1);lcd.print("P1:0");lcd.print(Shumi);
delay(1000);
//===================================================
//=======ควบคุมอุณหภูมิ===========================
if(temp < TL && temp < TH ){
digitalWrite(Relay1, HIGH);//ปั๊มน้ำหยุดทำงาน
lcd.setCursor(12, 1);lcd.print("P1:0");
delay(100);
digitalWrite(Relay2, LOW);//หลอดไฟทำงาน
lcd.setCursor(12, 0);lcd.print("L1:1");
delay(60000);//หน่วงเวลา 1 นาที
}else if(temp > TL && temp > TH){
digitalWrite(Relay2, HIGH);//หลอดไฟไม่ทำงาน
lcd.setCursor(12, 0);lcd.print("L1:0");
delay(100);
digitalWrite(Relay1, LOW);//ปั๊มน้ำทำงาน
lcd.setCursor(12, 1);lcd.print("P1:1");
delay(60000);//หน่วงเวลา 1 นาที
}else if(temp > TL && temp < TH){
//=======ควบคุมความชื้น===========================
if(humi < VRHL && humi < VRHH){
digitalWrite(Relay2, HIGH);//หลอดไฟไม่ทำงาน
lcd.setCursor(12, 0);lcd.print("L1:0");
delay(100);
digitalWrite(Relay1, LOW);//ปั๊มน้ำทำงาน
lcd.setCursor(12, 1);lcd.print("P1:1");
delay(60000);//หน่วงเวลา 1 นาที
}else if(humi > VRHL && humi > VRHH){
digitalWrite(Relay1, HIGH);//ปั๊มน้ำหยุดทำงาน
lcd.setCursor(12, 1);lcd.print("P1:0");
delay(100);
digitalWrite(Relay2, LOW);//หลอดไฟทำงาน
lcd.setCursor(12, 0);lcd.print("L1:1");
delay(60000);//หน่วงเวลา 1 นาที
}else if(humi > VRHL && humi < VRHH){
//delay(1000);
digitalWrite(Relay1, HIGH);//ปั๊มน้ำหยุดทำงาน
lcd.setCursor(12, 1);lcd.print("P1:0");
digitalWrite(Relay2, HIGH);//หลอดไฟไม่ทำงาน
lcd.setCursor(12, 0);lcd.print("L1:0");
//หน่วงเวลา 1 นาที
}
//=======ENDควบคุมความชื้น===========================
}
//=======ENDควบคุมอุณหภูมิ===========================
}
else
{
Serial.println();Serial.print("Error No :");
Serial.print(err);Serial.println();
}
delay(DHT11_RETRY_DELAY);
}
ความคิดเห็น
แสดงความคิดเห็น