#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <Adafruit_STMPE610.h>
// Define ILI9341 pins
#define TFT_CS 10
#define TFT_RST 9
#define TFT_DC 8
#define TFT_MOSI 11
#define TFT_CLK 13
#define TFT_LED 7
#define TFT_MISO 12
// Define STMPE610 pins
#define STMPE_CS 6
// Define LED pin
#define LED_PIN 1
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
Adafruit_STMPE610 touch = Adafruit_STMPE610(STMPE_CS);
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Initialize ILI9341
tft.begin();
tft.setRotation(3);
tft.fillScreen(ILI9341_BLACK);
// Initialize STMPE610 touch screen
if (!touch.begin()) {
Serial.println("STMPE not found!");
while (1);
}
// Set LED pin as output
pinMode(LED_PIN, OUTPUT);
// Draw buttons
drawButtons();
}
void loop() {
// Check for touch
if (touch.touched()) {
uint16_t x, y;
uint8_t z;
if (touch.bufferEmpty()) {
return;
}
touch.readData(&x, &y, &z);
// Map touch coordinates to screen coordinates
x = map(x, 0, 3800, 0, 320);
y = map(y, 0, 3800, 0, 240);
// Check if touch is within the ON button
if (x >= 60 && x <= 140 && y >= 100 && y <= 160) {
digitalWrite(LED_PIN, HIGH); // Turn on LED
}
// Check if touch is within the OFF button
if (x >= 180 && x <= 260 && y >= 100 && y <= 160) {
digitalWrite(LED_PIN, LOW); // Turn off LED
}
}
}
void drawButtons() {
// Draw ON button
tft.fillRect(60, 100, 80, 60, ILI9341_BLUE);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.setCursor(70, 120);
tft.print("ON");
// Draw OFF button
tft.fillRect(180, 100, 80, 60, ILI9341_RED);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.setCursor(190, 120);
tft.print("OFF");
}