#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <SPI.h>

// Define pins for SPI communication
#define TFT_CS      10
#define TFT_DC      9
#define TFT_RST     8

// Initialize TFT object
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);

#define WIDTH      320
#define HEIGHT     240

// Define colors for the rainbow
uint16_t colors[6] = {
  tft.color565(255, 0, 0), // Red
  tft.color565(255, 127, 0), // Orange
  tft.color565(255, 255, 0), // Yellow
  tft.color565(0, 255, 0), // Green
  tft.color565(0, 0, 255), // Blue
  tft.color565(127, 0, 255), // Indigo
};

// Delay between frames
int delayVal = 50;

void setup() {
  Serial.begin(9600);

  tft.begin(); // Initialize ILI9341 display
  tft.setRotation(0);
  tft.fillScreen(ILI9341_BLACK); // Set background to black
}

void loop() {
  // Draw the rainbow gradient
  for (int y = 0; y < HEIGHT; y++) {
    for (int x = 0; x < WIDTH; x++) {
      int colorIndex = (x * 6) / WIDTH;
      uint16_t color = colors[colorIndex];
      tft.drawPixel(x, y, color);
    }
  }

  // Shift the color pattern right
  for (int x = WIDTH - 1; x > 0; x--) {
    uint16_t color = tft.drawPixel(x - 1, 0);
    tft.drawPixel(x, 0, color);
  }

  // Set first column black for chasing effect
  for (int y = 1; y < HEIGHT; y++) {
    tft.drawPixel(0, y, ILI9341_BLACK);
  }

  delay(delayVal); // Delay between frames
}
Use code with caution. Learn more