#include <Adafruit_GFX.h> // Core graphics library
#include <SPI.h> // This is needed for display
#include <Adafruit_ILI9341.h>
#include <Wire.h> // This is needed for FT6206
#include <Adafruit_FT6206.h>
Adafruit_FT6206 ctp = Adafruit_FT6206();
#define TFT_CS 15
#define TFT_DC 2
#define TFT_RST -1 // Not connected
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
#define SLIDER_X 20
#define SLIDER_Y 60
#define SLIDER_WIDTH 200
#define SLIDER_HEIGHT 10
#define SLIDER_COLOR ILI9341_YELLOW
#define SLIDER_HANDLER_COLOR ILI9341_ORANGE
#define SLIDER_HANDLER_WIDTH (SLIDER_WIDTH / 10)
#define LED_PIN 5 // PWM pin for controlling the LED brightness
#define LED_MIN_BRIGHTNESS 0
#define LED_MAX_BRIGHTNESS 255
#define LED_BRIGHTNESS_STEP 1
int sliderValue = LED_MIN_BRIGHTNESS; // Current slider value
int previousSliderX = 0; // Previous slider X position
#define GRAY_R 128
#define GRAY_G 128
#define GRAY_B 128
#define GRAY_COLOR (tft.color565(GRAY_R, GRAY_G, GRAY_B))
void setup(void) {
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");
tft.fillScreen(ILI9341_BLACK);
drawSlider();
pinMode(LED_PIN, OUTPUT);
drawSliderHandle(map(sliderValue, LED_MIN_BRIGHTNESS, LED_MAX_BRIGHTNESS, SLIDER_X, SLIDER_X + SLIDER_WIDTH - SLIDER_HANDLER_WIDTH));
}
void drawSlider() {
tft.fillRect(SLIDER_X, SLIDER_Y, SLIDER_WIDTH, SLIDER_HEIGHT, SLIDER_COLOR);
}
void drawSliderHandle(int x) {
int handlerVisibility = 70; // Visibility percentage of the handler
int handlerWidth = SLIDER_HANDLER_WIDTH * handlerVisibility / 100;
int handlerX = x + (SLIDER_HANDLER_WIDTH - handlerWidth) / 2;
tft.fillRect(SLIDER_X, SLIDER_Y, SLIDER_WIDTH, SLIDER_HEIGHT, SLIDER_COLOR); // Clear slider background
tft.fillRect(handlerX, SLIDER_Y, handlerWidth, SLIDER_HEIGHT, SLIDER_HANDLER_COLOR);
}
void updateSliderValue(int newSliderValue) {
if (newSliderValue != sliderValue) {
analogWrite(LED_PIN, newSliderValue);
drawSliderHandle(map(newSliderValue, LED_MIN_BRIGHTNESS, LED_MAX_BRIGHTNESS, SLIDER_X, SLIDER_X + SLIDER_WIDTH - SLIDER_HANDLER_WIDTH));
sliderValue = newSliderValue;
}
}
void loop() {
// Wait for a touch
if (!ctp.touched()) {
delay(10); // Speeds up the simulation
return;
}
TS_Point p = ctp.getPoint();
p.x = map(p.x, 0, 240, 240, 0);
p.y = map(p.y, 0, 320, 320, 0);
Serial.print("("); Serial.print(p.x);
Serial.print(", "); Serial.print(p.y);
Serial.println(")");
if (p.y >= SLIDER_Y && p.y <= SLIDER_Y + SLIDER_HEIGHT) {
int newSliderX = constrain(p.x - SLIDER_HANDLER_WIDTH / 2, SLIDER_X, SLIDER_X + SLIDER_WIDTH - SLIDER_HANDLER_WIDTH);
if (newSliderX != previousSliderX) {
drawSliderHandle(newSliderX);
previousSliderX = newSliderX;
int newSliderValue = map(newSliderX, SLIDER_X, SLIDER_X + SLIDER_WIDTH - SLIDER_HANDLER_WIDTH, LED_MIN_BRIGHTNESS, LED_MAX_BRIGHTNESS);
updateSliderValue(newSliderValue);
}
}
}
Loading
ili9341-cap-touch
ili9341-cap-touch