#include "DHT.h"
#include <LiquidCrystal_I2C.h>
#include <Stepper.h>
//--------------
const int stepsPerRevolution = 200;
Stepper myStepper(stepsPerRevolution, 10, 11, 12, 13);
//--------------
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
//--------------
const float GAMMA = 0.7;
const float RL10 = 50;
//--------------
LiquidCrystal_I2C lcd(0x27, 20, 4); // Màn hình 20x4 I2C
//--------------
#define ECHO_PIN 3
#define TRIG_PIN 4
float readDistanceCM() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
int duration = pulseIn(ECHO_PIN, HIGH);
return duration * 0.034 / 2;
}
//--------------
int buzzerPin = 5;
//--------------
int inputPin = 6; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
//--------------
void setup() {
Serial.begin(115200);
dht.begin();
lcd.init();
lcd.backlight();
lcd.setCursor(6,1);
lcd.print("20004238");
lcd.setCursor(6,2);
lcd.print("20004191");
delay(3000);
lcd.clear();
lcd.setCursor(3,1);
lcd.print("Vuon Thong Minh");
delay(3000);
lcd.clear();
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(8, OUTPUT);
pinMode(7, OUTPUT);
pinMode(9, OUTPUT);
pinMode(inputPin, INPUT);
pinMode(buzzerPin, OUTPUT);
}
void loop() {
// Nhiệt độ và độ ẩm
float h = dht.readHumidity();
float t = dht.readTemperature();
lcd.setCursor(0,0);
lcd.print("ND: ");
lcd.setCursor(4,0);
lcd.print(t);
byte image01[8] = {B01110, B10001, B10001, B01110, B00000, B00000, B00000, B00000};
lcd.createChar(0, image01);
lcd.setCursor(9, 0);
lcd.write(byte(0));
lcd.setCursor(10,0);
lcd.print("C");
lcd.setCursor(0,1);
lcd.print("DA: ");
lcd.setCursor(4,1);
lcd.print(h);
lcd.setCursor(9,1);
lcd.print("%");
if (t<13) // Kịch bản 2: Nếu nhiệt độ phòng quá lạnh, cụ thể là dưới 13 độ C
{
lcd.setCursor(0,2);
lcd.print("ND thap");
delay(2000);
}
else
{
lcd.setCursor(0,2);
lcd.print(" ");
}
if (t>=29 && t<=50) // Kịch bản 3: Nếu nhiệt độ phòng đang rất nóng 29 độ
{
lcd.setCursor(0,2);
lcd.print("ND cao");
delay(2000);
}
else
{
lcd.setCursor(0,2);
lcd.print(" ");
}
if (t>50) // Kịch bản 1 : Nếu phát hiện nhiệt độ trên 50 độ C
{
lcd.setCursor(7,2);
lcd.print("CHAY !");
delay(3000);
digitalWrite(7, HIGH);
tone(buzzerPin, 1000);
delay(1000);
noTone(buzzerPin);
delay(1000);
}
else
{
lcd.setCursor(0,2);
lcd.print(" ");
digitalWrite(7, LOW);
}
if (h<60) // Kịch bản 4: Nếu độ ẩm dưới 60 % sẽ báo độ ẩm thấp cho biết phòng đang rất khô
{
lcd.setCursor(0,3);
lcd.print("DA thap");
delay(2000);
}
else
{
lcd.setCursor(0,3);
lcd.print(" ");
}
if (h>70) // Kịch bản 5: Nếu độ ẩm trên 70 % sẽ báo độ ẩm cao cho biết phòng đang rất ẩm
{
lcd.setCursor(0,3);
lcd.print("DA cao");
delay(2000);
}
else
{
lcd.setCursor(0,3);
lcd.print(" ");
}
// Đo mực nước
float distance = readDistanceCM();
if (distance < 50) // Kịch bản 6: Nếu phát hiện mực nước trong bình của phòng dưới 50 sẽ tự động bơm nước
// và ngược lại sẽ tắt khi mức nước đủ
{
lcd.setCursor(6,3);
lcd.print("Bom Nuoc!");
delay(3000);
digitalWrite(8, HIGH);
delay(500);
tone(buzzerPin,1000);
noTone(buzzerPin);
}
else
{
lcd.setCursor(6,3);
lcd.print(" ");
digitalWrite(8, LOW);
}
// Mở - Đóng cửa tự động
val = digitalRead(inputPin);
if (val == HIGH) // Kịch bản 7: Nếu phát hiện có người bước vào sẽ thông báo open NGƯỢC LẠI sẽ close sau đó
{
digitalWrite(9, HIGH);
if (pirState == LOW) {
tone(buzzerPin,1000);
delay(300);
noTone(buzzerPin);
myStepper.step(stepsPerRevolution);
lcd.setCursor(14,3);
lcd.print("OPEN!");
pirState = HIGH;
delay(2000);
}
} else {
digitalWrite(9, LOW);
if (pirState == HIGH)
{
myStepper.step(-stepsPerRevolution);
lcd.setCursor(14,3);
lcd.print("CLOSE!");
pirState = LOW;
delay(2000);
lcd.clear();
}
}
//Cảm biến ánh sáng
int analogValue = analogRead(A0);
float voltage = analogValue / 1024. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
if (lux > 400) {
lcd.setCursor(13,0);
lcd.print("Light!"); // Kịch bản 8: Nếu trời đang sáng thì sẽ thông báo LIGHT ngược lại nếu phòng tối sẽ thông báo DARK
} else {
lcd.setCursor(13,0);
lcd.print("Dark !");
}
lcd.setCursor(13, 1);
lcd.print(lux);
delay(100);
}