#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
// FPS
#define FRAME_PER_SECOND 1
// Name should be not over 10 characters else it will default to HOME/AWAY
String TEAM_A_NAME = "TESTING_A";
String TEAM_B_NAME = "AWAY";
// Button Define
#define button_1Point 2
#define button_2Point 3
#define button_3Point 4
#define TEAMA_SELECT 5
#define TEAMB_SELECT 6
#define button_stopClk 7
#define resetSClk 8
// Score variables
int teamA_score = 0;
int teamB_score = 0;
void setup() {
// LCD Initialization
lcd.init();
lcd.backlight();
// Pinmode declaration
pinMode(button_1Point, INPUT);
pinMode(button_2Point, INPUT);
pinMode(button_3Point, INPUT);
pinMode(button_stopClk, INPUT);
}
void teamTextDraw(){
// Number of strings
int teamA_StringLength = TEAM_A_NAME.length();
int teamB_StringLength = TEAM_B_NAME.length();
// Display Team A Name
if (teamA_StringLength > 10) {
lcd.setCursor(0, 0);
lcd.print("HOME");
} else {
lcd.setCursor(0, 0);
lcd.print(TEAM_A_NAME);
}
// Display Team B Name
if (teamB_StringLength > 10) {
lcd.setCursor(20 - teamB_StringLength, 0);
lcd.print("AWAY");
} else {
lcd.setCursor(20 - teamB_StringLength, 0);
lcd.print(TEAM_B_NAME);
}
}
void scoreDisplay(){
// Draw Team A Score
lcd.setCursor(1, 1);
lcd.print(teamA_score);
// Draw Team B Score
lcd.setCursor(20 - (String(teamB_score).length() + 1), 1);
lcd.print(teamB_score);
}
int secondCounter = 0;
int minuteCounter = 1;
int shotClockCounter = 0;
int quarter = 1;
void extraDetails(){
int stopClockState = digitalRead(button_stopClk);
if (stopClockState == LOW) {
if (secondCounter == 59) {
secondCounter = 0;
if (minuteCounter == 12) {
minuteCounter = 0;
if (quarter == 4) {
quarter = 1;
} else {
quarter++;
}
} else {
minuteCounter++;
}
} else {
secondCounter++;
}
// Shot Clock
if (shotClockCounter == 24) {
shotClockCounter = 0;
} else {
shotClockCounter++;
}
}
// Game Clock Display Timer
lcd.setCursor(0, 2);
lcd.print("GClock:");
lcd.setCursor(8,2);
int seconds = 59 - secondCounter;
int minutes = 12 - minuteCounter;
lcd.print(String(minutes) + ":" + String(seconds));
// Shot Clock Display Timer
lcd.setCursor(0, 3);
lcd.print("SClock:");
int shotClock = 24 - shotClockCounter;
lcd.setCursor(12, 3);
lcd.print("Q" + String(quarter));
lcd.setCursor(8,3);
lcd.print(String(shotClock));
}
void scoreSystem(){
if (digitalRead(TEAMA_SELECT) == HIGH) { // Selected Team A
if (digitalRead(button_1Point)) {
teamA_score++;
} else if (digitalRead(button_2Point)) {
teamA_score+= 2;
} else if (digitalRead(button_3Point)) {
teamA_score+= 3;
}
} else if (digitalRead(TEAMB_SELECT) == HIGH) { // Selected Team B
if (digitalRead(button_1Point)) {
teamB_score++;
} else if (digitalRead(button_2Point)) {
teamB_score+= 2;
} else if (digitalRead(button_3Point)) {
teamB_score+= 3;
}
}
}
void resetShotClock(){
if(digitalRead(resetSClk)){
shotClockCounter = 0;
}
}
void scoreBoard(){
extraDetails(); // Function to display details
teamTextDraw(); // Function to display Team Names
scoreDisplay(); // Function to display Score
scoreSystem(); // Function to process score
resetShotClock(); // Function to reset shot clock
}
void refresh() {
delay((1/FRAME_PER_SECOND)*1000);
lcd.clear();
}
void loop() {
scoreBoard();
refresh();
}