#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <SPI.h>
// Pin connections for ESP32 and ILI9341
#define TFT_CS 15
#define TFT_DC 2
#define TFT_RST 4
#define TFT_MOSI 23
#define TFT_CLK 18
#define TFT_MISO 19
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST, TFT_MOSI, TFT_CLK, TFT_MISO);
// Ball parameters
int ballSize = 10;
int ballX = 50;
int ballY = 50;
int ballSpeedX = 2;
int ballSpeedY = 2;
void setup() {
Serial.begin(115200);
tft.begin();
tft.setRotation(3); // Adjust the rotation if needed
Serial.println("Initializing display...");
// Fill the screen with a background color
tft.fillScreen(ILI9341_BLACK);
}
void loop() {
// Clear the previous position of the ball
tft.fillCircle(ballX, ballY, ballSize, ILI9341_BLACK);
// Update ball position
ballX += ballSpeedX;
ballY += ballSpeedY;
// Bounce off the edges
if (ballX < 0 || ballX > tft.width() - ballSize) {
ballSpeedX = -ballSpeedX;
}
if (ballY < 0 || ballY > tft.height() - ballSize) {
ballSpeedY = -ballSpeedY;
}
// Draw the ball at the new position
tft.fillCircle(ballX, ballY, ballSize, ILI9341_GREEN);
// Add a delay for the animation
delay(20);
}
Loading
esp32-devkit-v1
esp32-devkit-v1