aschmidt20

Arduino Pong

Jun 23rd, 2013
1,807
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.57 KB | None | 0 0
  1. //1D Pong
  2. //created by Andrew Schmidt (aschmidt20)
  3. //June 21, 2013
  4.  
  5. int ledPins[] = {2,3,4,5,6,7,8,9};
  6. int button1Pin = 10;
  7. int button2Pin = 11;
  8. int button1State, button2State;
  9. String gameInstance = "start";
  10. int player1Score, player2Score = 0;
  11. int delayTime;
  12.  
  13.  
  14. void setup()
  15. {
  16. int index;
  17. for(index = 0; index <= 7; index++)
  18. {
  19. pinMode(ledPins[index], OUTPUT);
  20. }
  21. pinMode(button1Pin, INPUT);
  22. pinMode(button2Pin, INPUT);
  23. delayTime = 100;
  24. }
  25.  
  26. void loop()
  27. { // Different points of the game as String variables
  28. if (gameInstance == "start")
  29. {
  30. leftRight();
  31. }
  32. else if (gameInstance == "player 1 return")
  33. {
  34. delayTime = delayTime - 5; // increases speed of "ball"
  35. rightLeft();
  36. }
  37. else if (gameInstance == "player 2 return")
  38. {
  39. delayTime = delayTime - 5;
  40. leftRight();
  41. }
  42. else if (gameInstance == "player 1 point")
  43. {
  44. player1Score++;
  45. delayTime = 100; // resets delay after every point
  46. showScore();
  47. }
  48. else if (gameInstance == "player 2 point")
  49. {
  50. player2Score++;
  51. delayTime = 100;
  52. showScore();
  53. }
  54.  
  55. }
  56.  
  57. void leftRight() // LEDs light up from Left to Right then check if the button is pressed
  58. {
  59. int index;
  60.  
  61. for(index = 0; index <= 6; index++)
  62. {
  63. digitalWrite(ledPins[index], HIGH);
  64. delay(delayTime);
  65. digitalWrite(ledPins[index], LOW);
  66. }
  67. digitalWrite(ledPins[7], HIGH);
  68.  
  69. button1State = digitalRead(button1Pin);
  70.  
  71. if (button1State == LOW) // sets gameInstance for when it goes through the main Loop
  72. {
  73. gameInstance = "player 1 return";
  74. }
  75. else
  76. {
  77. gameInstance = "player 2 point";
  78. }
  79. digitalWrite(ledPins[7], LOW);
  80. }
  81.  
  82. void rightLeft() // LEDs light up from Right to Left then check if the button is pressed
  83. {
  84. int index;
  85.  
  86. for(index = 7; index >=1; index--)
  87. {
  88. digitalWrite(ledPins[index], HIGH);
  89. delay(delayTime);
  90. digitalWrite(ledPins[index], LOW);
  91. }
  92. digitalWrite(ledPins[0], HIGH);
  93.  
  94. button2State = digitalRead(button2Pin);
  95.  
  96. if (button2State == LOW)
  97. {
  98. gameInstance = "player 2 return";
  99. }
  100. else
  101. {
  102. gameInstance = "player 1 point";
  103. }
  104. digitalWrite(ledPins[0], LOW);
  105. }
  106.  
  107. void showScore()
  108. {
  109. // Show player 1 score
  110. if (player1Score == 1)
  111. {
  112. digitalWrite(ledPins[7], HIGH);
  113. }
  114. else if (player1Score == 2)
  115. {
  116. digitalWrite(ledPins[7], HIGH);
  117. digitalWrite(ledPins[6], HIGH);
  118. }
  119. else if (player1Score == 3)
  120. {
  121. digitalWrite(ledPins[7], HIGH);
  122. digitalWrite(ledPins[6], HIGH);
  123. digitalWrite(ledPins[5], HIGH);
  124. }
  125. else if (player1Score == 4)
  126. {
  127. digitalWrite(ledPins[7], HIGH);
  128. digitalWrite(ledPins[6], HIGH);
  129. digitalWrite(ledPins[5], HIGH);
  130. digitalWrite(ledPins[4], HIGH);
  131. }
  132.  
  133.  
  134. // Show player 2 score
  135. if (player2Score == 1)
  136. {
  137. digitalWrite(ledPins[0], HIGH);
  138. }
  139. else if (player2Score == 2)
  140. {
  141. digitalWrite(ledPins[1], HIGH);
  142. digitalWrite(ledPins[0], HIGH);
  143. }
  144. else if (player2Score == 3)
  145. {
  146. digitalWrite(ledPins[2], HIGH);
  147. digitalWrite(ledPins[1], HIGH);
  148. digitalWrite(ledPins[0], HIGH);
  149. }
  150. else if (player2Score == 4)
  151. {
  152. digitalWrite(ledPins[3], HIGH);
  153. digitalWrite(ledPins[2], HIGH);
  154. digitalWrite(ledPins[1], HIGH);
  155. digitalWrite(ledPins[0], HIGH);
  156. }
  157.  
  158. delay(2000);
  159. ledsOff();
  160.  
  161. if (player1Score == 4) // If the game is over, marquee in direction of winner
  162. {
  163. marqueeRight();
  164. }
  165. else if (player2Score == 4)
  166. {
  167. marqueeLeft();
  168. }
  169.  
  170. gameInstance = "start";
  171. }
  172.  
  173. void ledsOff() // Turns off all LEDs
  174. {
  175. int index;
  176.  
  177. for(index = 0; index <= 7; index++)
  178. {
  179. digitalWrite(ledPins[index], LOW);
  180. }
  181. }
  182.  
  183. void marqueeRight() // Marquee code from Inventor's Kit examples, modified for length and direction
  184. {
  185. int index;
  186. int indez;
  187. int delayTime = 100; // milliseconds to pause between LEDs
  188. // Make this smaller for faster switching
  189.  
  190. // Step through the first four LEDs
  191. // (We'll light up one in the lower 4 and one in the upper 4)
  192. for(indez = 0; indez<= 5; indez++)
  193. {
  194. for(index = 0; index <= 3; index++) // Step from 0 to 3
  195. {
  196. digitalWrite(ledPins[index], HIGH); // Turn a LED on
  197. digitalWrite(ledPins[index+4], HIGH); // Skip four, and turn that LED on
  198. delay(delayTime); // Pause to slow down the sequence
  199. digitalWrite(ledPins[index], LOW); // Turn the LED off
  200. digitalWrite(ledPins[index+4], LOW); // Skip four, and turn that LED off
  201. }
  202. }
  203.  
  204. delay(2500);
  205. gameRestart();
  206. }
  207.  
  208. void marqueeLeft()
  209. {
  210. int index;
  211. int indez;
  212. int delayTime = 100; // milliseconds to pause between LEDs
  213. // Make this smaller for faster switching
  214.  
  215. // Step through the first four LEDs
  216. // (We'll light up one in the lower 4 and one in the upper 4)
  217. for(indez = 0; indez<= 5; indez++)
  218. {
  219. for(index = 3; index >= 0; index--) // Step from 0 to 3
  220. {
  221. digitalWrite(ledPins[index], HIGH); // Turn a LED on
  222. digitalWrite(ledPins[index+4], HIGH); // Skip four, and turn that LED on
  223. delay(delayTime); // Pause to slow down the sequence
  224. digitalWrite(ledPins[index], LOW); // Turn the LED off
  225. digitalWrite(ledPins[index+4], LOW); // Skip four, and turn that LED off
  226. }
  227. }
  228.  
  229. delay(2500);
  230. gameRestart();
  231. }
  232.  
  233. void gameRestart() // Restarts game, variables back to original
  234. {
  235. player1Score = 0;
  236. player2Score = 0;
  237. gameInstance = "start";
  238. delayTime = 100;
  239. }
Advertisement
Add Comment
Please, Sign In to add comment