/*Basketball code * This program is the scoreboard used in a typical basketball match. * A total of 9 7-segment displays and 2 16x2 LCD displays are used. * The counters and clocks used are the same as the ones used in the supporting sketches. */ #include #include #define CLK1 38 #define DIO1 39 #define CLK2 22 #define DIO2 23 #define CLK3 36 #define DIO3 37 #define CLK4 34 #define DIO4 35 #define CLK5 24 #define DIO5 25 #define CLK6 30 #define DIO6 31 #define CLK7 28 #define DIO7 29 #define CLK8 32 #define DIO8 33 #define CLK9 26 #define DIO9 27 #define INCREMENT_TEAM_1_SCORE 40 #define INCREMENT_TEAM_2_SCORE 41 #define INCREMENT_TEAM_1_FOULS 42 #define INCREMENT_TEAM_2_FOULS 43 #define TEAM_FOULS_RESET 44 #define INCREMENT_QUARTER 45 #define GAME_CLOCK_RESET 46 #define GAME_CLOCK_PAUSE 47 #define GAME_CLOCK_LOAD 48 #define TEAM_CLOCK_RESET24 49 #define TEAM_CLOCK_RESET14 50 #define TEAM_CLOCK_PAUSE 51 #define INCREMENT_DECREMENT_BUTTON 53 #define CLOCK_INITIAL_VALUE_LOAD_INPUT A0 SevenSegmentTM1637 display_1(CLK1,DIO1); SevenSegmentTM1637 display_2(CLK2,DIO2); SevenSegmentTM1637 display_3(CLK3,DIO3); SevenSegmentTM1637 display_4(CLK4,DIO4); SevenSegmentTM1637 display_5(CLK5,DIO5); SevenSegmentTM1637 display_6(CLK6,DIO6); SevenSegmentTM1637 display_7(CLK7,DIO7); SevenSegmentTM1637 display_8(CLK8,DIO8); SevenSegmentTM1637 display_9(CLK9,DIO9); LiquidCrystal lcd1(2,3,4,5,6,7); //RS, EN, D4, D5, D6, D7 LiquidCrystal lcd2(8,9,10,11,12,13); int team_1_score = 0, team_1_score_increment = 0; int team_2_score = 0, team_2_score_increment = 0; int team_1_fouls = 0, team_1_fouls_increment = 0; int team_2_fouls = 0, team_2_fouls_increment = 0; int quarter_count = 0, quarter_count_increment = 0; int CTRL_LOAD = 0, CTRL_RESET = 0, CTRL_PAUSE = 0, PAUSE_CLOCK = 0, GAME_CLOCK_MIN = 0, GAME_CLOCK_SEC = 0, CLOCK_ONE_SECOND_COUNT = 0; int TEAM_CLOCK_RESET24_CTRL = 0, TEAM_CLOCK_RESET14_CTRL = 0; int TEAM_CLOCK_PAUSE_CTRL = 0; int TEAM_PAUSE_CLOCK = 2; int team_1_clock_value = 24, team_2_clock_value = 24; String entry, lcd1_current_entry, lcd2_current_entry; void display_initialisation(SevenSegmentTM1637 &a, bool colon_value) { a.init(); a.on(); a.setColonOn(colon_value); } void display_data(SevenSegmentTM1637 &a, int e, int d, int c, int b) //This function displays a 4 digit number onto the 7-segment display { a.setCursor(0,0); a.print(b); a.setCursor(0,1); a.print(c); a.setCursor(0,2); a.print(d); a.setCursor(0,3); a.print(e); } void increment_decrement_count(int &count, int button, int &control, int increment_decrement) { if(button == 1) { control = 1; } if(button == 0 and control == 1 and increment_decrement == 0) { count--; control = 0; } else if(button == 0 and control == 1 and increment_decrement == 1) { count++; control = 0; } } void display_game_clock(int LOAD, int &CTRL_LOAD, int RESET, int &CTRL_RESET, int PAUSE, int &CTRL_PAUSE, int &GAME_CLOCK_SEC, int &GAME_CLOCK_MIN, int &PAUSE_CLOCK, int &GAME_CLOCK_ONE_SECOND_COUNT) { if(RESET == 1) { CTRL_RESET = 1; } if(RESET == 0 and CTRL_RESET == 1) { GAME_CLOCK_ONE_SECOND_COUNT = 0; GAME_CLOCK_MIN = 12; GAME_CLOCK_SEC = 0; PAUSE_CLOCK = 0; CTRL_RESET = 0; } if(LOAD == 1) { CTRL_LOAD = 1; } if(LOAD == 0 and CTRL_LOAD == 1) { int initial_value = analogRead(CLOCK_INITIAL_VALUE_LOAD_INPUT); int load_clock = 0.71*initial_value; GAME_CLOCK_SEC = load_clock%60; GAME_CLOCK_MIN = (load_clock/60); CTRL_LOAD = 0; PAUSE_CLOCK = 0; } if(PAUSE == 1) { CTRL_PAUSE= 1; } if(PAUSE == 0 and CTRL_PAUSE == 1) { if(PAUSE_CLOCK == 0) { PAUSE_CLOCK = 1; } else { PAUSE_CLOCK = 0; } CTRL_PAUSE = 0; } if(PAUSE_CLOCK == 1) { GAME_CLOCK_ONE_SECOND_COUNT++; if(GAME_CLOCK_ONE_SECOND_COUNT == 39) { if(GAME_CLOCK_SEC == 0) { GAME_CLOCK_SEC = 59; GAME_CLOCK_MIN--; } else { GAME_CLOCK_SEC--; } } } } void reset_team_clock_to_24_and_stop(int TEAM_CLOCK_RESET_24, int &TEAM_CLOCK_RESET24_CTRL) { if(TEAM_CLOCK_RESET_24 == 1) { TEAM_CLOCK_RESET24_CTRL = 1; } if(TEAM_CLOCK_RESET_24 == 0 and TEAM_CLOCK_RESET24_CTRL == 1) { team_1_clock_value = 24; team_2_clock_value = 24; TEAM_PAUSE_CLOCK = 2; TEAM_CLOCK_RESET24_CTRL = 0; } } void reset_team_clock_to_14_and_start(int TEAM_CLOCK_RESET_14, int &TEAM_CLOCK_RESET14_CTRL, int &team_clock_value) { if(TEAM_CLOCK_RESET_14 == 1) { TEAM_CLOCK_RESET14_CTRL = 1; } if(TEAM_CLOCK_RESET_14 == 0 and TEAM_CLOCK_RESET14_CTRL == 1) { team_clock_value = 14; TEAM_CLOCK_RESET14_CTRL = 0; } } void pause_start_team_clock(int PAUSE, int &TEAM_CLOCK_PAUSE_CTRL) { if(PAUSE == 1) { TEAM_CLOCK_PAUSE_CTRL = 1; } if(PAUSE == 0 and TEAM_CLOCK_PAUSE_CTRL == 1) { if(TEAM_PAUSE_CLOCK == 1) { team_1_clock_value = 24; TEAM_PAUSE_CLOCK = 0; } else { team_2_clock_value = 24; TEAM_PAUSE_CLOCK = 1; } TEAM_CLOCK_PAUSE_CTRL = 0; } } void control_team_clock(int &team_clock_value, int start_stop) { if(start_stop == 1 and team_clock_value > 0) { if(CLOCK_ONE_SECOND_COUNT == 37) { team_clock_value--; } } } void setup() { display_initialisation(display_1,false) ; display_initialisation(display_2,false) ; display_initialisation(display_3,true) ; display_initialisation(display_4,false) ; display_initialisation(display_5,false) ; display_initialisation(display_6,false) ; display_initialisation(display_7,true) ; display_initialisation(display_8,false) ; display_initialisation(display_9,true) ; display_data(display_1,1,0,0,0); display_data(display_2,2,0,0,0); display_data(display_3,3,0,0,0); display_data(display_4,4,0,0,0); display_data(display_5,5,0,0,0); display_data(display_6,6,0,0,0); display_data(display_7,7,0,0,0); display_data(display_8,8,0,0,0); display_data(display_9,9,0,0,0); Serial.begin(9600); Serial.println("LCD Display Controls"); Serial.println("To display text in the 1st display, type 1 followed by the data without any space in between them."); Serial.println("To display text in the 2nd display, type 2 followed by the data without any space in between them."); lcd1.begin(16,2); lcd2.begin(16,2); lcd1.setCursor(0,0); lcd2.setCursor(0,0); lcd1.clear(); lcd2.clear(); delay(1500); } void loop() { //////////////////////////////////////LCD CONTROL//////////////////////////////////////////////////////// //Serial.print("Serial available = "); //Serial.println(Serial.available()); if(Serial.available()) { entry = Serial.readString(); if(entry.charAt(0) == '1') { entry.remove(0,1); lcd1.clear(); lcd1.print(entry); lcd1_current_entry = entry; } else if(entry.charAt(0) == '2') { entry.remove(0,1); lcd2.clear(); lcd2.print(entry); lcd2_current_entry = entry; } } //Serial.print("entry = "); //Serial.println(entry); //Serial.print("lcd1_current_entry"); //Serial.println(lcd1_current_entry); //Serial.print("lcd2_current_entry"); //Serial.println(lcd2_current_entry); //////////////////////////////////////DISPLAY SCORE COUNTERS///////////////////////////////////////////// display_data(display_2, team_1_score%10, (team_1_score/10)%10, (team_1_score/100)%10, team_1_score/1000); display_data(display_6, team_2_score%10, (team_2_score/10)%10, (team_2_score/100)%10, team_2_score/1000); //////////////////////////////////////DISPLAY FOUL COUNTERS////////////////////////////////////////////// display_data(display_1, team_1_fouls%10, (team_1_fouls/10)%10, (team_1_fouls/100)%10, team_1_fouls/1000); display_data(display_5, team_2_fouls%10, (team_2_fouls/10)%10, (team_2_fouls/100)%10, team_2_fouls/1000); //////////////////////////////////////DISPLAY QUARTER COUNTER//////////////////////////////////////////// display_data(display_4, quarter_count%10, (quarter_count/10)%10, (quarter_count/100)%10, quarter_count/1000); //////////////////////////////////////INCREMENT OR DECREMENT SCORE COUNTERS////////////////////////////// increment_decrement_count(team_1_score, digitalRead(INCREMENT_TEAM_1_SCORE), team_1_score_increment, digitalRead(INCREMENT_DECREMENT_BUTTON)); increment_decrement_count(team_2_score, digitalRead(INCREMENT_TEAM_2_SCORE), team_2_score_increment, digitalRead(INCREMENT_DECREMENT_BUTTON)); //////////////////////////////////////INCREMENT OR DECREMENT FOULS COUNTERS////////////////////////////// if(team_1_fouls < 5) { increment_decrement_count(team_1_fouls, digitalRead(INCREMENT_TEAM_1_FOULS), team_1_fouls_increment, digitalRead(INCREMENT_DECREMENT_BUTTON)); } if(team_2_fouls < 5) { increment_decrement_count(team_2_fouls, digitalRead(INCREMENT_TEAM_2_FOULS), team_2_fouls_increment, digitalRead(INCREMENT_DECREMENT_BUTTON)); } if(digitalRead(TEAM_FOULS_RESET) == LOW) { team_1_fouls = 0; team_2_fouls = 0; } //////////////////////////////////////INCREMENT OR DECREMENT QUARTERS//////////////////////////////////// increment_decrement_count(quarter_count, digitalRead(INCREMENT_QUARTER), quarter_count_increment, digitalRead(INCREMENT_DECREMENT_BUTTON)); //////////////////////////////////////TEAM CLOCKS//////////////////////////////////////////////////////// reset_team_clock_to_24_and_stop(digitalRead(TEAM_CLOCK_RESET24), TEAM_CLOCK_RESET24_CTRL); reset_team_clock_to_24_and_stop(digitalRead(TEAM_CLOCK_RESET24), TEAM_CLOCK_RESET24_CTRL); pause_start_team_clock(digitalRead(TEAM_CLOCK_PAUSE), TEAM_CLOCK_PAUSE_CTRL); if(TEAM_PAUSE_CLOCK != 2) { control_team_clock(team_1_clock_value, TEAM_PAUSE_CLOCK); control_team_clock(team_2_clock_value, not TEAM_PAUSE_CLOCK); } if(TEAM_PAUSE_CLOCK == 1) { reset_team_clock_to_14_and_start(digitalRead(TEAM_CLOCK_RESET14), TEAM_CLOCK_RESET14_CTRL, team_1_clock_value); } else if(TEAM_PAUSE_CLOCK == 0) { reset_team_clock_to_14_and_start(digitalRead(TEAM_CLOCK_RESET14), TEAM_CLOCK_RESET14_CTRL, team_2_clock_value); } display_data(display_3, team_1_clock_value%10, (team_1_clock_value/10)%10, (team_1_clock_value/100)%10, team_1_clock_value/1000); display_data(display_7, team_2_clock_value%10, (team_2_clock_value/10)%10, (team_2_clock_value/100)%10, team_2_clock_value/1000); //////////////////////////////////////GAME CLOCK///////////////////////////////////////////////////////// display_game_clock(digitalRead(GAME_CLOCK_LOAD), CTRL_LOAD, digitalRead(GAME_CLOCK_RESET), CTRL_RESET, digitalRead(GAME_CLOCK_PAUSE), CTRL_PAUSE, GAME_CLOCK_SEC, GAME_CLOCK_MIN, PAUSE_CLOCK, CLOCK_ONE_SECOND_COUNT); display_data(display_9, GAME_CLOCK_SEC%10, (GAME_CLOCK_SEC/10)%10, GAME_CLOCK_MIN%10, (GAME_CLOCK_MIN/10)%10); if(CLOCK_ONE_SECOND_COUNT ==40 ) { CLOCK_ONE_SECOND_COUNT = 0; } delay(10); }