#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int LM35_PIN = A0;
const float THRESHOLD = 25.0; // Giá trị ngưỡng định mức
LiquidCrystal_I2C lcd(0x27, 16, 2);
float temperature = 0.0;
bool isAboveThreshold = false;
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.print("LM35 Test");
delay(1000);
}
void loop() {
// Task 1: Thu thập dữ liệu từ cảm biến LM35 và tính nhiệt độ
int sensorValue = analogRead(LM35_PIN);
float voltage = sensorValue * (5.0 / 1023.0); // Chuyển đổi giá trị analog thành điện áp
temperature = voltage * 100.0; // Chuyển đổi điện áp thành nhiệt độ
// Task 2: Hiển thị kết quả lên màn hình Serial Monitor
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println("C");
// Task 3: Quản lý dữ liệu input và toggle giá trị nếu vượt định mức
if (temperature > THRESHOLD) {
isAboveThreshold = !isAboveThreshold;
if (isAboveThreshold) {
// Thực hiện hành động khi nhiệt độ vượt ngưỡng định mức
// Ví dụ: toggle một thiết bị hoặc thực hiện một tác vụ khác
Serial.println("Threshold exceeded - Toggled!");
}
}
// Task 4: Hiển thị kết quả lên màn hình LCD
lcd.setCursor(0, 0);
lcd.print("T: ");
lcd.print(temperature);
lcd.print((char)223);
lcd.print("C");
delay(500);
}