#include <Adafruit_GFX.h>
#include <SPI.h>
#include <Adafruit_ILI9341.h>
#include <Wire.h>
#include <Adafruit_FT6206.h>
Adafruit_FT6206 ctp = Adafruit_FT6206();
#define TFT_CS 10
#define TFT_DC 9
#define TFT_RST -1 // Not connected
#define LED_PIN 13 // LED control pin
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
const int rectSize = 40;
const int rectSpacing = 30;
const int rectY = 150;
const int totalWidth = 3 * rectSize + 2 * rectSpacing;
const int startX = (tft.width() - totalWidth) / 2;
int rectColors[3] = {ILI9341_WHITE, ILI9341_WHITE, ILI9341_WHITE};
bool rectSelected[3] = {false, false, false};
int sliderX = startX;
int sliderY = rectY + rectSize + 20;
int sliderWidth = totalWidth;
int sliderHeight = 10;
int sliderMin = 0;
int sliderMax = 10;
int sliderValue = sliderMin;
int sliderPosition = map(sliderValue, sliderMin, sliderMax, sliderX, sliderX + sliderWidth);
int prevSliderValue[3] = {sliderValue, sliderValue, sliderValue}; // Store previous slider values for each button
bool isSliderEnabled = true;
void setup() {
Serial.begin(9600);
Serial.println(F("Cap Touch Paint!"));
tft.begin();
if (!ctp.begin(40)) {
Serial.println("Couldn't start FT6206 touchscreen controller");
while (1);
}
Serial.println("Capacitive touchscreen started");
tft.fillScreen(ILI9341_BLACK);
drawButtons();
drawSlider();
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
}
void loop() {
if (ctp.touched()) {
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(")");
handleButtonPress(p);
handleSliderPress(p);
}
}
void handleButtonPress(TS_Point p) {
bool clickedOnButton = false;
int selectedButton = -1;
for (int i = 0; i < 3; i++) {
int rectX = startX + i * (rectSize + rectSpacing);
if (isInsideRect(p.x, p.y, rectX, rectY, rectSize, rectSize)) {
clickedOnButton = true;
selectedButton = i;
break;
}
}
if (clickedOnButton) {
// Turn off the previously selected button LED
for (int i = 0; i < 3; i++) {
if (rectSelected[i]) {
rectSelected[i] = false;
rectColors[i] = ILI9341_WHITE;
digitalWrite(LED_PIN, LOW);
break;
}
}
// Turn on the selected button LED
rectSelected[selectedButton] = true;
rectColors[selectedButton] = ILI9341_RED;
digitalWrite(LED_PIN, HIGH);
// Update slider value and position
isSliderEnabled = true;
sliderValue = prevSliderValue[selectedButton];
sliderPosition = map(sliderValue, sliderMin, sliderMax, sliderX, sliderX + sliderWidth);
drawButtons();
drawSlider();
}
}
void handleSliderPress(TS_Point p) {
if (isSliderEnabled && isInsideRect(p.x, p.y, sliderX, sliderY, sliderWidth, sliderHeight)) {
int newPosition = constrain(p.x, sliderX, sliderX + sliderWidth);
int newValue = map(newPosition, sliderX, sliderX + sliderWidth, sliderMin, sliderMax);
if (newValue != sliderValue) {
sliderValue = newValue;
sliderPosition = newPosition;
drawSlider();
for (int i = 0; i < 3; i++) {
if (rectSelected[i]) {
prevSliderValue[i] = sliderValue;
break;
}
}
}
}
}
bool isInsideRect(int x, int y, int rectX, int rectY, int rectWidth, int rectHeight) {
return (x >= rectX && x <= rectX + rectWidth && y >= rectY && y <= rectY + rectHeight);
}
void drawButtons() {
tft.setTextColor(ILI9341_WHITE);
// Draw first button - Brightness
tft.setTextSize(1.8); // Decrease font size
tft.setCursor(startX - 10, rectY - 25);
tft.print("BRIGHTNESS");
drawButton(startX, rectY, rectColors[0]);
// Draw second button - Volume
tft.setTextSize(1.8); // Decrease font size
tft.setCursor(startX + rectSize + rectSpacing, rectY - 25);
tft.print("VOLUME");
drawButton(startX + rectSize + rectSpacing, rectY, rectColors[1]);
// Draw third button - Fan
tft.setTextSize(1.8); // Decrease font size
int16_t x, y;
uint16_t width, height;
tft.getTextBounds("Fan", 0, 0, &x, &y, &width, &height);
tft.setCursor(startX + 2 * (rectSize + rectSpacing) + (rectSize - width) / 2, rectY - 25);
tft.print("FAN");
drawButton(startX + 2 * (rectSize + rectSpacing), rectY, rectColors[2]);
}
void drawButton(int x, int y, uint16_t color) {
int buttonWidth = rectSize;
int buttonHeight = rectSize;
int buttonRadius = 5;
tft.fillRoundRect(x, y, buttonWidth, buttonHeight, buttonRadius, color);
int lineSpacing = 6;
for (int i = lineSpacing; i < buttonWidth; i += lineSpacing) {
tft.drawLine(x + i, y, x + i, y + buttonHeight - 1, ILI9341_BLACK); // V
}
for (int i = lineSpacing; i < buttonHeight; i += lineSpacing) {
tft.drawLine(x, y + i, x + buttonWidth - 1, y + i, ILI9341_BLACK); // H
}
}
void drawSlider() {
tft.fillRect(sliderX, sliderY, sliderWidth, sliderHeight, ILI9341_WHITE);
int sliderBarWidth = map(sliderValue, sliderMin, sliderMax, 0, sliderWidth);
tft.fillRect(sliderX, sliderY, sliderBarWidth, sliderHeight, ILI9341_RED);
}