/***************************************************
The code showcases basic graphics animations on the
TFT display with a variety of colors. The animations
include a moving square, a rising circle, and a
diagonal-traveling triangle. The loop repeats these
animations with different
colors.
by arvind patil
15/12/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);
// Define some rainbow colors
#define TFT_RED 0xF800
#define TFT_ORANGE 0xFD20
#define TFT_YELLOW 0xFFE0
#define TFT_GREEN 0x07E0
#define TFT_BLUE 0x001F
#define TFT_INDIGO 0x4810
#define TFT_VIOLET 0x780F
// 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 square
int y = 100; // y-coordinate of the square
int size = 50; // size of the square
int xSpeed = 3; // horizontal speed of the square
while (x < tft.width()) {
tft.fillRect(x, y, size, size, color);
delay(50); // Small delay to control the animation speed
tft.fillRect(x, y, size, size, ILI9341_BLACK); // Clear the previous square
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(TFT_RED);
// Draw the second animation with orange color
animate2(TFT_ORANGE);
// Draw the third animation with yellow color
animate3(TFT_YELLOW);
// Repeat the animations with the other rainbow colors
animate1(TFT_GREEN);
animate2(TFT_BLUE);
animate3(TFT_INDIGO);
animate1(TFT_VIOLET);
}
Loading
ili9341-cap-touch
ili9341-cap-touch