document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #include <LiquidCrystal.h>
  2. #include <Wire.h>
  3. #include <ArduinoNunchuk.h>
  4.  
  5. ArduinoNunchuk nunchuk = ArduinoNunchuk();
  6. LiquidCrystal lcd(2,3,4,5,6,7);
  7.  
  8. int posX = 90;
  9. int x = 0;
  10. int x2 = 0;
  11. int prevx = 0;
  12. int prevx2 = 0;
  13. int prevPosX = 0;
  14. int zState = 0;
  15. int zLast = 0;
  16. unsigned long previousMillis = 0;
  17. unsigned long startMillis = 0;
  18. unsigned long time = 0;
  19. unsigned long shotTime = 0;
  20. long interval = 500;
  21. long shotDelay = 1000;
  22. int shot = 9;
  23. int i = 0;
  24. int j = 0;
  25. int score = 0;
  26. const int buzzer = 9;
  27.  
  28. byte enemy[8] = {
  29. B01110,
  30. B10001,
  31. B10101,
  32. B10001,
  33. B10101,
  34. B11011,
  35. B10101,
  36. };
  37.  
  38. byte gun[8] = {
  39. B00000,
  40. B00000,
  41. B00100,
  42. B01110,
  43. B01010,
  44. B01110,
  45. B11111,
  46. };
  47.  
  48. byte full[8] = {
  49. B11111,
  50. B11111,
  51. B11111,
  52. B11111,
  53. B11111,
  54. B11111,
  55. B11111,
  56. };
  57.  
  58. byte empty[8] = {
  59. B00000,
  60. B00000,
  61. B00000,
  62. B00000,
  63. B00000,
  64. B00000,
  65. B00000,
  66. };
  67.  
  68. void setup(){
  69. Serial.begin(9600);
  70. lcd.begin(16,2);
  71. nunchuk.init();
  72. lcd.createChar(5, enemy);
  73. lcd.createChar(1, gun);
  74. lcd.createChar(2, full);
  75. lcd.createChar(3, empty);
  76. }
  77.  
  78. void loop(){
  79. nunchuk.update();
  80. unsigned long currentMillis = millis();
  81.  
  82. x = constrain(x, 0, 14);
  83. x2 = constrain(x2, 0, 14);
  84.  
  85. lcd.setCursor(x,0);
  86. lcd.write(5);
  87. lcd.setCursor(x2,1);
  88. lcd.write(1);
  89. lcd.setCursor(15, 0);
  90. lcd.print(score);
  91. lcd.setCursor(15, 1);
  92. lcd.print(shot);
  93.  
  94. zState = nunchuk.zButton;
  95.  
  96. posX = nunchuk.analogX;
  97.  
  98. if(zState == HIGH && x == x2 && currentMillis < shotTime + shotDelay){
  99. lcd.setCursor(x,0);
  100. lcd.write(2);
  101. tone(buzzer, 100, 90);
  102. delay(333);
  103. score++;
  104. x = random(0,15);
  105. }
  106.  
  107. if(posX < 60 && prevPosX > 60){
  108. x2--;
  109. }
  110. if(posX > 200 && prevPosX < 200){
  111. x2++;
  112. }
  113.  
  114. if(currentMillis - previousMillis > interval){
  115. i = random(0,2);
  116. if(i == 0){
  117. x--;
  118. }
  119. if(i == 1){
  120. x++;
  121. }
  122. previousMillis = currentMillis;
  123. }
  124.  
  125. if(zState != zLast && zState == HIGH){
  126. shotTime = currentMillis;
  127. tone(buzzer, 1319, 50);
  128. if(x != x2){
  129. shot--;
  130. }
  131. }
  132. zLast = zState;
  133.  
  134. if(shot == 0){
  135. shot = 9;
  136. score--;
  137. }
  138.  
  139. if(score == 9){
  140. time = currentMillis - startMillis;
  141. lcd.setCursor(0,0);
  142. lcd.print(" GAME OVER ");
  143. lcd.setCursor(2,1);
  144. lcd.print(time / 1000);
  145. lcd.print(" Seconds ");
  146. delay(5000);
  147. score = 0;
  148. shot = 9;
  149. startMillis = millis();
  150. }
  151.  
  152. if(score < 0){
  153. lcd.setCursor(0,0);
  154. lcd.print(" GAME OVER ");
  155. lcd.setCursor(0,1);
  156. lcd.print(" YOU LOSE! ");
  157. delay(5000);
  158. score = 0;
  159. shot = 9;
  160. startMillis = millis();
  161. }
  162.  
  163. if(x != prevx || x2 != prevx2){
  164. lcd.clear();
  165. }
  166.  
  167. prevx = x;
  168. prevPosX = posX;
  169. prevx2 = x2;
  170. }
');