#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <Adafruit_FT6206.h>
#include <DHT.h>
#define L1 2
#define L2 3
#define L3 4
#define TFT_CS 10
#define TFT_RST 9
#define TFT_DC 8
#define DHT_PIN 7
#define DHT_TYPE DHT22
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
Adafruit_FT6206 ts = Adafruit_FT6206();
DHT dht(DHT_PIN, DHT_TYPE);
int suhu = 25; // Nilai awal suhu
int kelembaban = 50; // Nilai awal kelembaban
void setup() {
Serial.begin(9600);
tft.begin();
ts.begin(40);
dht.begin();
tft.setRotation(3); // Sesuaikan rotasi layar jika diperlukan
tft.fillScreen(ILI9341_BLACK);
drawButtons();
updateTempHumidity();
pinMode(L1, OUTPUT);
pinMode(L2, OUTPUT);
pinMode(L3, OUTPUT);
}
void loop() {
if (ts.touched()) {
TS_Point p = ts.getPoint();
if (p.x > 10 && p.x < 110 && p.y > 60 && p.y < 160) {
// Tombol 1 ditekan
increaseSuhu();
} else if (p.x > 120 && p.x < 220 && p.y > 60 && p.y < 160) {
// Tombol 2 ditekan
increaseKelembaban();
} else if (p.x > 230 && p.x < 330 && p.y > 60 && p.y < 160) {
// Tombol 3 ditekan
decreaseKelembaban();
}
}
}
void drawButtons() {
tft.fillScreen(ILI9341_BLACK);
tft.fillRect(10, 60, 100, 100, ILI9341_BLUE); // Tombol 1
tft.fillRect(120, 60, 100, 100, ILI9341_GREEN); // Tombol 2
tft.fillRect(230, 60, 100, 100, ILI9341_RED); // Tombol 3
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(3);
tft.setCursor(40, 95);
tft.print("1");
tft.setCursor(150, 95);
tft.print("2");
tft.setCursor(260, 95);
tft.print("3");
}
void updateTempHumidity() {
float temp = dht.readTemperature();
float humidity = dht.readHumidity();
if (isnan(temp) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
tft.fillRect(0, 0, 320, 40, ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.setCursor(10, 10);
tft.print("Suhu: ");
tft.print(temp);
tft.print(" C");
tft.setCursor(160, 10);
tft.print("Kelembaban: ");
tft.print(humidity);
tft.print("%");
}
void increaseSuhu() {
static int pressCount = 0;
pressCount++;
if (pressCount == 1) {
digitalWrite(L1, 1);
suhu++;
} else if (pressCount == 2) {
//digitalWrite(L1, 0);
kelembaban++;
} else if (pressCount == 3) {
//digitalWrite(L1, 1);
tft.fillRect(120, 60, 100, 100, ILI9341_DARKGREY); // Menonaktifkan tombol 2
tft.fillRect(230, 60, 100, 100, ILI9341_DARKGREY); // Menonaktifkan tombol 3
}
if (pressCount > 3) {
pressCount = 0;
}
updateTempHumidity();
}
void increaseKelembaban() {
static int pressCount = 0;
pressCount++;
if (pressCount == 1) {
suhu--;
} else if (pressCount == 2) {
kelembaban--;
} else if (pressCount == 3) {
tft.fillRect(120, 60, 100, 100, ILI9341_DARKGREY); // Menonaktifkan tombol 2
tft.fillRect(230, 60, 100, 100, ILI9341_DARKGREY); // Menonaktifkan tombol 3
}
if (pressCount > 3) {
pressCount = 0;
}
updateTempHumidity();
}
void decreaseKelembaban() {
suhu = 25;
kelembaban = 50;
tft.fillRect(120, 60, 100, 100, ILI9341_GREEN); // Mengaktifkan tombol 2
tft.fillRect(230, 60, 100, 100, ILI9341_RED); // Mengaktifkan tombol 3
updateTempHumidity();
}