#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include <Adafruit_NeoPixel.h>
#include <TouchScreen.h>
// Define pins for the TFT display
#define TFT_DC 9
#define TFT_CS 10
// Define the pin for the Neopixel data line
#define PIN 6
// Define the number of LEDs in the strip
#define NUM_LEDS 4
// Define the pins for the 4 pushbuttons
const int BUTTON_PINS[4] = {2, 3, 4, 5};
// Calibration values for the touch screen
#define TS_MINX 150
#define TS_MAXX 920
#define TS_MINY 120
#define TS_MAXY 940
// Touchscreen pins
#define YP A3 // must be an analog pin, use "An" notation!
#define XM A2 // must be an analog pin, use "An" notation!
#define YM 9 // can be a digital pin
#define XP 8 // can be a digital pin
#define MINPRESSURE 10
#define MAXPRESSURE 1000
// Create a Neopixel object
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);
// Create TFT and Touch Screen objects
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
// Define effect modes
enum EffectMode { GRADIENT, SOLID_COLOR, OFF };
EffectMode currentEffect = GRADIENT;
// Variables for gradient effect
uint16_t hue = 0;
void setup() {
// Initialize the Neopixel strip
strip.begin();
strip.show(); // Initialize all pixels to 'off'
// Initialize the TFT display
tft.begin();
tft.setRotation(1);
tft.fillScreen(ILI9341_WHITE); // White background
// Draw the user interface
drawUI();
// Set up the button pins
for (int i = 0; i < 4; i++) {
pinMode(BUTTON_PINS[i], INPUT_PULLUP);
}
// Initialize serial communication for debugging
Serial.begin(9600);
}
void loop() {
// Always run the gradient effect in the background unless a physical button is pressed
if (currentEffect == GRADIENT) {
applyGradient();
}
bool anyButtonPressed = false;
// Check if any button is pressed
for (int i = 0; i < 4; i++) {
if (digitalRead(BUTTON_PINS[i]) == LOW) {
anyButtonPressed = true;
// Turn off all LEDs
strip.clear();
// Turn on the corresponding LED in green
strip.setPixelColor(i, strip.Color(0, 255, 0)); // Green color
strip.show();
break; // No need to check other buttons
}
}
// If no button is pressed, check for touchscreen input
if (!anyButtonPressed) {
TSPoint p = ts.getPoint();
if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
// Map the touch coordinates to the TFT display coordinates
p.x = map(p.x, TS_MINX, TS_MAXX, tft.width(), 0);
p.y = map(p.y, TS_MINY, TS_MAXY, tft.height(), 0);
// Check which button was pressed
if (p.x >= 10 && p.x <= 100 && p.y >= 220 && p.y <= 260) {
currentEffect = SOLID_COLOR;
} else if (p.x >= 110 && p.x <= 200 && p.y >= 220 && p.y <= 260) {
currentEffect = OFF;
}
// Update the display to show the selected effect
updateDisplay();
}
delay(100); // Small delay for debouncing
}
// Apply the current effect if it's not the gradient
if (currentEffect != GRADIENT) {
applyEffect();
}
}
void drawUI() {
// Draw buttons for different effects
tft.fillRect(10, 220, 90, 40, ILI9341_BLUE);
tft.setCursor(20, 230);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.print("Solid");
tft.fillRect(110, 220, 90, 40, ILI9341_RED);
tft.setCursor(120, 230);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.print("Off");
}
void updateDisplay() {
// Highlight the selected effect
switch (currentEffect) {
case SOLID_COLOR:
tft.drawRect(10, 220, 90, 40, ILI9341_YELLOW);
tft.drawRect(110, 220, 90, 40, ILI9341_BLACK);
break;
case OFF:
tft.drawRect(10, 220, 90, 40, ILI9341_BLACK);
tft.drawRect(110, 220, 90, 40, ILI9341_YELLOW);
break;
case GRADIENT:
tft.drawRect(10, 220, 90, 40, ILI9341_BLACK);
tft.drawRect(110, 220, 90, 40, ILI9341_BLACK);
break;
}
}
void applyEffect() {
switch (currentEffect) {
case SOLID_COLOR:
// Apply a solid color effect
for (int i = 0; i < NUM_LEDS; i++) {
strip.setPixelColor(i, strip.Color(255, 0, 0)); // Red color
}
strip.show();
break;
case OFF:
// Turn off the LEDs
strip.clear();
strip.show();
break;
default:
break;
}
}
void applyGradient() {
for (int i = 0; i < NUM_LEDS; i++) {
uint32_t color = strip.ColorHSV(hue + (65536 / NUM_LEDS) * i, 255, 255);
strip.setPixelColor(i, color);
}
strip.show();
hue += 256; // Adjust this value to control the speed of the gradient
delay(20); // Adjust this value to control the speed of the gradient
}