#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#define I2C_ADDR1 0x27
#define LCD_COLUMNS1 16
#define LCD_LINES1 2
#define I2C_ADDR2 0x20
#define LCD_COLUMNS2 16
#define LCD_LINES2 2
LiquidCrystal_I2C lcd1(I2C_ADDR1, LCD_COLUMNS1, LCD_LINES1);
LiquidCrystal_I2C lcd2(I2C_ADDR2, LCD_COLUMNS2, LCD_LINES2);
#define DHTPIN 14
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
#define LDR_PIN 15
const float GAMMA = 0.7;
const float RL10 = 33;
byte customChar[] = {
B00000,
B01010,
B10101,
B01010,
B00100,
B00000,
B00000,
B00000
};
void setup() {
Serial.begin(9600);
lcd1.init();
lcd1.backlight();
lcd2.init();
lcd2.backlight();
dht.begin();
pinMode(LDR_PIN, INPUT);
lcd1.createChar(0, customChar);
// lcd1.createChar(1, customChar1);
// lcd1.createChar(2, customChar2);
// lcd1.createChar(3, customChar3);
}
void loop() {
// Đọc giá trị từ LDR
int analogValue = analogRead(LDR_PIN);
float voltage = analogValue / 4096. * 3.3;
float resistance = 2000 * voltage / (1 - voltage / 3.3);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
lcd1.clear();
lcd1.setCursor(0, 0);
if (lux < 100) {
lcd1.print("moi truong rat toi");
} else if (lux >= 100 && lux < 500) {
lcd1.print("anh sang yeu");
} else if (lux >= 500 && lux < 1000) {
lcd1.print("anh sang nhe");
} else if (lux >= 1000 && lux < 5000) {
lcd1.print("anh sang moi truong tu nhien");
} else if (lux >= 5000 && lux < 50000) {
lcd1.print("anh sang manh");
} else {
lcd1.print("rat sang");
}
lcd1.setCursor(0, 1);
lcd1.write(0);
Serial.print("Lux: ");
Serial.println(lux);
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
lcd2.setCursor(0, 0);
lcd2.print("Temp: ");
lcd2.print(temperature);
lcd2.print("C");
lcd2.setCursor(0, 1);
lcd2.print("Humidity: ");
lcd2.print(humidity);
lcd2.print("%");
delay(500);
}