/**
ESP32s3 adaptation by Tyeth Gundry of Arduino version of
First demo for FT6206 Capactive Touch Screen on Wokwi. Enjoy!
https://wokwi.com/arduino/projects/311598148845830720
*/
/***************************************************
This is our touchscreen painting example for the Adafruit ILI9341
captouch shield
----> http://www.adafruit.com/products/1947
Check out the links above for our tutorials and wiring diagrams
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
MIT license, all text above must be included in any redistribution
****************************************************/
#include <Adafruit_GFX.h>
#include <SPI.h>
#include <Adafruit_ILI9341.h>
#include <Arduino.h>
#include <Adafruit_FT6206.h>
// Initialize the display and touchscreen
#define TFT_DC 2
#define TFT_CS 15
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
Adafruit_FT6206 ctp = Adafruit_FT6206();
// Button dimensions and layout
#define BUTTON_WIDTH 100
#define BUTTON_HEIGHT 50
#define BUTTON_SPACING 20
#define BUTTON_Y 120
// Button positions
#define BUTTON1_X 30
#define BUTTON2_X (BUTTON1_X + BUTTON_WIDTH + BUTTON_SPACING)
#define BUTTON3_X (BUTTON2_X + BUTTON_WIDTH + BUTTON_SPACING)
void setup(void) {
Serial.begin(115200);
Serial.println("Capacitive Touch GUI Demo!");
Wire.setPins(10, 8); // Set I2C pins if needed
tft.begin();
tft.setRotation(1); // Landscape mode
if (!ctp.begin(40)) {
Serial.println("Couldn't start FT6206 touchscreen controller");
while (1);
}
// Draw the buttons
drawButtons();
}
void loop() {
if (!ctp.touched()) return; // If no touch detected, exit the loop
TS_Point p = ctp.getPoint();
p.x = map(p.x, 0, 320, 320, 0);
p.y = map(p.y, 0, 240, 240, 0);
// Check which button was pressed based on touch coordinates
if (p.y > BUTTON_Y && p.y < (BUTTON_Y + BUTTON_HEIGHT)) {
if (p.x > BUTTON1_X && p.x < (BUTTON1_X + BUTTON_WIDTH)) {
handleButtonPress("Small", ILI9341_GREEN);
}
else if (p.x > BUTTON2_X && p.x < (BUTTON2_X + BUTTON_WIDTH)) {
handleButtonPress("Medium", ILI9341_YELLOW);
}
else if (p.x > BUTTON3_X && p.x < (BUTTON3_X + BUTTON_WIDTH)) {
handleButtonPress("Large", ILI9341_RED);
}
}
}
// Function to draw buttons
void drawButtons() {
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.setCursor(30, 50);
tft.print("Choose a Size:");
tft.fillRect(BUTTON1_X, BUTTON_Y, BUTTON_WIDTH, BUTTON_HEIGHT, ILI9341_GREEN);
tft.setCursor(BUTTON1_X + 10, BUTTON_Y + 20);
tft.setTextColor(ILI9341_BLACK);
tft.print("Small");
tft.fillRect(BUTTON2_X, BUTTON_Y, BUTTON_WIDTH, BUTTON_HEIGHT, ILI9341_YELLOW);
tft.setCursor(BUTTON2_X + 10, BUTTON_Y + 20);
tft.setTextColor(ILI9341_BLACK);
tft.print("Medium");
tft.fillRect(BUTTON3_X, BUTTON_Y, BUTTON_WIDTH, BUTTON_HEIGHT, ILI9341_RED);
tft.setCursor(BUTTON3_X + 10, BUTTON_Y + 20);
tft.setTextColor(ILI9341_BLACK);
tft.print("Large");
}
// Function to handle button press action
void handleButtonPress(const char* buttonText, uint16_t color) {
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(60, 120);
tft.setTextColor(color);
tft.setTextSize(3);
tft.print("You chose ");
tft.print(buttonText);
delay(2000); // Show message for 2 seconds
drawButtons(); // Redraw the button layout
}
Loading
esp32-s3-devkitc-1
esp32-s3-devkitc-1