#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <dht.h>
dht DHT;
#define DHT22_PIN 5
#define TFT_CS 10
#define TFT_RST 9
#define TFT_DC 8
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
#define GRAPH_WIDTH 20
#define GRAPH_HEIGHT 100
#define GRAPH_X 60
#define GRAPH_Y 80 // Geser ke bawah lagi
#define GRAPH_COLOR_TEMP ILI9341_RED
#define GRAPH_COLOR_HUM ILI9341_BLUE
#define MAX_VALUE 100
#define MIN_VALUE 0
#define LED_GRAPH_X 50
#define LED_GRAPH_Y 220
#define LED_GRAPH_WIDTH 200
#define LED_GRAPH_HEIGHT 20
#define LED_BAR_WIDTH (LED_GRAPH_WIDTH / 10)
#define LED_BAR_SPACING 2
#define BATTERY_LEVEL 75 // contoh level baterai
void setup() {
Serial.begin(115200);
tft.begin();
tft.setRotation(3); // Sesuaikan dengan orientasi layar Anda
tft.fillScreen(ILI9341_PINK); // Background diubah menjadi pink
// Geser teks judul mendekati gambar grafik thermometer
tft.setCursor(20, 10);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.println("SUHU DAN KELEMBABAN");
delay(2000);
}
void drawThermometer(int value, int maxValue, int color, int xOffset, String title) {
// Draw title
tft.setCursor(GRAPH_X + xOffset - 15, GRAPH_Y - 30);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(1);
tft.print(title);
// Draw outer frame of thermometer
tft.drawRect(GRAPH_X + xOffset - 5, GRAPH_Y + 30, 10, GRAPH_HEIGHT, ILI9341_WHITE);
// Draw inner frame of thermometer
tft.fillRect(GRAPH_X + xOffset - 4, GRAPH_Y + GRAPH_HEIGHT - GRAPH_HEIGHT + 20, 8, GRAPH_HEIGHT - 40, ILI9341_BLACK);
// Draw scale lines
for (int i = 0; i <= maxValue; i += 10) {
int y = map(i, 0, maxValue, GRAPH_Y + GRAPH_HEIGHT, GRAPH_Y + GRAPH_HEIGHT - GRAPH_HEIGHT + 20);
tft.drawFastHLine(GRAPH_X + xOffset - 10, y, 5, ILI9341_WHITE);
}
// Draw value line
int lineY = map(value, MIN_VALUE, maxValue, GRAPH_Y + GRAPH_HEIGHT, GRAPH_Y + GRAPH_HEIGHT - GRAPH_HEIGHT + 20);
tft.drawFastHLine(GRAPH_X + xOffset - 10, lineY, 5, color);
// Draw value text
tft.setCursor(GRAPH_X + xOffset + 10, lineY - 5);
tft.setTextColor(color);
tft.setTextSize(1);
tft.print(value);
}
void drawBatteryLevel(int level) {
// Calculate width for each bar
int barWidth = LED_BAR_WIDTH - LED_BAR_SPACING;
// Draw each bar
for (int i = 0; i < 10; i++) {
int barX = LED_GRAPH_X + (LED_BAR_WIDTH + LED_BAR_SPACING) * i;
int barHeight = LED_GRAPH_HEIGHT;
int barColor = (i * 10 <= level) ? ILI9341_YELLOW : ILI9341_BLACK;
tft.fillRect(barX, LED_GRAPH_Y, barWidth, barHeight, barColor);
}
}
void loop() {
// READ DATA
int chk = DHT.read22(DHT22_PIN);
// DISPLAY DATA ON LCD
// Draw thermometer for temperature and humidity
drawThermometer(DHT.temperature, MAX_VALUE, GRAPH_COLOR_TEMP, 0, "Suhu");
drawThermometer(DHT.humidity, MAX_VALUE, GRAPH_COLOR_HUM, 100, "Kelembaban");
// Draw battery level
drawBatteryLevel(BATTERY_LEVEL);
delay(2000);
}