Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. #define startSwitch A0
  2. #define alarm 8
  3.  
  4. unsigned long startTime, buttonEvent1, buttonEvent2, playerOne, playerTwo, pwr;
  5. int switchState = 0;
  6. int startInterval = 0;
  7. int press1 = 0;
  8. int press2 = 0;
  9. int dis1 = 0;
  10. int dis2 = 0;
  11. int disQ = 0;
  12.  
  13. #include <LiquidCrystal.h>
  14. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
  15.  
  16. void setup() {
  17. lcd.begin(16, 2);
  18. pinMode (2,OUTPUT);
  19. pinMode(3,OUTPUT);
  20. pinMode (8, OUTPUT);
  21. pinMode (9,INPUT);
  22. pinMode (10, INPUT);
  23. Serial.begin(9600);
  24. Serial.println("criticalGame2");
  25. }
  26.  
  27. void loop() {
  28.  
  29. pwr = millis();
  30. int gameStarted = 0;
  31.  
  32.  
  33. digitalWrite(8,0);
  34. lcd.clear();
  35. lcd.noDisplay();
  36. switchState = analogRead(startSwitch);
  37.  
  38. Serial.print("magnet: ");
  39. Serial.println(switchState);
  40.  
  41. // run the program
  42. if (switchState > 17) {
  43.  
  44. startTime = millis();
  45. Serial.println(startTime - pwr);
  46. startInterval = random(1000, 7000);
  47.  
  48. Serial.print("timeToStart: ");
  49. Serial.println(startInterval);
  50.  
  51. lcd.display();
  52. lcd.print("Get Ready!");
  53. lcd.print("");
  54.  
  55. // Game is ready, loop until some player presses a button
  56. while (press1 < 1001 && press2 < 1001) {
  57.  
  58. // When the start interval is up, say GO!
  59. if (startTime -pwr >= startInterval && gameStarted == 0) {
  60. lcd.clear();
  61. lcd.print("Go!");
  62. digitalWrite(8,1);
  63. gameStarted = 1;
  64. delay(2000);
  65. lcd.clear();
  66. }
  67.  
  68. // Read to see if either player pressed a button
  69. press1 = analogRead(A1);
  70. press2 = analogRead(A2);
  71.  
  72. Serial.print(press1);
  73. Serial.print(": ");
  74. Serial.println(press2);
  75.  
  76. if (press1 > 1000) {
  77. // player 1 pressed the button before the interval elapsed - CHEATER
  78. if (gameStarted == 0) {
  79. lcd.print("Player 1");
  80. lcd.setCursor(0,1);
  81. lcd.print("Disqualifed!");
  82. delay(2000);
  83. lcd.clear();
  84. }
  85. else {
  86. // print player 1 wins and beep/light
  87. buttonEvent1 = millis();
  88. playerOne = (buttonEvent1 - startTime);
  89. Serial.println("player1 wins");
  90. lcd.clear();
  91. digitalWrite(8,0);
  92. lcd.setCursor(1,0);
  93. lcd.print("Player 1 wins!");
  94. delay(2000);
  95. lcd.clear();
  96. lcd.setCursor(6,0);
  97. lcd.print(playerOne);
  98. lcd.setCursor(2,1);
  99. lcd.print("Milliseconds");
  100. delay(5000);
  101. }
  102. }
  103.  
  104. if (press2 > 1000) {
  105. // player 2 pressed the button before the interval elapsed - CHEATER
  106. if (gameStarted == 1) {
  107. lcd.print("Player 2");
  108. lcd.setCursor(0,1);
  109. lcd.print("Disqualifed!");
  110. delay(2000);
  111. lcd.clear();
  112. }
  113. else {
  114. // print player2 wins and beep/light
  115. buttonEvent2 = millis();
  116. playerTwo = (buttonEvent2 - startTime);
  117. Serial.println("player2 wins");
  118. lcd.clear();
  119. digitalWrite(8,0);
  120. lcd.setCursor(1,0);
  121. lcd.print("Player 2 wins!");
  122. delay(2000);
  123. lcd.clear();
  124. lcd.setCursor(6,0);
  125. lcd.print(playerTwo);
  126. lcd.setCursor(2,1);
  127. lcd.print("Milliseconds");
  128. delay(5000);
  129. }
  130. }
  131. }
  132.  
  133. }
  134. // If game not started yet, loop again, at 40hz
  135. else {
  136. delay(25);
  137. }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement