#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // Địa chỉ I2C của màn hình LCD và kích thước (16x2)
const int referenceResistor = 10000; // Giá trị của trở kháng tham chiếu (10K ohm)
const int buttonPin1 = 23; // Chân của nút 1
const int buttonPin2 = 19; // Chân của nút 2
const int buttonPin3 = 18; // Chân của nút 3
const int buttonPin4 = 5; // Chân của nút 4
bool mainScreen = true;
void setup() {
lcd.init(); // Khởi tạo màn hình LCD
lcd.backlight();
pinMode(buttonPin1, INPUT_PULLUP); // Thiết lập chân nút 1 là INPUT với điện trở kéo lên
pinMode(buttonPin2, INPUT_PULLUP); // Thiết lập chân nút 2 là INPUT với điện trở kéo lên
pinMode(buttonPin3, INPUT_PULLUP); // Thiết lập chân nút 3 là INPUT với điện trở kéo lên
pinMode(buttonPin4, INPUT_PULLUP); // Thiết lập chân nút 4 là INPUT với điện trở kéo lên
}
void loop() {
if (digitalRead(buttonPin1) == LOW) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Xin chao");
mainScreen = false;
}
else if (digitalRead(buttonPin2) == LOW) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Ngoc Vu, Van Ton");
lcd.setCursor(0, 1);
lcd.print("Huy Hoang");
mainScreen = false;
}
else if (digitalRead(buttonPin3) == LOW) {
lcd.clear();
mainScreen = false;
while (digitalRead(buttonPin1) == HIGH && digitalRead(buttonPin2) == HIGH && digitalRead(buttonPin4) == HIGH) {
lcd.setCursor(0, 0);
lcd.print("Temp: " + String(readTemperature()) + " C");
}
}
else if (digitalRead(buttonPin4) == LOW) {
mainScreen = true;
lcd.clear();
}
if (mainScreen) {
// Hiển thị màn hình chính chia làm 4 ô
lcd.setCursor(3, 0);
lcd.print("1");
lcd.setCursor(7, 0);
lcd.print("|");
lcd.setCursor(11, 0);
lcd.print("2");
lcd.setCursor(3, 1);
lcd.print("3");
lcd.setCursor(7, 1);
lcd.print("|");
lcd.setCursor(11, 1);
lcd.print("4");
}
readTemperature();
}
float readTemperature() {
int sensorValue = analogRead(34); // Đọc giá trị từ cảm biến NTC qua chân analog 34
float voltage = sensorValue * (5 / 4095.0); // Chuyển đổi giá trị đọc thành điện áp
float resistance = (5 - voltage) * referenceResistor / voltage; // Tính giá trị trở kháng của NTC
float temperature = 1.0 / (1.0 / 298.15 + 1.0 / 3435.0 * log(resistance / 10000.0)) - 273.15; // Tính nhiệt độ dựa trên giá trị trở kháng
return temperature;
}