/* File name : ExecutiveGolf9.ino 9/4/2020 - Start Executive Par 3 Code 9/8/2020 - it worked, tested over the weekend, cleaning up some things displayed on LCD 9/9/2020 - Added function to control scoring LEDs and game start 9/10/2020 - Didn't work with analog pins - maybe because declared variable as interger. Worked on digital pins Will use mega arduino for more pins 9/10/2020 - added button for 1 vs. 2 player game. Got rid of manual inning button left from BagsBaseball =========== means new today - tried just the "Ace" hole and the code seemed to work 9/11/2020 - converted all scoring to 2 player option 9/15/2020 - add 2 player LED when button pushed - single player game is going on 1 too many turns 9/16/2020 - didn't work, single player game ending at 8 9/17/2020 - changed last if statement to 20 - it worked 10/2/2020 - add Double Bogey button when ball in net or on "water" or off playing surface. Score of 5. - Changed ace and birdie to see if displays sync better 10/6/2020 - Cleaned up some code for 1-player game */ #include #include #include "Adafruit_LEDBackpack.h" Adafruit_7segment matrix1 = Adafruit_7segment(); Adafruit_7segment matrix2 = Adafruit_7segment(); #include #include LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); #define SENSORPIN1 9 #define SENSORPIN2 10 #define SENSORPIN3 11 #define SENSORPIN4 12 #define SENSORPIN5 35 //---Add Double Bogey button #define BUTTONPINreset A0 //---Reset, cancel, new game #define BUTTONPINatbat A1 //---Now 1 vs 2 player int sensorState1 = 0; int sensorState2 = 0; int sensorState3 = 0; int sensorState4 = 0; int sensorState5 = 0; //---Add Double Bogey button int sensorStatereset = 0; int sensorStateatbat = 0; int totalscore1; int totalscore2; int atbat; int inn; int homeaway; int noplayer; //========= added for 1 vs. 2 player game //============================Large 7-segment================================= byte seven_seg_digits[10][7] = { { 1,1,1,1,1,1,0 }, // = 0 { 0,1,1,0,0,0,0 }, // = 1 { 1,1,0,1,1,0,1 }, // = 2 { 1,1,1,1,0,0,1 }, // = 3 { 0,1,1,0,0,1,1 }, // = 4 { 1,0,1,1,0,1,1 }, // = 5 { 1,0,1,1,1,1,1 }, // = 6 { 1,1,1,0,0,0,0 }, // = 7 { 1,1,1,1,1,1,1 }, // = 8 { 1,1,1,0,0,1,1 } // = 9 }; void sevenSegWrite(byte digit) { byte pin = 2; for (byte segCount = 0; segCount < 7; ++segCount) { digitalWrite(pin, seven_seg_digits[digit][segCount]); ++pin; } } //======================================================================== //==============================Function to turn on/off score LEDs & Start int controlscoreled(int pinnum){ digitalWrite(22, LOW); // -- Ace digitalWrite(24, LOW); // -- Birdie digitalWrite(26, LOW); // -- Par digitalWrite(28, LOW); // -- Bogey digitalWrite(13, LOW); // -- Green light - start new game and keep on // -- Once 9 holes it goes off - game over digitalWrite(pinnum, HIGH); } //======================================================================== void setup() { //====================Set pins for Single 7-segment number pinMode(2, OUTPUT); pinMode(3, OUTPUT); pinMode(4, OUTPUT); pinMode(5, OUTPUT); pinMode(6, OUTPUT); pinMode(7, OUTPUT); pinMode(8, OUTPUT); //============================================ //======================= Hole Value LED Light and Start=== //*****Need to fill in new pin numbers when get Mega board pinMode(22, OUTPUT); //--Ace pinMode(24, OUTPUT); //--Birdie pinMode(26, OUTPUT); //--Par pinMode(28, OUTPUT); //--Bogey pinMode(13, OUTPUT); //=== New Game LED pinMode(30, OUTPUT); //=== 2-player indicator, 9/15/2020 //========================================================= pinMode(SENSORPIN1, INPUT_PULLUP); pinMode(SENSORPIN2, INPUT_PULLUP); pinMode(SENSORPIN3, INPUT_PULLUP); pinMode(SENSORPIN4, INPUT_PULLUP); pinMode(SENSORPIN5, INPUT_PULLUP); //---Add Double Bogey button pinMode(BUTTONPINreset, INPUT_PULLUP); pinMode(BUTTONPINatbat, INPUT_PULLUP); digitalWrite(SENSORPIN1, HIGH); // turn on the pullups digitalWrite(SENSORPIN2, HIGH); digitalWrite(SENSORPIN3, HIGH); digitalWrite(SENSORPIN4, HIGH); matrix1.begin(0x70); matrix1.println(0,DEC); matrix1.writeDisplay(); matrix2.begin(0x72); matrix2.println(0,DEC); matrix2.writeDisplay(); matrix2.setBrightness(1); delay(10); totalscore1 = 0; //-- Player 1 totalscore2 = 0; //-- Player 2 atbat = 2; //-- what team is at bat divide by 2 inn = 1; //-- inning 7-segment display homeaway = 0; //-- Start with Player 1 up noplayer = 2; //========== One player game lcd.begin(20,4); lcd.setCursor(3,0); lcd.print("Par 3 Golf"); lcd.setCursor(1,2); lcd.print("Player 1 Starts"); //======================================== 7-segment single digit==== sevenSegWrite(1); // --------------Start at Hole 1 //=================================================================== controlscoreled(13); } void loop() //**************************************************Main Loop { sensorState1 = digitalRead(SENSORPIN1); //-- read the state value sensorState2 = digitalRead(SENSORPIN2); sensorState3 = digitalRead(SENSORPIN3); sensorState4 = digitalRead(SENSORPIN4); sensorState5 = digitalRead(SENSORPIN5); //--Added Double Bogey button sensorStatereset = digitalRead(BUTTONPINreset); sensorStateatbat = digitalRead(BUTTONPINatbat); if (sensorStatereset == LOW) //-- check if button is pushed { delay(300); totalscore1 = 0; //-- Player 1 Team totalscore2 = 0; //-- Player 2 Team atbat = 2; //-- hole on and what player up inn = 1; //-- Hole - 7-segment display homeaway = 0; //-- Start with Player 1 noplayer = 2; //========Start one player game lcd.begin(20,4); lcd.setCursor(3,0); lcd.print("Par 3 Golf"); lcd.setCursor(1,2); lcd.print("Player 1 Starts"); sevenSegWrite(1); matrix1.begin(0x70); matrix1.println(0,DEC); matrix1.writeDisplay(); matrix2.begin(0x72); matrix2.println(0,DEC); matrix2.writeDisplay(); matrix2.setBrightness(0); controlscoreled(13); digitalWrite(30, LOW); //===== Added 9/15/2020 delay(10); } if (sensorStateatbat == LOW) //-- check if button is pushed - 2 player { delay(300); if (atbat == 2) { //=Be sure 1player game hasn't already started noplayer = 1; //==================== Means two player came matrix2.setBrightness(15); //== 2 player scoreboard brightens up digitalWrite(30, HIGH); //=============Added 9/15/2020-2 player LED } } if (sensorState1 == LOW && atbat < 20) { // check if Ace sensor beam is broken //=====Added upper limit of trys delay(100); controlscoreled(22); delay(150); if (homeaway == 0) { totalscore1 = totalscore1 + 1; //----Player 1 if (noplayer == 2) { //--means 1-player game matrix2.setBrightness(0); matrix1.setBrightness(15); matrix1.print(totalscore1); matrix1.writeDisplay(); delay(1500); } else { matrix1.print(totalscore1); matrix1.writeDisplay(); delay(1500); matrix1.setBrightness(0); matrix2.setBrightness(15); } lcd.clear(); lcd.setCursor(4,0); lcd.print("Player 1"); lcd.setCursor(4,1); lcd.print("Total Score: "); lcd.print(totalscore1); lcd.setCursor(1,2); lcd.print(atbat); lcd.setCursor(10,2); lcd.print(homeaway); lcd.setCursor(0,3); lcd.print("Through Hole: "); lcd.print(inn); } else //----homeaway = 1 { totalscore2 = totalscore2 + 1; //----Player 2 matrix2.print(totalscore2); matrix2.writeDisplay(); delay(1500); matrix1.setBrightness(15); matrix2.setBrightness(0); lcd.clear(); lcd.setCursor(4,0); lcd.print("Player 2"); lcd.setCursor(4,1); lcd.print("Total Score: "); lcd.print(totalscore2); lcd.setCursor(1,2); lcd.print(atbat); lcd.setCursor(10,2); lcd.print(homeaway); lcd.setCursor(0,3); lcd.print("Through Hole: "); lcd.print(inn); } atbat = atbat + noplayer; homeaway = atbat % 2; inn = (atbat/2); sevenSegWrite(inn); } if (sensorState2 == LOW && atbat < 20) { // check if Birdie sensor beam is broken delay(100); controlscoreled(24); delay(150); if (homeaway == 0) { totalscore1 = totalscore1 + 2; //----Player 1 if (noplayer == 2) { //--means 1-player game matrix2.setBrightness(0); matrix1.setBrightness(15); matrix1.print(totalscore1); matrix1.writeDisplay(); delay(1500); } else { matrix1.print(totalscore1); matrix1.writeDisplay(); delay(1500); matrix1.setBrightness(0); matrix2.setBrightness(15); } lcd.clear(); lcd.setCursor(4,0); lcd.print("Player 1"); lcd.setCursor(4,1); lcd.print("Total Score: "); lcd.print(totalscore1); lcd.setCursor(1,2); lcd.print(atbat); lcd.setCursor(10,2); lcd.print(homeaway); lcd.setCursor(0,3); lcd.print("Through Hole: "); lcd.print(inn); } else { totalscore2 = totalscore2 + 2; //----Player 2 matrix2.print(totalscore2); matrix2.writeDisplay(); delay(1500); matrix1.setBrightness(15); matrix2.setBrightness(0); lcd.clear(); lcd.setCursor(4,0); lcd.print("Player 2"); lcd.setCursor(4,1); lcd.print("Total Score: "); lcd.print(totalscore2); lcd.setCursor(1,2); lcd.print(atbat); lcd.setCursor(10,2); lcd.print(homeaway); lcd.setCursor(0,3); lcd.print("Through Hole: "); lcd.print(inn); } atbat = atbat + noplayer; homeaway = atbat % 2; inn = (atbat/2); sevenSegWrite(inn); } if (sensorState3 == LOW && atbat < 20) { // check if Par sensor beam is broken delay(100); controlscoreled(26); delay(150); if (homeaway == 0) { totalscore1 = totalscore1 + 3; //----Player 1 if (noplayer == 2) { //--means 1-player game matrix2.setBrightness(0); matrix1.setBrightness(15); matrix1.print(totalscore1); matrix1.writeDisplay(); delay(1500); } else { matrix1.print(totalscore1); matrix1.writeDisplay(); delay(1500); matrix1.setBrightness(0); matrix2.setBrightness(15); } lcd.clear(); lcd.setCursor(4,0); lcd.print("Player 1"); lcd.setCursor(4,1); lcd.print("Total Score: "); lcd.print(totalscore1); lcd.setCursor(1,2); lcd.print(atbat); lcd.setCursor(10,2); lcd.print(homeaway); lcd.setCursor(0,3); lcd.print("End of Hole: "); lcd.print(inn); } else { totalscore2 = totalscore2 + 3; //----Player 2 matrix2.print(totalscore2); matrix2.writeDisplay(); delay(1500); matrix1.setBrightness(15); matrix2.setBrightness(0); lcd.clear(); lcd.setCursor(4,0); lcd.print("Player 2"); lcd.setCursor(4,1); lcd.print("Total Score: "); lcd.print(totalscore2); lcd.setCursor(1,2); lcd.print(atbat); lcd.setCursor(10,2); lcd.print(homeaway); lcd.setCursor(0,3); lcd.print("End of Hole: "); lcd.print(inn); } atbat = atbat + noplayer; homeaway = atbat % 2; inn = (atbat/2); sevenSegWrite(inn); } if (sensorState4 == LOW && atbat < 20) { // check if Bogie sensor beam is broken delay(100); controlscoreled(28); delay(150); if (homeaway == 0) { totalscore1 = totalscore1 + 4; //----Player 1 if (noplayer == 2) { //--means 1-player game matrix2.setBrightness(0); matrix1.setBrightness(15); matrix1.print(totalscore1); matrix1.writeDisplay(); delay(1500); } else { matrix1.print(totalscore1); matrix1.writeDisplay(); delay(1500); matrix1.setBrightness(0); matrix2.setBrightness(15); } lcd.clear(); lcd.setCursor(4,0); lcd.print("Player 1"); lcd.setCursor(4,1); lcd.print("Total Score: "); lcd.print(totalscore1); lcd.setCursor(1,2); lcd.print(atbat); lcd.setCursor(10,2); lcd.print(homeaway); lcd.setCursor(0,3); lcd.print("End of Hole: "); lcd.print(inn); } else { totalscore2 = totalscore2 + 4; //----Player 2 matrix2.print(totalscore2); matrix2.writeDisplay(); delay(1500); matrix1.setBrightness(15); matrix2.setBrightness(0); lcd.clear(); lcd.setCursor(4,0); lcd.print("Player 2"); lcd.setCursor(4,1); lcd.print("Total Score: "); lcd.print(totalscore2); lcd.setCursor(1,2); lcd.print(atbat); lcd.setCursor(10,2); lcd.print(homeaway); lcd.setCursor(0,3); lcd.print("End of Hole: "); lcd.print(inn); } atbat = atbat + noplayer; homeaway = atbat % 2; inn = (atbat/2); sevenSegWrite(inn); } //=============================Added Double Bogey button 10/2/2020===== if (sensorState5 == LOW && atbat < 20) { // check if Double Bogey button is pressed delay(100); controlscoreled(34); //===Dummy light if (homeaway == 0) { totalscore1 = totalscore1 + 5; //----Player 1 if (noplayer == 2) { //--means 1-player game matrix2.setBrightness(0); matrix1.setBrightness(15); matrix1.print(totalscore1); matrix1.writeDisplay(); delay(1500); } else { matrix1.print(totalscore1); matrix1.writeDisplay(); delay(1500); matrix1.setBrightness(0); matrix2.setBrightness(15); } lcd.clear(); lcd.setCursor(4,0); lcd.print("Player 1"); lcd.setCursor(4,1); lcd.print("Total Score: "); lcd.print(totalscore1); lcd.setCursor(1,2); lcd.print(atbat); lcd.setCursor(10,2); lcd.print(homeaway); lcd.setCursor(0,3); lcd.print("Through Hole: "); lcd.print(inn); } else { totalscore2 = totalscore2 + 5; //----Player 2 matrix2.print(totalscore2); matrix2.writeDisplay(); delay(1500); matrix1.setBrightness(15); matrix2.setBrightness(0); lcd.clear(); lcd.setCursor(4,0); lcd.print("Player 2"); lcd.setCursor(4,1); lcd.print("Total Score: "); lcd.print(totalscore2); lcd.setCursor(1,2); lcd.print(atbat); lcd.setCursor(10,2); lcd.print(homeaway); lcd.setCursor(0,3); lcd.print("Through Hole: "); lcd.print(inn); } atbat = atbat + noplayer; homeaway = atbat % 2; inn = (atbat/2); sevenSegWrite(inn); } //================================================================== if (atbat == 20) { //==Game over - 9 Holes // account for 18 & 19 totals controlscoreled(34); //===Dummy light lcd.clear(); lcd.setCursor(4,1); lcd.print("Game Over!"); lcd.setCursor(1,3); lcd.print("Reset for new game"); matrix1.setBrightness(0); matrix2.setBrightness(0); sevenSegWrite(9); } //==================================================================== } //******************************************************End of Loop Function