#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <TouchScreen.h>
#define TFT_CS 10
#define TFT_DC 9
#define TFT_RST 8
#define TS_CS A5
#define TS_IRQ 2
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
TouchScreen ts = TouchScreen(SP, YL, XH, YL, 300);
void setup() {
Serial.begin(9600);
tft.begin();
tft.setRotation(0);
}
void loop() {
// Display a green background
tft.fillScreen(ILI9341_GREEN); // 16-bit color for green
// Display a red square (40x40 pixels) in the bottom right corner
int squareSize = 40;
int squareColor = ILI9341_RED; // 16-bit color for red
int xStart = tft.width() - squareSize;
int yStart = tft.height() - squareSize;
tft.fillRect(xStart, yStart, squareSize, squareSize, squareColor);
// Check for touch input
TSPoint touch = ts.getPoint();
// Check if the touch is within the red square
if (touch.z > 0 && touch.x > xStart && touch.x < (xStart + squareSize) && touch.y > yStart && touch.y < (yStart + squareSize)) {
// Change the background to blue
tft.fillScreen(ILI9341_BLUE); // 16-bit color for blue
delay(1000); // Add a delay for better touch response
}
// Clear the screen for the next iteration
tft.fillScreen(ILI9341_BLACK); // 16-bit color for black
}
Loading
ili9341-cap-touch
ili9341-cap-touch