#include <TM1637Display.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16,2);
// Define TM1637 pins
const int CLK_PIN = 2;
const int DIO_PIN = 3;
bool colon = false;
TM1637Display display(CLK_PIN, DIO_PIN);
// Define button pins
const int startButtonPin = A0; //ปุ่มเขี่ยว
const int teamAScoreButtonPin = A1; //ปุ่มแดงเพิ่มสกอร์ทีมA
const int teamBScoreButtonPin = A2; //ปุ่มน้ำเงินเพิ่มสกอร์ทีมB
const int pauseButtonPin = A3; //ปุ่มดำ หยุดเวลา
const int resetButtonPin = 6; //ปุ่มเหลือง reset
// Define variables
int teamAScore = 0;
int teamBScore = 0;
int minutes = 10;
int seconds = 0;
bool isPaused = true;
bool pauseButtonState = false; // New variable to track pause button state
unsigned long pausedTime = 0;
void setup() {
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(5,0); //กำหนดตำแหน่งเริ่มต้นตัวอักษรเริ่มต้นที่ตัวที่ 5 แถวที่ 1
lcd.print("Basketball");
delay(1800);
lcd.setCursor(2,1);
lcd.print("TEAM A VS TEAM B");
delay(1600);
lcd.clear();
lcd.setCursor(5,1);
lcd.print("QUARTER ONE");
delay(1600);
lcd.clear();
lcd.begin(20, 4);
lcd.print("TeamA: 0 TeamB: 0");
lcd.setCursor(4, 1);
lcd.print("Time : 10:00");
lcd.setCursor(2, 3);
lcd.print("Press for Start!");
pinMode(startButtonPin, INPUT_PULLUP);
pinMode(teamAScoreButtonPin, INPUT_PULLUP);
pinMode(teamBScoreButtonPin, INPUT_PULLUP);
pinMode(pauseButtonPin, INPUT_PULLUP);
pinMode(resetButtonPin, INPUT_PULLUP);
display.setBrightness(0x0f);
display.showNumberDec(minutes * 100 + seconds, 0b01000000);
}
void loop() {
if (digitalRead(startButtonPin) == LOW && isPaused) {
startCountdown();
}
// Check if the pause button state has changed
bool newPauseButtonState = (digitalRead(pauseButtonPin) == LOW);
if (newPauseButtonState != pauseButtonState) {
if (newPauseButtonState) {
// Pause or resume the game
isPaused = !isPaused;
if (isPaused) {
// Stop countdown
pausedTime = millis();
lcd.setCursor(2, 3);
lcd.print(" Game Pause ! ");
} else {
// Resume countdown
unsigned long elapsedTime = millis() - pausedTime;
pausedTime += elapsedTime; // Update pausedTime to account for paused time
lcd.setCursor(2,3);
lcd.print(" Game Running !");
lcd.setCursor(11, 1);
lcd.print(" ");
lcd.setCursor(11, 1);
lcd.print(minutes);
lcd.print(":");
if (seconds < 10) {
lcd.print("0");
}
lcd.print(seconds);
}
}
pauseButtonState = newPauseButtonState;
delay(500);
}
if (!isPaused) {
if (digitalRead(resetButtonPin) == LOW) {
resetGame();
}
if (digitalRead(teamAScoreButtonPin) == LOW) {
teamAScore++;
updateScore();
delay(80);
}
if (digitalRead(teamBScoreButtonPin) == LOW) {
teamBScore++;
updateScore();
delay(80);
}
updateDisplay();
updateCountdown();
}
}
void startCountdown() {
isPaused = false;
lcd.setCursor(4, 1);
lcd.print("Time : ");
lcd.setCursor(0, 3);
lcd.print(" GAME START! ");
}
void updateScore() {
lcd.setCursor(7, 0);
lcd.print(" ");
lcd.setCursor(7, 0);
lcd.print(teamAScore);
lcd.setCursor(17, 0);
lcd.print(" ");
lcd.setCursor(17, 0);
lcd.print(teamBScore);
}
void updateDisplay() {
display.showNumberDecEx(minutes * 100 + seconds, 0b01000000);
}
void updateCountdown() {
static unsigned long lastUpdateTime = 0;
unsigned long currentTime = millis();
unsigned long elapsedTime = currentTime - lastUpdateTime;
if (elapsedTime >= 1000) {
lastUpdateTime = currentTime;
if (minutes == 0 && seconds == 0) {
lcd.setCursor(0, 3);
lcd.print("Time Out");
if (teamAScore > teamBScore) {
lcd.setCursor(13, 3);
lcd.print("Team A Wins");
} else if (teamBScore > teamAScore) {
lcd.setCursor(13, 3);
lcd.print("Team B Wins");
} else {
lcd.setCursor(13, 3);
lcd.print("Draw");
}
display.showNumberDec(0);
colon = !colon;
isPaused = true;
} else {
if (seconds == 0) {
seconds = 59;
minutes--;
} else {
seconds--;
}
lcd.setCursor(11, 1);
lcd.print(" ");
lcd.setCursor(11, 1);
lcd.print(minutes);
lcd.print(":");
if (seconds < 10) {
lcd.print("0");
}
lcd.print(seconds);
display.showNumberDecEx(minutes * 100 + seconds, colon);
}
}
}
void resetGame() {
isPaused = true;
teamAScore = 0;
teamBScore = 0;
minutes = 10;
seconds = 0;
lcd.setCursor(7, 0);
lcd.print(" ");
lcd.setCursor(7, 0);
lcd.print(teamAScore);
lcd.setCursor(17, 0);
lcd.print(" ");
lcd.setCursor(17, 0);
lcd.print(teamBScore);
lcd.setCursor(4, 1);
lcd.print("Time : 10:00");
lcd.setCursor(0, 3);
lcd.print(" Press for Start! ");
display.showNumberDecEx(minutes * 100 + seconds);
}