#include <Adafruit_ILI9341.h>
#include <SPI.h>
#define TFT_CS 10 // TFT chip select pin
#define TFT_DC 9 // TFT data/command pin
#define TFT_RST 8 // TFT reset pin
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
const int ballRadius = 10;
const int screenWidth = 320;
const int screenHeight = 240;
void setup() {
Serial.begin(115200);
tft.begin();
tft.setRotation(3); // Adjust the screen rotation if needed
tft.fillScreen(ILI9341_BLACK); // Clear the screen to black
}
void loop() {
// Initial position of the ball
int x = screenWidth / 2;
int y = screenHeight / 2;
// Initial movement direction
int directionX = 1;
// Speed of the animation
int speed = 3;
// Animation loop
while (true) {
// Erase the previous frame by drawing a black ball over it
tft.fillCircle(x, y, ballRadius, ILI9341_BLACK);
// Update the position of the ball
x += speed * directionX;
// Check for collisions with the screen edges
if (x <= ballRadius || x >= (screenWidth - ballRadius)) {
// Change the direction when the ball hits the edges
directionX = -directionX;
}
// Draw the ball at the updated position
tft.fillCircle(x, y, ballRadius, ILI9341_GREEN);
// Delay to control the animation speed
delay(30);
}
}