#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include "Fonts/FreeSans9pt7b.h" // Thêm font tùy chỉnh từ thư viện Adafruit
const float BETA = 3950; // should match the Beta Coefficient of the thermistor
//int analogValue = analogRead(A0);
//float celsius = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
#define TFT_DC 9
#define TFT_CS 10
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
void setup() {
tft.begin();
tft.setFont(&FreeSans9pt7b); // Sử dụng font tùy chỉnh có hỗ trợ ký tự đặc biệt
tft.setCursor(0, 100);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(2);
tft.println("LIVING ROOM");
tft.setCursor(60, 140);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(1.5);
tft.println("Temperature");
}
void loop() {
int analogValue = analogRead(A0); // Đọc giá trị analog từ chân A0
float celsius = 1 / (log(1 / (1023.0 / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15; // Tính nhiệt độ Celsius từ giá trị analog
// Xóa văn bản nhiệt độ cũ trước khi hiển thị giá trị mới
tft.fillRect(0, 160, 400, 180, ILI9341_BLACK); // Xóa vùng hiển thị nhiệt độ (tọa độ và kích thước có thể chỉnh sửa)
tft.setCursor(60, 200);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2.5);
tft.print(celsius,1); // In nhiệt độ với 1 chữ số thập phân
//tft.println(" ");
//tft.write(176); // In ký tự "°" bằng mã ASCII
// Vẽ ký hiệu "°" bằng drawCircle
int x = tft.getCursorX() + 7; // Lấy vị trí hiện tại và thêm khoảng cách nhỏ
int y = tft.getCursorY() - 20; // Đặt vị trí của ký hiệu "°" hơi cao lên so với nhiệt độ
tft.drawCircle(x, y, 2, ILI9341_WHITE); // Vẽ một vòng tròn nhỏ (ký hiệu "°")
tft.println(" C");
delay(500); // Chờ 1 giây trước khi cập nhật lại giá trị nhiệt độ
}