#include <Arduino_GFX_Library.h>
#include <Adafruit_FT6206.h>
#define TFT_DC 2
#define TFT_CS 15
#define TFT_SCK 18
#define TFT_MOSI 23
#define TFT_MISO 19
#define TFT_RESET 4
#define LED 12
Adafruit_FT6206 ctp;
void setup(void) {
Serial.begin(115200);
Arduino_ESP32SPI bus = Arduino_ESP32SPI(TFT_DC, TFT_CS, TFT_SCK, TFT_MOSI, TFT_MISO);
Arduino_ILI9341 display = Arduino_ILI9341(&bus, TFT_RESET);
display.begin();
display.fillScreen(LIGHTGREY);
display.drawRect(10, 10, 70, 75, PURPLE);
display.fillRect(10, 10, 70, 75, PURPLE);
display.setCursor(30, 44);
display.setTextSize(2);
display.setTextColor(WHITE);
display.print("LED");
display.fillRect(110, 10, 55, 75, GREEN);
display.fillRect(165, 10, 55, 75, RED);
display.setCursor(132, 44);
display.println("ON");
display.setCursor(187, 44);
display.println("OFF");
display.drawRect(10, 120, 70, 75, PURPLE);
display.fillRect(10, 120, 70, 75, PURPLE);
display.drawRect(10, 220, 70, 75, PURPLE);
display.fillRect(10, 220, 70, 75, PURPLE);
if (!ctp.begin(40)) { // pass in 'sensitivity' coefficient
Serial.println("Couldn't start FT6206 touchscreen controller");
while (1);
}
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);
}
void loop() {
if (!ctp.touched()) {
delay(10);
return;
}
TS_Point p = ctp.getPoint();
p.x = map(p.x, 0, 240, 240, 0);
p.y = map(p.y, 0, 320, 320, 0);
if (ctp.touched()) {
Serial.print("Touch detected at X = ");
Serial.print(p.x);
Serial.print(", Y = ");
Serial.println(p.y);
if (p.x >110 && p.x < 165 && p.y > 10 && p.y < 85) {
Serial.println("Turning LED ON");
digitalWrite(LED, HIGH); // Turn LED ON
} else if (p.x > 165 && p.x < 230 && p.y > 10 && p.y < 85) {
Serial.println("Turning LED OFF");
digitalWrite(LED, LOW); // Turn LED OFF
}
}
}