#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
// TFT display pins
#define TFT_CS 10
#define TFT_RST 9
#define TFT_DC 8
// Joystick pins
#define JOY_HORZ A0
#define JOY_VERT A1
#define JOY_SEL 7 // Optional button pin
// Initialize the display
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
// Ball position and properties
int ballX = 120;
int ballY = 160;
const int ballRadius = 5;
void setup() {
tft.begin();
tft.fillScreen(ILI9341_BLACK);
tft.fillCircle(ballX, ballY, ballRadius, ILI9341_RED);
}
void loop() {
int xVal = analogRead(JOY_HORZ);
int yVal = analogRead(JOY_VERT);
// Deadzone to avoid drift
int threshold = 100;
int dx = 0, dy = 0;
// Inverted horizontal direction
if (xVal < 512 - threshold) dx = 1; // right
if (xVal > 512 + threshold) dx = -1; // left
if (yVal < 512 - threshold) dy = 1; // down
if (yVal > 512 + threshold) dy = -1; // up
// Erase previous ball
tft.fillCircle(ballX, ballY, ballRadius, ILI9341_BLACK);
// Update position
ballX += dx * 2;
ballY += dy * 2;
// Keep ball within screen bounds
ballX = constrain(ballX, ballRadius, tft.width() - ballRadius);
ballY = constrain(ballY, ballRadius, tft.height() - ballRadius);
// Draw new ball
tft.fillCircle(ballX, ballY, ballRadius, ILI9341_RED);
delay(20);
}