#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_ILI9341.h> // Hardware-specific library
#include <SPI.h>
#define TFT_CS 10
#define TFT_RST 9
#define TFT_DC 8
#define PLAYER1_BUTTON_PIN 2 // Button for Player 1
#define PLAYER2_BUTTON_PIN 3 // Button for Player 2
#define SWITCH_GAME_BUTTON_PIN 4 // Button to switch games
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
volatile int player1Score = 0;
volatile int player2Score = 0;
volatile int currentGame = 1; // Current game number
volatile bool player1ButtonPressed = false;
volatile bool player2ButtonPressed = false;
volatile bool switchGameButtonPressed = false;
void player1ButtonInterrupt() {
player1ButtonPressed = true;
}
void player2ButtonInterrupt() {
player2ButtonPressed = true;
}
void switchGameButtonInterrupt() {
switchGameButtonPressed = true;
}
void setup() {
Serial.begin(9600);
tft.begin();
tft.setRotation(3); // Adjust display rotation if needed
pinMode(PLAYER1_BUTTON_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(PLAYER1_BUTTON_PIN), player1ButtonInterrupt, FALLING);
pinMode(PLAYER2_BUTTON_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(PLAYER2_BUTTON_PIN), player2ButtonInterrupt, FALLING);
pinMode(SWITCH_GAME_BUTTON_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(SWITCH_GAME_BUTTON_PIN), switchGameButtonInterrupt, FALLING);
drawScore();
}
void loop() {
if (player1ButtonPressed) {
player1Score++;
drawScore();
player1ButtonPressed = false;
}
if (player2ButtonPressed) {
player2Score++;
drawScore();
player2ButtonPressed = false;
}
if (switchGameButtonPressed) {
// Increment current game number
currentGame++;
// Reset scores for the new game
player1Score = 0;
player2Score = 0;
drawScore();
switchGameButtonPressed = false;
}
}
void drawScore() {
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(10, 10);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(3);
tft.print("Game: ");
tft.print(currentGame);
// Draw player 1 score
tft.setTextSize(3);
tft.setCursor(10, 50);
tft.print("Player 1: ");
tft.print(player1Score);
// Draw player 2 score
tft.setTextSize(3);
tft.setCursor(10, 90);
tft.print("Player 2: ");
tft.print(player2Score);
}