#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <Adafruit_FT6206.h>
// TFT and touch screen pins
#define TFT_CS 15
#define TFT_RST 4
#define TFT_DC 2
#define TFT_MOSI 23
#define TFT_SCK 18
#define TFT_MISO 19
#define TOUCH_SDA 21
#define TOUCH_SCL 22
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
Adafruit_FT6206 ts = Adafruit_FT6206();
// Function to draw the touch button
void drawButton() {
tft.fillRect(60, 200, 200, 50, ILI9341_BLUE);
tft.setCursor(120, 220);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.print("Next Joke");
Serial.println("Button drawn");
}
// Setup function
void setup() {
Serial.begin(115200);
// Initialize the TFT display
tft.begin();
tft.setRotation(1);
// Initialize the touch screen
if (!ts.begin(40)) { // Pass in '40' for FT6206 capacitive touch with a higher threshold
Serial.println("Unable to start the touchscreen.");
while (1);
}
tft.fillScreen(ILI9341_BLACK);
// Draw the initial button
drawButton();
}
// Main loop function
void loop() {
if (ts.touched()) {
TS_Point p = ts.getPoint();
Serial.print("Touch detected at: ");
Serial.print(p.x);
Serial.print(", ");
Serial.println(p.y);
// Check if the touch is within the button area
if (p.x > 60 && p.x < 260 && p.y > 200 && p.y < 250) {
Serial.println("Button pressed!");
tft.fillRect(60, 200, 200, 50, ILI9341_RED); // Change button color to indicate press
delay(500); // Wait for 500 ms
drawButton(); // Redraw button
}
}
delay(100);
}