Advertisement
Guest User

timer_clock_code

a guest
Jul 9th, 2013
4,205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <Adafruit_MCP23017.h>
  3. #include <Adafruit_RGBLCDShield.h>
  4.  
  5. Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
  6.  
  7. #define RED 0x1
  8. #define YELLOW 0x3
  9. #define GREEN 0x2
  10. #define TEAL 0x6
  11. #define BLUE 0x4
  12. #define VIOLET 0x5
  13. #define WHITE 0x7
  14.  
  15. const int player1Pin = 6;
  16. const int player2Pin = 7;
  17.  
  18. void setup() {
  19.  
  20. Serial.begin(9600);
  21. lcd.begin(16, 2);
  22. lcd.setBacklight(WHITE);
  23. lcd.setCursor(0, 0);
  24. pinMode(player1Pin, INPUT);
  25. pinMode(player2Pin, INPUT);
  26. digitalWrite(player1Pin, HIGH);
  27. digitalWrite(player2Pin, HIGH);
  28. UpdatePlayer1Time();
  29. UpdatePlayer2Time();
  30.  
  31. }
  32.  
  33. double player1Time = 300000;
  34. int player1Minutes = 0;
  35. int player1Seconds = 0;
  36. double player1LastCheck = millis();
  37. double player2Time = 300000;
  38. int player2Minutes = 0;
  39. int player2Seconds = 0;
  40. double player2LastCheck = millis();
  41. bool isPlayer1Turn = true;
  42. bool gameOver = false;
  43. int player1ButtonState = 0;
  44. int player2ButtonState = 0;
  45.  
  46.  
  47. void loop() {
  48.  
  49. //Check the status of the buttons attached to this project.
  50. player1ButtonState = digitalRead(player1Pin);
  51. player2ButtonState = digitalRead(player2Pin);
  52.  
  53. //If both buttons are pressed, reset the game.
  54. if (player1ButtonState == LOW && player2ButtonState == LOW) {
  55. RestartGame();
  56. }
  57.  
  58. //If the game is over, start the loop again.
  59. if (gameOver) {
  60. return;
  61. }
  62.  
  63. //If the player 1 button is pressed, switch to player one's turn.
  64. if (player1ButtonState == LOW) {
  65. StartPlayer1Turn();
  66. }
  67.  
  68. //If the player 2 button is pressed, switch to player two's turn.
  69. if (player2ButtonState == LOW) {
  70. StartPlayer2Turn();
  71. }
  72.  
  73. //Whichever player is currently active--update their clock.
  74. if (isPlayer1Turn) {
  75. UpdatePlayer1Time();
  76. } else {
  77. UpdatePlayer2Time();
  78. }
  79. }
  80.  
  81. void UpdatePlayer1Time() {
  82. //Tell the LCD screen that we're going to be writing on the first line, where player one's time is displayed.
  83. lcd.setCursor(0, 0);
  84. //Update the variable that contains the number of milliseconds left on player one's timer.
  85. // millis() is a built-in function that returns the number of milliseconds that have elapsed
  86. //since the Arduino was turned on. We check how many milliseconds have elapsed since last time
  87. //we ran this function.
  88. player1Time -= ((millis() - player1LastCheck));
  89. player1LastCheck = millis();
  90.  
  91. //If the player is out of time, they lose and the game is over.
  92. if (player1Time <= 0) {
  93. gameOver = true;
  94. lcd.print("LOSE");
  95. return;
  96. }
  97.  
  98. //Separate out the minutes and seconds from the time value (which is in milliseconds).
  99. player1Minutes = floor(player1Time / 60000);
  100. player1Seconds = floor(player1Time / 1000) - player1Minutes * 60;
  101.  
  102. //Print the minutes, then a colon, then the seconds.
  103. lcd.print(player1Minutes);
  104. lcd.print(":");
  105. if (player1Seconds < 10) {
  106. lcd.print(0);
  107. }
  108. lcd.print(player1Seconds);
  109. }
  110.  
  111. void UpdatePlayer2Time() {
  112. lcd.setCursor(0, 1);
  113. player2Time -= ((millis() - player2LastCheck));
  114. if (player2Time <= 0) {
  115. gameOver = true;
  116. lcd.print("LOSE");
  117. return;
  118. }
  119. player2LastCheck = millis();
  120. player2Minutes = floor(player2Time / 60000);
  121. player2Seconds = floor(player2Time / 1000) - player2Minutes * 60;
  122. lcd.print(player2Minutes);
  123. lcd.print(":");
  124. if (player2Seconds < 10) {
  125. lcd.print(0);
  126. }
  127. lcd.print(player2Seconds);
  128. }
  129.  
  130. void StartPlayer1Turn () {
  131. if (isPlayer1Turn) {
  132. return;
  133. }
  134. lcd.setBacklight(RED);
  135. isPlayer1Turn = true;
  136. player1LastCheck = millis();
  137. }
  138.  
  139. void StartPlayer2Turn () {
  140. if (!isPlayer1Turn) {
  141. return;
  142. }
  143. lcd.setBacklight(TEAL);
  144. isPlayer1Turn = false;
  145. player2LastCheck = millis();
  146. }
  147.  
  148. void RestartGame() {
  149. StartPlayer1Turn();
  150. player1Time = 300000;
  151. player2Time = 300000;
  152. gameOver = false;
  153. UpdatePlayer1Time();
  154. UpdatePlayer2Time();
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement