#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <MAX6675.h>
// ==== Pin cấu hình ====
#define TFT_CS 2
#define TFT_DC 3
#define TFT_RST 10
#define MOSFET_PIN 25
#define BUZZER_PIN 26
#define SLEEP_SENSOR_PIN 27
#define THERMO_CLK 18
#define THERMO_CS 5
#define THERMO_DO 19
// ==== Biến hệ thống ====
float currentTemp = 0;
float setTemp = 350;
float standbyTemp = 150;
float quickTemps[3] = {300, 350, 400};
float hysteresis = 10; // chênh lệch tắt/mở
unsigned long lastMovement = 0;
unsigned long standbyDelay = 60 * 1000; // 1 phút
unsigned long shutdownDelay = 5 * 60 * 1000; // 5 phút
bool ironInUse = true;
bool heating = true;
// ==== Khởi tạo đối tượng ====
MAX6675 thermocouple(THERMO_CLK, THERMO_CS, THERMO_DO);
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
// ==== Giao diện ====
void drawUI() {
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(10, 10);
tft.setTextColor(ILI9341_YELLOW);
tft.setTextSize(2);
tft.print("Nhiet do: ");
tft.print(currentTemp);
tft.print("C");
tft.setCursor(10, 40);
tft.print("Cai dat: ");
tft.print(setTemp);
tft.print("C");
// Nút preset
for (int i = 0; i < 3; i++) {
tft.fillRect(10 + i * 70, 80, 60, 30, ILI9341_BLUE);
tft.setCursor(20 + i * 70, 85);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.print(quickTemps[i]);
}
// Nút tăng giảm
tft.fillRect(10, 130, 60, 30, ILI9341_GREEN);
tft.setCursor(20, 135);
tft.print("+");
tft.fillRect(80, 130, 60, 30, ILI9341_RED);
tft.setCursor(100, 135);
tft.print("-");
}
// ==== Kiểm tra cảm biến mỏ hàn ====
void checkSleepSensor() {
bool touched = digitalRead(SLEEP_SENSOR_PIN) == HIGH;
if (touched) {
if (!ironInUse) {
tone(BUZZER_PIN, 2000, 100);
ironInUse = true;
setTemp = quickTemps[1]; // mặc định về preset 350
}
lastMovement = millis();
} else {
if (millis() - lastMovement > shutdownDelay) {
setTemp = 0;
} else if (millis() - lastMovement > standbyDelay) {
setTemp = standbyTemp;
}
ironInUse = false;
}
}
// ==== Điều khiển gia nhiệt ====
void controlHeating() {
if (currentTemp < setTemp - hysteresis) {
digitalWrite(MOSFET_PIN, HIGH);
heating = true;
} else if (currentTemp >= setTemp + hysteresis) {
digitalWrite(MOSFET_PIN, LOW);
heating = false;
}
}
// ==== Kiểm tra chạm để đổi nhiệt độ ====
void checkTouch() {
// Giả lập touch: bạn thay thế bằng thư viện touch hoặc nút nhấn tùy phần cứng
// Ở đây là ví dụ đơn giản dùng Serial hoặc nút gắn ngoài
}
// ==== Cập nhật màn hình ====
void updateUI() {
tft.fillRect(120, 10, 80, 20, ILI9341_BLACK);
tft.setCursor(120, 10);
tft.print(currentTemp);
tft.fillRect(120, 40, 80, 20, ILI9341_BLACK);
tft.setCursor(120, 40);
tft.print(setTemp);
}
// ==== Setup ====
void setup() {
Serial.begin(115200);
tft.begin();
tft.setRotation(1);
drawUI();
pinMode(MOSFET_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(SLEEP_SENSOR_PIN, INPUT);
digitalWrite(MOSFET_PIN, LOW);
lastMovement = millis();
}
// ==== Vòng lặp chính ====
void loop() {
currentTemp = thermocouple.readCelsius();
checkSleepSensor();
controlHeating();
checkTouch();
updateUI();
delay(200);
}