Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include <Adafruit_MCP23017.h>
- #include <Adafruit_RGBLCDShield.h>
- Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
- #define RED 0x1
- #define YELLOW 0x3
- #define GREEN 0x2
- #define TEAL 0x6
- #define BLUE 0x4
- #define VIOLET 0x5
- #define WHITE 0x7
- const int player1Pin = 6;
- const int player2Pin = 7;
- void setup() {
- Serial.begin(9600);
- lcd.begin(16, 2);
- lcd.setBacklight(WHITE);
- lcd.setCursor(0, 0);
- pinMode(player1Pin, INPUT);
- pinMode(player2Pin, INPUT);
- digitalWrite(player1Pin, HIGH);
- digitalWrite(player2Pin, HIGH);
- 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.
- lcd.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;
- lcd.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.
- lcd.print(player1Minutes);
- lcd.print(":");
- if (player1Seconds < 10) {
- lcd.print(0);
- }
- lcd.print(player1Seconds);
- }
- void UpdatePlayer2Time() {
- lcd.setCursor(0, 1);
- player2Time -= ((millis() - player2LastCheck));
- if (player2Time <= 0) {
- gameOver = true;
- lcd.print("LOSE");
- return;
- }
- player2LastCheck = millis();
- player2Minutes = floor(player2Time / 60000);
- player2Seconds = floor(player2Time / 1000) - player2Minutes * 60;
- lcd.print(player2Minutes);
- lcd.print(":");
- if (player2Seconds < 10) {
- lcd.print(0);
- }
- lcd.print(player2Seconds);
- }
- void StartPlayer1Turn () {
- if (isPlayer1Turn) {
- return;
- }
- lcd.setBacklight(RED);
- isPlayer1Turn = true;
- player1LastCheck = millis();
- }
- void StartPlayer2Turn () {
- if (!isPlayer1Turn) {
- return;
- }
- lcd.setBacklight(TEAL);
- isPlayer1Turn = false;
- player2LastCheck = millis();
- }
- void RestartGame() {
- StartPlayer1Turn();
- player1Time = 300000;
- player2Time = 300000;
- gameOver = false;
- UpdatePlayer1Time();
- UpdatePlayer2Time();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement