#include <TFT_eSPI.h>
TFT_eSPI tft = TFT_eSPI();
const int btnLeftPin = 34;
const int btnRightPin = 35;
int btnLeftState = HIGH;
int btnRightState = HIGH;
int btnLeftPrevState = HIGH;
int btnRightPrevState = HIGH;
int screenWidth;
int screenHeight;
int rectWidth;
int rectHeight;
int spacing;
int rectX;
int rectY;
int visibleRectCount = 6;
void setup() {
tft.begin();
tft.setRotation(1);
tft.fillScreen(TFT_BLACK);
screenWidth = tft.width();
screenHeight = tft.height();
rectWidth = screenWidth / 7;
rectHeight = screenHeight / 12;
spacing = rectHeight / 2;
rectX = 0;
rectY = screenHeight - rectHeight;
drawRectangles();
}
void loop() {
btnLeftState = digitalRead(btnLeftPin);
btnRightState = digitalRead(btnRightPin);
if (btnLeftState == LOW && btnLeftPrevState == HIGH) {
hideRectangle();
}
if (btnRightState == LOW && btnRightPrevState == HIGH) {
showRectangle();
}
btnLeftPrevState = btnLeftState;
btnRightPrevState = btnRightState;
}
void drawRectangles() {
for (int i = 0; i < visibleRectCount; i++) {
tft.fillRect(rectX, rectY, rectWidth, rectHeight, TFT_WHITE);
rectX += rectWidth + spacing;
}
}
void hideRectangle() {
if (visibleRectCount > 0) {
rectX -= rectWidth + spacing;
tft.fillRect(rectX, rectY, rectWidth, rectHeight, TFT_BLACK);
visibleRectCount--;
}
}
void showRectangle() {
if (visibleRectCount < 6) {
tft.fillRect(rectX, rectY, rectWidth, rectHeight, TFT_WHITE);
rectX += rectWidth + spacing;
visibleRectCount++;
}
}