#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
#define POT_PIN 34  // Analog pin for the potentiometer

int sliderValue = LED_MIN_BRIGHTNESS;  // Current slider value
int previousSliderX = 0;  // Previous slider X position

#define ENCODER_CLK 13
#define ENCODER_DT 12
#define ENCODER_SW 14

uint8_t brightness = 1;
uint8_t volume = 0;
uint8_t fan = 0;
uint8_t br_temp, v_temp, f_temp;

typedef enum {
  SET_BRIGHTNESS,
  SET_VOLUME,
  SET_FAN,
} Mode;
int a;

Mode mode = SET_BRIGHTNESS;

void nextMode() {
  switch (mode) {
    case SET_BRIGHTNESS:
      mode = SET_VOLUME;
      break;

    case SET_VOLUME:
      mode = SET_FAN;
      break;

    case SET_FAN:
      mode = SET_BRIGHTNESS;
      break;
  }
}

void updateValue(int delta) {
  switch (mode) {
    case SET_BRIGHTNESS:
      brightness = constrain(brightness + delta, 1, 5);
       br_temp = map(brightness, 0, 5, 0 ,255 );
        Serial.print("Brightness: ");
        Serial.print(brightness);
        Serial.print("   ");
        Serial.println(br_temp);        
        updateSliderValue(br_temp);
      break;

    case SET_VOLUME:
      volume = constrain(volume + delta, 0, 4);
       v_temp = map(volume, 0, 4, 0 ,255 );
        Serial.print("Volume: ");
        Serial.print(volume);
        Serial.print("   ");
        Serial.println(v_temp);
        updateSliderValue(v_temp);  
      break;

    case SET_FAN:
      fan = constrain(fan + delta, 0, 4);
      f_temp = map(fan, 0, 4, 0 ,255 );
        Serial.print("Fan: ");
       Serial.print(fan);
        Serial.print("   ");
        Serial.println(f_temp);
        updateSliderValue(f_temp);  

      break;
  }
}

void setup(void) {
  Serial.begin(115200);
  Serial.println(F("Cap Touch Paint!"));
  tft.begin();
  // tft.setRotation(3);
  pinMode(ENCODER_CLK, INPUT);
  pinMode(ENCODER_DT, INPUT);
  pinMode(ENCODER_SW, INPUT_PULLUP);
  updateValue(a);
  
  
  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;
  }
}

long int modeLastChanged = 0;
int prevClk = HIGH;

void loop() {

  Rotary();

  // 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);
      // Serial.println(newSliderValue);
    }
  }

}

void Rotary(){
   if (digitalRead(ENCODER_SW) == LOW && millis() - modeLastChanged > 300) {
    modeLastChanged = millis();
    nextMode();
      // updateSerial();
    updateValue(a);
  }

  int clk = digitalRead(ENCODER_CLK);
  if (clk != prevClk && clk == LOW) {
    int dt = digitalRead(ENCODER_DT);
    int delta1 = dt == HIGH ? 1 : -1;
    updateValue(delta1);
    // updateSerial(); 
  }
  prevClk = clk;
}
Loading
ili9341-cap-touch