/************************************
The provided code uses the Adafruit ILI9341 library to control a
10 by 10 matrix display. The matrix is initially filled with small
squares, each colored in a rainbow pattern. The loop() function
creates a simple animation by shifting colors diagonally across
the matrix. The animation includes updating the top-left corner
with a random color. The speed of the animation can be adjusted
by changing the delay value. Overall, the code showcases how
to manipulate and animate a 10 by 10 matrix on an ILI9341
TFT display.
copywrite by arvind 25/11/2023
************************************************/
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <SPI.h>
#define TFT_CS 10
#define TFT_DC 9
#define TFT_RST -1
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
int maxSquaresX;
int maxSquaresY;
int squareSizeX;
int squareSizeY;
uint16_t colors[10][10];
void setup() {
// Initialize TFT display
tft.begin();
// Set the color mode to use RGB values
tft.setRotation(3); // Adjust the rotation if needed
// Get the dimensions of the TFT screen
int screenWidth = tft.width();
int screenHeight = tft.height();
// Calculate the maximum number of squares in both dimensions
maxSquaresX = 10;
maxSquaresY = 10;
// Draw the big square
tft.fillScreen(ILI9341_WHITE);
tft.drawRect(0, 0, screenWidth, screenHeight, ILI9341_BLACK);
// Initialize colors with rainbow pattern
for (int i = 0; i < maxSquaresY; i++) {
for (int j = 0; j < maxSquaresX; j++) {
colors[i][j] = tft.color565(255 * j / maxSquaresX, 255 * (maxSquaresX - j) / maxSquaresX, 0);
}
}
// Draw and fill the small squares with initial colors
squareSizeX = screenWidth / maxSquaresX;
squareSizeY = screenHeight / maxSquaresY;
for (int i = 0; i < maxSquaresY; i++) {
for (int j = 0; j < maxSquaresX; j++) {
int x = j * squareSizeX;
int y = i * squareSizeY;
tft.fillRect(x, y, squareSizeX, squareSizeY, colors[i][j]);
}
}
// Print the total number of squares to the serial monitor
Serial.begin(9600);
// Print text on the TFT display in yellow color
tft.setCursor(10, 50);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(3);
tft.print("Animation by Arvind Patil");
delay(1500);
}
void loop() {
// Shift the colors diagonally
for (int i = maxSquaresY - 1; i > 0; i--) {
for (int j = maxSquaresX - 1; j > 0; j--) {
colors[i][j] = colors[i - 1][j - 1];
}
}
// Update the first row with new colors
for (int j = maxSquaresX - 1; j > 0; j--) {
colors[0][j] = colors[maxSquaresY - 1][j - 1];
}
// Update the first column with new colors
for (int i = maxSquaresY - 1; i > 0; i--) {
colors[i][0] = colors[i - 1][maxSquaresX - 1];
}
// Update the top-left corner with a new color
colors[0][0] = tft.color565(random(256), random(256), random(256));
// Update the colors of the small squares on the display
for (int i = 0; i < maxSquaresY; i++) {
for (int j = 0; j < maxSquaresX; j++) {
int x = j * squareSizeX;
int y = i * squareSizeY;
tft.fillRect(x, y, squareSizeX, squareSizeY, colors[i][j]);
}
}
// Add a small delay to control the speed of the animation
delay(1000);
}
tft code by arvind.
अरविन्द पाटील 23/11/23 .