#include <Wire.h> // for I2C communication
#include <LiquidCrystal_I2C.h> // for LCD
#include <RTClib.h> // for RTC
#include <Button.h> // for debounce buttons
#include <SPI.h>
#include <Timer.h>
//#include <nRF24L01.h>
//#include <RF24.h>
int HomeScore=0; //Score for Home
int VisitorsScore=0; // Score for visitors
//int PlayTime=600; // 10 minutes playtime in seconds
int Round=1; // 1e round is first half , 2 is 2e half.
int t=0;
int s=0;
int m=0;
// Countdown Timer for playtime
unsigned long startTime;
unsigned long currentTime;
unsigned long elapsedTime;
unsigned long remainingTime;
unsigned long pausedTime = 0;
unsigned long COUNTDOWN_TIME = 600; // 5 minutes in seconds
int Pause_Time=1; // 1 for pause 0 for running
Button switchHomeP(2); // plus button Score Home
Button switchHomeM(3); // Minus Button Score Homr
Button switchVitorsP(4); // Plus Button Score Visitors
Button switchVitorsM(5); // Minus Button Score Visitors
Button PlaytimeP(6); // playtime plus 1 sec
Button PlaytimeM(7); // playtime minus 1 sec
Button PlaytimeS(8); // Playtime Start Countdown from 10min to 0
Button greencardHome(14); // 2 minute wait greeg card
Button greencardVisitors(15); // 2 minute wait green card
Button ShotClockStart(16); // Shotclock start, countdown from 60 sec
Button ShotClockpause(17); // Schotclock pause
Button ShotClockStop(18); // stop shotclock
Button MirroScoreboard(19); // mirror scoreboard (NOT THE REMOTE !!!!!!)
// reset button ?
LiquidCrystal_I2C lcd(0x27, 16, 2); // create LCD with I2C address 0x27, 16 characters per line, 2 lines
RTC_DS3231 rtc; // create rtc for the DS3231 RTC module, address is fixed at 0x68
/*
function to update RTC time using user input
*/
void updateRTC()
{
lcd.clear(); // clear LCD display
lcd.setCursor(0, 0);
lcd.print("Edit Mode...");
// ask user to enter new date and time
const char txt[6][15] = { "year [4-digit]", "month [1~12]", "day [1~31]",
"hours [0~23]", "minutes [0~59]", "seconds [0~59]"};
String str = "";
long newDate[6];
while (Serial.available()) {
Serial.read(); // clear serial buffer
}
for (int i = 0; i < 6; i++) {
Serial.print("Enter ");
Serial.print(txt[i]);
Serial.print(": ");
while (!Serial.available()) {
; // wait for user input
}
str = Serial.readString(); // read user input
newDate[i] = str.toInt(); // convert user input to number and save to array
Serial.println(newDate[i]); // show user input
}
// update RTC
rtc.adjust(DateTime(newDate[0], newDate[1], newDate[2], newDate[3], newDate[4], newDate[5]));
Serial.println("RTC Updated!");
}
/*
function to update LCD text
*/
void updateLCD()
{
// get time and date from RTC and save in variables
DateTime rtcTime = rtc.now();
int ss = rtcTime.second();
int mm = rtcTime.minute();
int hh = rtcTime.hour();
// move LCD cursor to upper-left position
lcd.setCursor(0, 0);
// move LCD cursor to upper-left position
lcd.setCursor(4, 0);
// Display time in 12H format
if (hh < 10) lcd.print("0"); lcd.print(hh);
lcd.print(':');
if (mm < 10) lcd.print("0"); lcd.print(mm);
lcd.print(':');
if (ss < 10) lcd.print("0"); lcd.print(ss);
// move LCD cursor to lower-left position
lcd.setCursor(0, 1);
if (HomeScore < 10) lcd.print("0"); lcd.print(HomeScore);
t = remainingTime; s = t % 60; t = (t - s)/60; m = t % 60;
lcd.setCursor(6, 1);
//lcd.print(PlayTime);
if (m < 10) lcd.print("0");
lcd.print(m);
lcd.print(':');
if (s < 10) lcd.print("0");
lcd.print(s);
lcd.setCursor(14, 1);
if (VisitorsScore < 10) lcd.print("0");
lcd.print(VisitorsScore);
}
void setup()
{
startTime = millis(); // Record the starting time
switchHomeP.begin();
switchHomeM.begin();
switchVitorsP.begin();
switchVitorsM.begin();
PlaytimeP.begin();
PlaytimeM.begin();
PlaytimeS.begin();
// Set input pins
Serial.begin(9600); // initialize serial
lcd.init(); // initialize lcd
lcd.backlight(); // switch-on lcd backlight
rtc.begin(); // initialize rtc
}
void loop()
{
currentTime = millis(); // Get the current time
elapsedTime = (currentTime - startTime) / 1000; // Calculate elapsed time in seconds
//if (elapsedTime <= COUNTDOWN_TIME) {remainingTime = COUNTDOWN_TIME - elapsedTime;}
if (elapsedTime <= COUNTDOWN_TIME && Pause_Time == 1) { pausedTime = elapsedTime;
Serial.print("Pause ");
Serial.print(" remainingTime:");Serial.print(remainingTime);
Serial.print(" COUNTDOWN_TIME:");Serial.print(COUNTDOWN_TIME);
Serial.print(" pausedTime:");Serial.print(pausedTime);
Serial.print(" elapsedTime:");Serial.print(elapsedTime);
} //pause time
if (elapsedTime <= COUNTDOWN_TIME && Pause_Time == 0)
{
remainingTime = COUNTDOWN_TIME - elapsedTime + pausedTime; //running time
Serial.print("running ");
Serial.print(" remainingTime:");Serial.print(remainingTime);
Serial.print(" COUNTDOWN_TIME:");Serial.print(COUNTDOWN_TIME);
Serial.print(" pausedTime:");Serial.print(pausedTime);
Serial.print(" elapsedTime:");Serial.print(elapsedTime);
}
ReadButtons(); // Read Buttons on Remote
updateLCD(); // update LCD text
if (Serial.available()) {
char input = Serial.read();
if (input == 'u') updateRTC(); // update RTC time
}
}
void ReadButtons() {
if (switchHomeP.pressed()) {HomeScore++;}
if (switchHomeM.pressed()) {HomeScore--;}
if (switchVitorsP.pressed()){VisitorsScore++;}
if (switchVitorsM.pressed()){VisitorsScore--;}
if (PlaytimeP.pressed()) {remainingTime++;}
if (PlaytimeM.pressed()) {remainingTime--;}
if (PlaytimeS.pressed()) {Pause_Time = !Pause_Time;}
// correct unwanted results
if (HomeScore==-1) {HomeScore=0;}
if (VisitorsScore==-1) {VisitorsScore=0;}
}