#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
// Pin definitions for the Wokwi Simulator display
#define TFT_DC 2
#define TFT_CS 15
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
int rectX = 0;
const int actionButton = 14;
void setup() {
tft.begin();
tft.setRotation(1); // Landscape mode
tft.fillScreen(ILI9341_BLACK);
pinMode(actionButton,INPUT_PULLUP);
}
void loop() {
// 3. Update position
int aButtonPressed = digitalRead(actionButton);
if(aButtonPressed == LOW){
// 1. Erase the old rectangle by drawing over it in black
tft.fillRect(rectX - 2, 100, 40, 40, ILI9341_BLACK);
rectX += 2;
// 2. Draw the new red rectangle
tft.fillRect(rectX, 100, 40, 40, ILI9341_RED);
}
if (rectX > 320) rectX = -40;
delay(10); // Control the speed
}