rjk79

Arduino Game 3

Feb 3rd, 2013
3,421
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Space Invader type of game. 16X2 LCD set up into pins 2-7,
  3. buzzer on pin 9 and wiiNunchuk attached to A4 (data) and A5 (clock).
  4.  
  5. Difficulty select on start to set the rate at which the enemy moves.
  6. WiiNunchuk analog used to move left and right, z button to shoot.
  7. Game is to 10 points after which your time will be displayed.
  8. Misses reduce ammo, if you run out of ammo it costs 1 point to reload,
  9. if your score falls below zero you lose.
  10.  
  11. Much credit to Gabriel Bianconi for creating the ArduinoNunchuk
  12. library, I could have never made this project with out that.
  13. http://www.gabrielbianconi.com/projects/arduinonunchuk/
  14. */
  15.  
  16. #include <LiquidCrystal.h>
  17. #include <Wire.h>
  18. #include <ArduinoNunchuk.h>
  19.  
  20. ArduinoNunchuk nunchuk = ArduinoNunchuk();
  21. LiquidCrystal lcd(2,3,4,5,6,7);
  22.  
  23. int posX = 90;
  24. int x = 0;
  25. int x2 = 7;
  26. int prevx = 0;
  27. int prevx2 = 0;
  28. int prevPosX = 0;
  29. int zState = 0;
  30. int zLast = 0;
  31. unsigned long previousMillis = 0;
  32. unsigned long startMillis = 0;
  33. unsigned long currentMillis = 0;
  34. unsigned long time = 0;
  35. unsigned long shotTime = 0;
  36. long moveDelay;
  37. long moveSpeed;
  38. long shotDelay = 100;
  39. int shot = 9;
  40. int i = 0;
  41. int j = 6;
  42. int score = 0;
  43. const int buzzer = 9;
  44.  
  45. #define NOTE_B0 31
  46. #define NOTE_C1 33
  47. #define NOTE_CS1 35
  48. #define NOTE_D1 37
  49. #define NOTE_DS1 39
  50. #define NOTE_E1 41
  51. #define NOTE_F1 44
  52. #define NOTE_FS1 46
  53. #define NOTE_G1 49
  54. #define NOTE_GS1 52
  55. #define NOTE_A1 55
  56. #define NOTE_AS1 58
  57. #define NOTE_B1 62
  58. #define NOTE_C2 65
  59. #define NOTE_CS2 69
  60. #define NOTE_D2 73
  61. #define NOTE_DS2 78
  62. #define NOTE_E2 82
  63. #define NOTE_F2 87
  64. #define NOTE_FS2 93
  65. #define NOTE_G2 98
  66. #define NOTE_GS2 104
  67. #define NOTE_A2 110
  68. #define NOTE_AS2 117
  69. #define NOTE_B2 123
  70. #define NOTE_C3 131
  71. #define NOTE_CS3 139
  72. #define NOTE_D3 147
  73. #define NOTE_DS3 156
  74. #define NOTE_E3 165
  75. #define NOTE_F3 175
  76. #define NOTE_FS3 185
  77. #define NOTE_G3 196
  78. #define NOTE_GS3 208
  79. #define NOTE_A3 220
  80. #define NOTE_AS3 233
  81. #define NOTE_B3 247
  82. #define NOTE_C4 262
  83. #define NOTE_CS4 277
  84. #define NOTE_D4 294
  85. #define NOTE_DS4 311
  86. #define NOTE_E4 330
  87. #define NOTE_F4 349
  88. #define NOTE_FS4 370
  89. #define NOTE_G4 392
  90. #define NOTE_GS4 415
  91. #define NOTE_A4 440
  92. #define NOTE_AS4 466
  93. #define NOTE_B4 494
  94. #define NOTE_C5 523
  95. #define NOTE_CS5 554
  96. #define NOTE_D5 587
  97. #define NOTE_DS5 622
  98. #define NOTE_E5 659
  99. #define NOTE_F5 698
  100. #define NOTE_FS5 740
  101. #define NOTE_G5 784
  102. #define NOTE_GS5 831
  103. #define NOTE_A5 880
  104. #define NOTE_AS5 932
  105. #define NOTE_B5 988
  106. #define NOTE_C6 1047
  107. #define NOTE_CS6 1109
  108. #define NOTE_D6 1175
  109. #define NOTE_DS6 1245
  110. #define NOTE_E6 1319
  111. #define NOTE_F6 1397
  112. #define NOTE_FS6 1480
  113. #define NOTE_G6 1568
  114. #define NOTE_GS6 1661
  115. #define NOTE_A6 1760
  116. #define NOTE_AS6 1865
  117. #define NOTE_B6 1976
  118. #define NOTE_C7 2093
  119. #define NOTE_CS7 2217
  120. #define NOTE_D7 2349
  121. #define NOTE_DS7 2489
  122. #define NOTE_E7 2637
  123. #define NOTE_F7 2794
  124. #define NOTE_FS7 2960
  125. #define NOTE_G7 3136
  126. #define NOTE_GS7 3322
  127. #define NOTE_A7 3520
  128. #define NOTE_AS7 3729
  129. #define NOTE_B7 3951
  130. #define NOTE_C8 4186
  131. #define NOTE_CS8 4435
  132. #define NOTE_D8 4699
  133. #define NOTE_DS8 4978
  134. #define R1 0
  135.  
  136. int winMelody[] = {NOTE_C4,NOTE_B3,NOTE_G3,NOTE_C4,NOTE_B3,NOTE_E3,R1,NOTE_C3,NOTE_C3,NOTE_G3,NOTE_A6,NOTE_C7};
  137. int winBeats[] = {16,16,16,8,8,16,32,16,16,16,8,8};
  138. int winMaxCount = sizeof(winMelody) / 2;
  139. int lossMelody[] = {NOTE_C8,R1,NOTE_G7,NOTE_C7,NOTE_B7,NOTE_E7,R1,NOTE_C8,R1,NOTE_G7,NOTE_A7,NOTE_C8};
  140. int lossBeats[] = {16,16,16,8,8,16,32,16,16,16,8,8};
  141. int lossMaxCount = sizeof(lossMelody) / 2;
  142. long tempo = 10000;
  143. int pause = 1000;
  144. int restCount = 100;
  145. int tone_ = 0;
  146. int beat = 0;
  147. long duration = 0;
  148.  
  149. byte enemy1[8] = {
  150. B00100,
  151. B01010,
  152. B10001,
  153. B10101,
  154. B10101,
  155. B01010,
  156. B10101,
  157. B10001,
  158. };
  159.  
  160. byte enemy2[8] = {
  161. B00000,
  162. B00100,
  163. B01010,
  164. B10001,
  165. B01110,
  166. B10101,
  167. B00000,
  168. B00000,
  169. };
  170.  
  171. byte enemy3[8] = {
  172. B00000,
  173. B00100,
  174. B01110,
  175. B01010,
  176. B01110,
  177. B10101,
  178. B00000,
  179. B00000,
  180. };
  181.  
  182. byte gun[8] = {
  183. B00000,
  184. B00000,
  185. B00000,
  186. B00100,
  187. B01110,
  188. B01010,
  189. B01010,
  190. B11111,
  191. };
  192.  
  193. byte gunShoot[8] = {
  194. B00100,
  195. B00100,
  196. B01010,
  197. B00100,
  198. B01110,
  199. B01010,
  200. B01010,
  201. B11111,
  202. };
  203.  
  204. byte hitShot[8] = {
  205. B01010,
  206. B00000,
  207. B10001,
  208. B01010,
  209. B01010,
  210. B10001,
  211. B00000,
  212. B01010,
  213. };
  214.  
  215. byte missShot[8] = {
  216. B01110,
  217. B10101,
  218. B10101,
  219. B00100,
  220. B00100,
  221. B00100,
  222. B00100,
  223. B00100,
  224. };
  225.  
  226. byte blank[8] = {
  227. B00000,
  228. B00000,
  229. B00000,
  230. B00000,
  231. B00000,
  232. B00000,
  233. B00000,
  234. B00000,
  235. };
  236.  
  237. void setup(){
  238. lcd.begin(16,2);
  239. nunchuk.init();
  240. lcd.createChar(1, gun);
  241. lcd.createChar(2, gunShoot);
  242. lcd.createChar(3, hitShot);
  243. lcd.createChar(4, missShot);
  244. lcd.createChar(5, blank);
  245. lcd.createChar(6, enemy1);
  246. lcd.createChar(7, enemy2);
  247. lcd.createChar(8, enemy3);
  248. nunchuk.update();
  249. zState = nunchuk.zButton;
  250. while(zState == LOW){
  251. difficulty();
  252. }
  253. lcd.setCursor(x2,1);
  254. lcd.write(2);
  255. lcd.setCursor(0,0);
  256. lcd.print(" ");
  257. lcd.setCursor(x2,0);
  258. lcd.write(3);
  259. delay(250);
  260. lcd.clear();
  261. startMillis = millis();
  262. }
  263.  
  264. void loop(){
  265. if(score <= 4){
  266. moveSpeed = moveDelay;
  267. j = 6;
  268. }
  269. else if( score <= 7){
  270. moveSpeed = moveDelay / 2;
  271. j = 7;
  272. }
  273. else{
  274. moveSpeed = moveDelay / 4;
  275. j = 8;
  276. }
  277. nunchuk.update();
  278. currentMillis = millis();
  279.  
  280. posX = nunchuk.analogX;
  281. if(posX < 60 && prevPosX > 60){
  282. x2--;
  283. }
  284. if(posX > 200 && prevPosX < 200){
  285. x2++;
  286. }
  287. x2 = constrain(x2, 0, 14);
  288.  
  289. if(currentMillis - previousMillis > moveSpeed){
  290. i = random(0,2);
  291. if(i == 0){
  292. x--;
  293. }
  294. if(i == 1){
  295. x++;
  296. }
  297. previousMillis = currentMillis;
  298. }
  299. x = constrain(x, 0, 14);
  300.  
  301. lcd.setCursor(prevx,0);
  302. lcd.write(5);
  303. lcd.setCursor(x,0);
  304. lcd.write(j);
  305. lcd.setCursor(prevx2, 1);
  306. lcd.write(5);
  307. lcd.setCursor(x2,1);
  308. lcd.write(1);
  309. lcd.setCursor(15, 0);
  310. lcd.print(score);
  311. lcd.setCursor(15, 1);
  312. lcd.print(shot);
  313.  
  314. zState = nunchuk.zButton;
  315. if(zState != zLast && zState == HIGH){
  316. shotTime = currentMillis;
  317. lcd.setCursor(x2,1);
  318. lcd.write(2);
  319. if(x == x2 && currentMillis < shotTime + shotDelay){
  320. hit();
  321. }
  322. else{
  323. miss();
  324. }
  325. }
  326. zLast = zState;
  327.  
  328. if(shot == 0){
  329. shot = 9;
  330. score--;
  331. }
  332.  
  333. prevx = x;
  334. prevPosX = posX;
  335. prevx2 = x2;
  336.  
  337. if(score > 9){
  338. time = currentMillis - startMillis;
  339. lcd.setCursor(0,0);
  340. lcd.print(" YOU WIN ");
  341. lcd.setCursor(0,1);
  342. lcd.print(" ");
  343. lcd.print(time / 1000);
  344. lcd.print(" Seconds ");
  345. playWin();
  346. delay(4000);
  347. score = 0;
  348. shot = 9;
  349. startMillis = millis();
  350. lcd.clear();
  351. }
  352.  
  353. if(score < 0){
  354. lcd.setCursor(0,0);
  355. lcd.print(" GAME OVER ");
  356. lcd.setCursor(0,1);
  357. lcd.print(" YOU LOSE! ");
  358. playLoss();
  359. delay(4000);
  360. score = 0;
  361. shot = 9;
  362. startMillis = millis();
  363. lcd.clear();
  364. }
  365. }
  366.  
  367. void hit(){
  368. lcd.setCursor(x,0);
  369. lcd.write(3);
  370. tone(buzzer, 100, 90);
  371. delay(10);
  372. tone(buzzer, 250, 150);
  373. delay(333);
  374. score++;
  375. lcd.setCursor(x,0);
  376. lcd.write(5);
  377. x = random(0,15);
  378. }
  379.  
  380. void miss(){
  381. tone(buzzer, 1000, 90);
  382. delay(10);
  383. tone(buzzer, 1250, 150);
  384. shot--;
  385. lcd.setCursor(x2,0);
  386. lcd.write(4);
  387. delay(50);
  388. lcd.setCursor(x2,0);
  389. lcd.write(5);
  390. }
  391.  
  392. void difficulty(){
  393. nunchuk.update();
  394. x2 = constrain(x2, 0, 15);
  395. lcd.setCursor(0,0);
  396. lcd.print("SLOW FAST");
  397.  
  398. posX = nunchuk.analogX;
  399. if(posX < 60 && prevPosX > 60){
  400. x2--;
  401. }
  402. if(posX > 200 && prevPosX < 200){
  403. x2++;
  404. }
  405. prevPosX = posX;
  406. lcd.setCursor(prevx2,1);
  407. lcd.write(5);
  408. lcd.setCursor(x2,1);
  409. lcd.write(1);
  410.  
  411. prevx2 = x2;
  412. moveDelay = map(x2, 0, 15, 1600, 400);
  413. zState = nunchuk.zButton;
  414. }
  415.  
  416. void playWin(){
  417. for(int i = 0; i < winMaxCount; i++){
  418. tone_ = winMelody[i];
  419. beat = winBeats[i];
  420. duration = beat * tempo;
  421. playTone();
  422. delayMicroseconds(pause);
  423. }
  424. }
  425.  
  426. void playLoss(){
  427. for(int i = 0; i < lossMaxCount; i++){
  428. tone_ = lossMelody[i];
  429. beat = lossBeats[i];
  430. duration = beat * tempo;
  431. playTone();
  432. delayMicroseconds(pause);
  433. }
  434. }
  435.  
  436. void playTone(){
  437. long elapsedTime = 0;
  438. if(tone_ > 0){
  439. while (elapsedTime < duration){
  440. digitalWrite(buzzer, HIGH);
  441. delayMicroseconds(tone_ / 2);
  442. digitalWrite(buzzer, LOW);
  443. delayMicroseconds(tone_ / 2);
  444. elapsedTime += (tone_);
  445. }
  446. }
  447. else{
  448. for(int j = 0; j < restCount; j++){
  449. delayMicroseconds(duration);
  450. }
  451. }
  452. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×