// ALFIAN CENTER
// SUBSCRIBE
// =-=-=-=-=-=-=-=--=---==-=-=--=-=-==---=-=-=--=---
#include <SPI.h> // Library untuk komunikasi SPI
#include <Wire.h> // Library untuk komunikasi I2C
#include <Adafruit_GFX.h> // Library untuk grafik LCD
#include <Adafruit_ILI9341.h> // Library untuk layar LCD ILI9341
#include <Adafruit_FT6206.h> // Library untuk touchscreen FT6206
// Definisi pin untuk LCD
const int TFT_CS = 15;
const int TFT_DC = 2;
const int TFT_RST = 4;
const int LED_PIN = 12; // Pin untuk LED
// Koordinat tombol pada layar
const int BTN_GRN_X = 30;
const int BTN_GRN_Y = 30;
const int BTN_RED_X = 180;
const int BTN_RED_Y = 30;
bool isLEDOn = false; // Variabel untuk menyimpan status LED
// Inisialisasi objek LCD dan touchscreen
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
Adafruit_FT6206 ts = Adafruit_FT6206();
void setup() {
Serial.begin(9600); // Memulai komunikasi serial
tft.begin(); // Inisialisasi LCD
tft.setRotation(1); // Menyesuaikan orientasi layar
ts.begin(); // Inisialisasi touchscreen
pinMode(LED_PIN, OUTPUT); // Menentukan LED sebagai output
// Menggambar tombol ON dan OFF pada layar
drawButton(BTN_GRN_X, BTN_GRN_Y, "ON ", 0x07E0);
drawButton(BTN_RED_X, BTN_RED_Y, "OFF", 0xF800);
// Menampilkan teks awal "LED OFF"
tft.setTextSize(3);
tft.setTextColor(ILI9341_BLUE);
tft.setCursor(100, 165);
tft.print("LED OFF");
Serial.println("Ready"); // Debugging di Serial Monitor
}
// ALFIAN CENTER
// SUBSCRIBE
// =-=-=-=-=-=-=-=--=---==-=-=--=-=-==---=-=-=--=---
void loop() {
TS_Point p = getTouchPoint(); // Membaca titik sentuh
if (p.z > 0) { // Jika ada sentuhan pada layar
p.x = map(p.x, 0, 240, 240, 0); // Menyesuaikan koordinat X
displayTouchCoordinates(p); // Menampilkan koordinat sentuhan untuk debugging
// Jika tombol ON ditekan
if (isButtonPressed(p, BTN_GRN_X, BTN_GRN_Y)) {
handleButtonPress(BTN_GRN_X, BTN_GRN_Y, "LED ON ", HIGH, 0x07E0);
Serial.println("Green"); // Debugging
}
// Jika tombol OFF ditekan
if (isButtonPressed(p, BTN_RED_X, BTN_RED_Y)) {
handleButtonPress(BTN_RED_X, BTN_RED_Y, "LED OFF", LOW, 0xF800);
Serial.println("Red"); // Debugging
}
}
}
// Fungsi untuk mendapatkan koordinat sentuhan touchscreen
TS_Point getTouchPoint() {
TS_Point p;
if (ts.touched()) {
p = ts.getPoint(); // Membaca titik sentuhan
} else {
p = TS_Point(0, 0, 0); // Jika tidak ada sentuhan
}
return p;
}
// Fungsi untuk menampilkan koordinat sentuhan di layar
void displayTouchCoordinates(TS_Point p) {
tft.setTextSize(2);
tft.setTextColor(ILI9341_RED, ILI9341_BLACK);
tft.setCursor(0, 155);
tft.print("X: ");
tft.print(p.x);
tft.print(" ");
tft.setCursor(0, 185);
tft.print("Y: ");
tft.print(p.y);
tft.print(" ");
tft.setCursor(0, 215);
tft.print("Z: ");
tft.print(p.z);
tft.print(" ");
Serial.print("("); Serial.print(p.x);
Serial.print(", "); Serial.print(p.y);
Serial.println(")");
}
// Fungsi untuk mengecek apakah tombol ditekan
bool isButtonPressed(TS_Point p, int btnY, int btnX) {
return (p.x > btnX && p.x < (btnX + 95) && p.y > btnY && p.y < (btnY + 95));
}
// Fungsi untuk menangani aksi saat tombol ditekan
void handleButtonPress(int btnX, int btnY, const char* message, int ledState, uint16_t buttonColor) {
tft.setTextSize(3);
tft.setTextColor(ILI9341_BLUE, ILI9341_BLACK);
tft.setCursor(100, 165);
tft.print(message); // Menampilkan status LED di layar
digitalWrite(LED_PIN, ledState); // Mengubah status LED
// Efek tombol ditekan (opsional)
tft.fillRoundRect(btnX, btnY, 95, 95, 10, 0x18E3); // Warna efek ditekan
delay(100);
drawButton(btnX, btnY, message, buttonColor); // Menggambar ulang tombol
}
// Fungsi untuk menggambar tombol pada layar
void drawButton(int xp, int yp, const char* label, uint16_t color) {
tft.fillRoundRect(xp, yp, 95, 95, 10, 0x18E3); // Menggambar latar belakang tombol
tft.fillRoundRect(xp + 2, yp + 2, 95, 95, 10, color); // Menggambar tombol
tft.setCursor(xp + 8, yp + 40);
tft.setTextSize(2);
tft.setTextColor(ILI9341_WHITE);
tft.println(label); // Menampilkan teks tombol
}
// ALFIAN CENTER
// SUBSCRIBE
// =-=-=-=-=-=-=-=--=---==-=-=--=-=-==---=-=-=--=---