#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include <SPI.h>
// TFT display pins
#define TFT_CS 10
#define TFT_RST 9
#define TFT_DC 8
// Button pins
const int buttonPins[] = {2, 3, 4}; // Red, Yellow, Blue buttons
// Define colors using hexadecimal values
const uint16_t colors[] = {
0xF800, // Red
0xFFE0, // Yellow
0x001F // Blue
};
// Initialize display
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
// Debouncing variables
unsigned long lastDebounceTime = 0;
const unsigned long debounceDelay = 50;
void setup() {
// Initialize buttons with internal pullup resistors
for (int i = 0; i < 3; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
}
// Initialize TFT display
tft.initR(INITR_BLACKTAB);
tft.fillScreen(ST77XX_BLACK); // Clear the screen to black
tft.setRotation(1); // Set the rotation of the display
}
void loop() {
unsigned long currentTime = millis();
// Check button states and handle debouncing
for (int i = 0; i < 3; i++) {
bool buttonState = !digitalRead(buttonPins[i]); // Read button state (inverted due to INPUT_PULLUP)
// If button is pressed and debounce time has passed
if (buttonState && (currentTime - lastDebounceTime) > debounceDelay) {
tft.fillScreen(colors[i]); // Fill screen with the corresponding color
lastDebounceTime = currentTime; // Update the last debounce time
break; // Exit the loop after handling one button press
}
}
}