/***************************************************************************************************
The given Arduino code controls an ILI9341 TFT display and contains two animations. The code uses
the Adafruit_ILI9341 library to interact with the display. Here's a summary of the code:
Library and Pin Definitions:
The necessary libraries (Adafruit_ILI9341, Adafruit_GFX, and SPI) are included.
Pin numbers for TFT_RST, TFT_DC, TFT_CS, and TFT_LED are defined.
TFT Display Initialization:
The code sets up the TFT display, sets its rotation, and turns on the TFT backlight.
Animation 1 (Function animate1):
The first animation draws a rectangle that moves horizontally across the screen from left to right.
It takes a color (16-bit RGB565 format) as an argument, and the rectangle's color changes accordingly.
The animation clears the previous rectangle after a short delay to create the illusion of movement.
Animation 2 (Function animate2):
The second animation creates a grid of rectangles on the screen.
The color of each rectangle varies based on its position, producing a color gradient effect.
Setup Function:
The setup function initializes the TFT display, sets the rotation, and turns on the backlight.
It defines two colors for the animations: color1 (red) and color2 (blue).
Animation 1 (animate1) is executed with color1.
After a 2-second delay, the screen is cleared.
Animation 2 (animate2) is executed after the screen is cleared.
Loop Function:
The loop function is empty, as the animations run only once in the setup phase.
The code showcases how to use the ILI9341 TFT display to create simple animations with basic
shapes and colors.
by arvind patil 29/7/23
Copyright (c) 2014-2016 GitHub, Inc.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
by arvind patil 29/7/23
***************************************************************************************/
#include <Adafruit_ILI9341.h>
#include <Adafruit_GFX.h>
#include <SPI.h>
#define TFT_RST 8 // Reset pin (change to your wiring)
#define TFT_DC 9 // Data/Command pin (change to your wiring)
#define TFT_CS 10 // Chip Select pin (change to your wiring)
#define TFT_LED 7 // Backlight LED pin (change to your wiring)
// Create an instance of the ILI9341 TFT display
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
// Function to clear the screen
void clearScreen() {
tft.fillScreen(ILI9341_BLACK);
}
// Function to draw the first animation
void animate1(uint16_t color) {
int x = 0; // x-coordinate of the rectangle
int y = 100; // y-coordinate of the rectangle
int width = 50; // width of the rectangle
int height = 30; // height of the rectangle
int xSpeed = 3; // horizontal speed of the rectangle
while (x < tft.width()) {
tft.fillRect(x, y, width, height, color);
delay(50); // Small delay to control the animation speed
tft.fillRect(x, y, width, height, ILI9341_BLACK); // Clear the previous rectangle
x += xSpeed;
}
}
// Function to draw the second animation
void animate2(uint16_t color) {
int x = 100; // x-coordinate of the circle
int y = 0; // y-coordinate of the circle
int radius = 25; // radius of the circle
int ySpeed = 1; // vertical speed of the circle
while (y < tft.height()) {
tft.fillCircle(x, y, radius, color);
delay(20); // Small delay to control the animation speed
tft.fillCircle(x, y, radius, ILI9341_BLACK); // Clear the previous circle
y += ySpeed;
}
}
// Function to draw the third animation
void animate3(uint16_t color) {
int x = 0; // x-coordinate of the triangle
int y = 0; // y-coordinate of the triangle
int size = 40; // size of the triangle
int xSpeed = 1; // horizontal speed of the triangle
int ySpeed = 1; // vertical speed of the triangle
while (x < tft.width() - size && y < tft.height() - size) {
tft.fillTriangle(x, y, x + size, y, x + size / 2, y + size, color);
delay(20); // Small delay to control the animation speed
tft.fillTriangle(x, y, x + size, y, x + size / 2, y + size, ILI9341_BLACK); // Clear the previous triangle
x += xSpeed;
y += ySpeed;
}
}
void setup() {
// Initialize the display
tft.begin();
// Set the backlight brightness
analogWrite(TFT_LED, 128);
// Clear the screen
clearScreen();
}
void loop() {
// Draw the first animation with red color
animate1(ILI9341_RED);
// Draw the second animation with green color
animate2(ILI9341_GREEN);
// Draw the third animation with blue color
animate3(ILI9341_BLUE);
}