#include <TFT_eSPI.h>
#include <SPI.h>
#include <Wire.h>
#include <TouchScreen.h>
#include <Adafruit_FT6206.h>
Adafruit_FT6206 ctp = Adafruit_FT6206();
#define LCD_CS 15
#define LCD_DC 2
#define LCD_SCK 18
#define LCD_MOSI 23
#define LCD_RESET 4
#define LCD_MISO 19
#define LCD_SDA 21
#define LCD_SCL 22
#define A5 12
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define YP 18
#define XM 21
#define YM 7
#define XP 6
#define TS_MINX 940
#define TS_MINY 160
#define TS_MAXX 160
#define TS_MAXY 970
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
TFT_eSPI tft = TFT_eSPI();
#define MINPRESSURE 5
#define MAXPRESSURE 1000
bool ledState = false;
void setup() {
pinMode(A5, OUTPUT);
digitalWrite(A5, LOW);
Serial.begin(9600);
tft.begin(0x9325);
tft.setTextColor(WHITE);
tft.setTextSize(3);
tft.fillScreen(BLACK);
tft.fillRect(0, 0, 120, 120, GREEN);
tft.fillRect(120, 0, 120, 120, RED);
tft.setCursor(8, 45);
tft.println("LED ON");
tft.setCursor(128, 45);
tft.println("LEDOFF");
Serial.begin(115200);
Serial.println(F("Cap Touch Paint!"));
tft.begin();
if (! ctp.begin(40)) { // pass in 'sensitivity' coefficient
Serial.println("Couldn't start FT6206 touchscreen controller");
while (1);
}
Serial.println("Capacitive touchscreen started");
}
// void loop() {
// TSPoint p = ts.getPoint();
// if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
// int16_t screen_x = map(p.x, TS_MINX, TS_MAXX, tft.width(), 0);
// int16_t screen_y = map(p.y, TS_MINY, TS_MAXY, tft.height(), 0);
// Serial.print("Touch detected at (");
// Serial.print(screen_x);
// Serial.print(", ");
// Serial.print(screen_y);
// Serial.println(")");
// if (screen_x > 0 && screen_x < 120 && screen_y > 0 && screen_y < 120) {
// Serial.println("Button 1 pressed");
// ledState = !ledState;
// digitalWrite(A5, ledState ? HIGH : LOW);
// delay(200);
// }
// if (screen_x > 120 && screen_x < 240 && screen_y > 0 && screen_y < 120) {
// Serial.println("Button 2 pressed");
// ledState = !ledState;
// digitalWrite(A5, ledState ? HIGH : LOW);
// delay(200);
// }
// }
// }
void loop() {
if (! ctp.touched()) {
delay(10); // Speeds up the simulation
return;
}
// Retrieve a point
TS_Point p = ctp.getPoint();
// Print out raw data from screen touch controller
Serial.print("X = "); Serial.print(p.x);
Serial.print("\tY = "); Serial.print(p.y);
Serial.print(" -> ");
// flip it around to match the screen.
p.x = map(p.x, 0, 240, 240, 0);
p.y = map(p.y, 0, 320, 320, 0);
// Print out the remapped (rotated) coordinates
Serial.print("("); Serial.print(p.x);
Serial.print(", "); Serial.print(p.y);
Serial.println(")");
// See if there's any touch data for us
if (ctp.touched())
{
// Retrieve a point
TS_Point p = ctp.getPoint();
// rotate coordinate system
// flip it around to match the screen.
p.x = map(p.x, 0, 240, 240, 0);
p.y = map(p.y, 0, 320, 320, 0);
int y = tft.height() - p.x;
int x = p.y;
}
// TSPoint p = ts.getPoint();
if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
// Map touchscreen coordinates to screen coordinates
int16_t screen_x = map(p.x, TS_MINX, TS_MAXX, tft.width(), 0);
int16_t screen_y = map(p.y, TS_MINY, TS_MAXY, tft.height(), 0);
Serial.print("Touch detected at (");
Serial.print(screen_x);
Serial.print(", ");
Serial.print(screen_y);
Serial.println(")");
// Check if touch falls within button regions
if (screen_x > 0 && screen_x < 120 && screen_y > 0 && screen_y < 120) {
ledState = !ledState; // Toggle LED state
digitalWrite(A5, ledState ? HIGH : LOW); // Turn LED on/off based on state
delay(200); // Debounce delay
}
if (screen_x > 120 && screen_x < 240 && screen_y > 0 && screen_y < 120) {
ledState = !ledState; // Toggle LED state
digitalWrite(A5, ledState ? HIGH : LOW); // Turn LED on/off based on state
delay(200); // Debounce delay
}
}
}