#include #include LiquidCrystal lcd1(8,6,10,11,12,13); LiquidCrystal lcd2(8,9,10,11,12,13); const int player1Pin = A0; const int player2Pin = A1; void setup() { Serial.begin(9600); lcd1.begin(16, 2); lcd1.setCursor(0, 0); lcd2.begin(16, 2); lcd2.setCursor(0, 0); pinMode(player1Pin, INPUT_PULLUP); pinMode(player2Pin, INPUT_PULLUP); UpdatePlayer1Time(); UpdatePlayer2Time(); } double player1Time = 300000; int player1Minutes = 0; int player1Seconds = 0; double player1LastCheck = millis(); double player2Time = 300000; int player2Minutes = 0; int player2Seconds = 0; double player2LastCheck = millis(); bool isPlayer1Turn = true; bool gameOver = false; int player1ButtonState = 0; int player2ButtonState = 0; void loop() { //Check the status of the buttons attached to this project. player1ButtonState = digitalRead(player1Pin); player2ButtonState = digitalRead(player2Pin); //If both buttons are pressed, reset the game. if (player1ButtonState == LOW && player2ButtonState == LOW) { RestartGame(); } //If the game is over, start the loop again. if (gameOver) { return; } //If the player 1 button is pressed, switch to player one's turn. if (player1ButtonState == LOW) { StartPlayer1Turn(); } //If the player 2 button is pressed, switch to player two's turn. if (player2ButtonState == LOW) { StartPlayer2Turn(); } //Whichever player is currently active--update their clock. if (isPlayer1Turn) { UpdatePlayer1Time(); } else { UpdatePlayer2Time(); } } void UpdatePlayer1Time() { //Tell the LCD screen that we're going to be writing on the first line, where player one's time is displayed. lcd1.setCursor(0, 0); //Update the variable that contains the number of milliseconds left on player one's timer. // millis() is a built-in function that returns the number of milliseconds that have elapsed //since the Arduino was turned on. We check how many milliseconds have elapsed since last time //we ran this function. player1Time -= ((millis() - player1LastCheck)); player1LastCheck = millis(); //If the player is out of time, they lose and the game is over. if (player1Time <= 0) { gameOver = true; lcd1.print("LOSE"); return; } //Separate out the minutes and seconds from the time value (which is in milliseconds). player1Minutes = floor(player1Time / 60000); player1Seconds = floor(player1Time / 1000) - player1Minutes * 60; //Print the minutes, then a colon, then the seconds. lcd1.print(player1Minutes); lcd1.print(":"); if (player1Seconds < 10) { lcd1.print(0); } lcd1.print(player1Seconds); } void UpdatePlayer2Time() { lcd2.setCursor(0, 1); player2Time -= ((millis() - player2LastCheck)); if (player2Time <= 0) { gameOver = true; lcd2.print("LOSE"); return; } player2LastCheck = millis(); player2Minutes = floor(player2Time / 60000); player2Seconds = floor(player2Time / 1000) - player2Minutes * 60; lcd2.print(player2Minutes); lcd2.print(":"); if (player2Seconds < 10) { lcd2.print(0); } lcd2.print(player2Seconds); } void StartPlayer1Turn () { if (isPlayer1Turn) { return; } isPlayer1Turn = true; player1LastCheck = millis(); } void StartPlayer2Turn () { if (!isPlayer1Turn) { return; } isPlayer1Turn = false; player2LastCheck = millis(); } void RestartGame() { StartPlayer1Turn(); player1Time = 300000; player2Time = 300000; gameOver = false; UpdatePlayer1Time(); UpdatePlayer2Time(); }