#include "DHT.h"//
#define DHTPIN 6 //
#define DHTTYPE DHT22 //
DHT dht(DHTPIN, DHTTYPE);
#include <LiquidCrystal_I2C.h>//
LiquidCrystal_I2C lcd(0x27, 16, 4);//
#include "RTClib.h"//
RTC_DS1307 rtc;//
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
#define LDR_PIN A0
const float GAMMA = 0.7;
const float RL10 = 50;
#include <Servo.h>
Servo servo1;
Servo servo2;
int pos = 0;
void setup(){
Serial.begin(9600);//ตั้งความเร็วสื่อสารที่ 9600 และสั่งให้เริ่มทำงาน
lcd.init();// initialize the lcd
dht.begin();//
lcd.backlight();//
lcd.begin(16, 4);//
servo1.attach(5);
servo2.attach(3);
}
void loop(){
time();//
read_dht();//
light();
servo(); //
}
void read_dht(){
float h = dht.readHumidity();//
float t = dht.readTemperature();//
float f = dht.readTemperature(true);//
Serial.print(F("Humidity: "));//แสดง Humidity:
Serial.print(h);//แสดง
Serial.print(F("% Temperature: "));//แสดง % Temperature:
Serial.print(t);//แสดง
Serial.print(F("°C "));//แสดง
Serial.print(f);//แสดง
Serial.println(F("°F"));//แสดง
lcd.home();//
lcd.setCursor(0, 0);//
lcd.print("Temp : ");//แสดง
lcd.print(t);//แสดง
lcd.setCursor(0, 1);//
lcd.print("Humidity : ");//แสดง
lcd.print(t);//แสดง
lcd.print("%");//แสดง
lcd.println();
delay(1000);
}
void time(){
rtc.begin();//
DateTime now = rtc.now();//
Serial.print("Current time: ");//แสดง Current time:
Serial.print(now.year(), DEC);//แสดง
Serial.print('/');//แสดง /
Serial.print(now.month(), DEC);//แสดง
Serial.print('/');//แสดง /
Serial.print(now.day(), DEC);//แสดง
Serial.print(" (");//แสดง (
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);//แสดง
Serial.print(") ");//แสดง )
Serial.print(now.hour(), DEC);//แสดง
Serial.print(':');//แสดง :
Serial.print(now.minute(), DEC);//แสดง
Serial.print(':');//แสดง :
Serial.print(now.second(), DEC);//แสดง
Serial.println();
delay(500);//
}
void servo(){
float t = dht.readTemperature();
DateTime now = rtc.now();
int H= now.hour();
int analogValue = analogRead(A0);//อ่านค่าจาก A0
float voltage = analogValue / 1024. * 5;//สูตรคิดค่า volt
float resistance = 2000 * voltage / (1 - voltage / 5);//สูตรคิดค่า Ohm
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));//สูตรคิดค่า lux
if ((t>30)||(H%2==0)||(lux > 50)){
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
servo1.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
servo1.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
servo2.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
servo2.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
else{
return;
}
}
void light(){
int analogValue = analogRead(A0);//อ่านค่าจาก A0
float voltage = analogValue / 1024. * 5;//สูตรคิดค่า volt
float resistance = 2000 * voltage / (1 - voltage / 5);//สูตรคิดค่า Ohm
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));//สูตรคิดค่า lux
lcd.setCursor(0,2);//
lcd.print("Lux: ");//
lcd.print(lux);//
lcd.print(" ");//
if (lux > 50) {//
lcd.setCursor(0,3);
lcd.print("Light!");
} else {
lcd.setCursor(0,3);
lcd.print("Dark!");//
}
delay(900);//
}