Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.44 KB | None | 0 0
  1. #include <LiquidCrystal_I2C.h>
  2. #include <Wire.h>
  3. #include <Tone.h>
  4.  
  5. LiquidCrystal_I2C lcd(0x27, 16, 2);
  6. Tone speakerpin;
  7. int starttune[] = {NOTE_E4, NOTE_E4, NOTE_C4, NOTE_E4, NOTE_G4, NOTE_G4};
  8. int duration2[] = {100, 200, 100, 200, 100, 400};
  9. int note[] = {NOTE_A4, NOTE_A4, NOTE_A4, NOTE_A4, NOTE_B4, NOTE_A4};
  10. int duration[] = {100, 100, 100, 300, 100, 300};
  11. int button[] = {2, 3, 4, 5}; //The four button input pins
  12. int ledpin[] = {8, 9, 10, 11}; // LED pins
  13. int turn = 0; // turn counter
  14. int buttonstate = 0; // button state checker
  15. int randomArray[50]; //long to store up to 50 inputs
  16. int inputArray[50];
  17. int highscore = 0;
  18. int MyScore;
  19. void setup()
  20. {
  21. //lcd.setBacklightPin(3,POSITIVE);
  22. lcd.setBacklight(LOW);
  23. lcd.begin();
  24. lcd.clear();
  25. lcd.setCursor(0, 1);
  26. lcd.print("Your Score: 0");
  27. lcd.setCursor(0, 0);
  28. lcd.print("High Score: 0");
  29. Serial.begin(9600);
  30. speakerpin.begin(12); // speaker is on pin 12
  31. for(int x=0; x<4; x++) // LED pins are outputs
  32. {
  33. pinMode(ledpin[x], OUTPUT);
  34. }
  35. for(int x=0; x<4; x++)
  36. {
  37. pinMode(button[x],INPUT); // button pins are inputs
  38. digitalWrite(button[x], HIGH); // enable internal pullup; buttons start in high position; logic reversed
  39. }
  40. randomSeed(analogRead(0)); //Added to generate "more randomness" with the randomArray for the output function
  41. for (int thisNote = 0; thisNote < 6; thisNote ++) {
  42. // play the next note:
  43. speakerpin.play(starttune[thisNote]);
  44. // hold the note:
  45. if (thisNote==0 || thisNote==2)
  46. {
  47. digitalWrite(ledpin[0], HIGH);
  48. }
  49. if (thisNote==1 || thisNote==3 )
  50. {
  51. digitalWrite(ledpin[1], HIGH);
  52. }
  53. if (thisNote==4 || thisNote==5)
  54. {
  55. digitalWrite(ledpin[2], HIGH);
  56. }
  57. if (thisNote==6)
  58. {
  59. digitalWrite(ledpin[3], HIGH);
  60. }
  61. delay(duration2[thisNote]);
  62. // stop for the next note:
  63. speakerpin.stop();
  64. digitalWrite(ledpin[0], LOW);
  65. digitalWrite(ledpin[1], LOW);
  66. digitalWrite(ledpin[2], LOW);
  67. digitalWrite(ledpin[3], LOW);
  68. delay(25);
  69. }
  70. delay(1000);
  71. }
  72.  
  73. void loop()
  74. {
  75. for (int y=0; y<=2; y++)
  76. {
  77. //function for generating the array to be matched by the player
  78. digitalWrite(ledpin[0], HIGH);
  79. digitalWrite(ledpin[1], HIGH);
  80. digitalWrite(ledpin[2], HIGH);
  81. digitalWrite(ledpin[3], HIGH);
  82.  
  83. for (int thisNote = 0; thisNote < 6; thisNote ++) {
  84. // play the next note:
  85. speakerpin.play(note[thisNote]);
  86. // hold the note:
  87. delay(duration[thisNote]);
  88. // stop for the next note:
  89. speakerpin.stop();
  90. delay(25);
  91. }
  92.  
  93. digitalWrite(ledpin[0], LOW);
  94. digitalWrite(ledpin[1], LOW);
  95. digitalWrite(ledpin[2], LOW);
  96. digitalWrite(ledpin[3], LOW);
  97. delay(1000);
  98.  
  99. for (int y=turn; y <= turn; y++) //untuk sekali random
  100. { //Limited by the turn variable
  101. Serial.println(""); //Some serial output to follow along
  102. Serial.print("Turn: ");
  103. Serial.print(y);
  104. Serial.println("");
  105. lcd.clear();
  106. MyScore=y;
  107. lcd.setCursor(0, 1); //(Column,Row)
  108. lcd.print("Your Score: ");
  109. lcd.setCursor(12, 1);
  110. lcd.print(MyScore);
  111. if(MyScore > highscore) {
  112. highscore = y;
  113. }
  114. lcd.setCursor(0, 0);
  115. lcd.print("High Score: " + (String)(highscore));
  116. Serial.print(y);
  117. Serial.println("");
  118. delay(1000);
  119. randomArray[y] = random(1, 5); //Assigning a random number (1-4) to the randomArray[y], y being the turn count
  120. for (int x=0; x <= turn; x++)
  121. {
  122. Serial.print(randomArray[x]);
  123.  
  124. for(int y=0; y<4; y++)
  125. {
  126.  
  127. if (randomArray[x] == 1 && ledpin[y] == 8)
  128. { //if statements to display the stored values in the array
  129. digitalWrite(ledpin[y], HIGH);
  130. speakerpin.play(NOTE_G3, 100);
  131. delay(400);
  132. digitalWrite(ledpin[y], LOW);
  133. delay(100);
  134. }
  135.  
  136. if (randomArray[x] == 2 && ledpin[y] == 9)
  137. {
  138. digitalWrite(ledpin[y], HIGH);
  139. speakerpin.play(NOTE_A3, 100);
  140. delay(400);
  141. digitalWrite(ledpin[y], LOW);
  142. delay(100);
  143. }
  144.  
  145. if (randomArray[x] == 3 && ledpin[y] == 10)
  146. {
  147. digitalWrite(ledpin[y], HIGH);
  148. speakerpin.play(NOTE_B3, 100);
  149. delay(400);
  150. digitalWrite(ledpin[y], LOW);
  151. delay(100);
  152. }
  153.  
  154. if (randomArray[x] == 4 && ledpin[y] == 11)
  155. {
  156. digitalWrite(ledpin[y], HIGH);
  157. speakerpin.play(NOTE_C4, 100);
  158. delay(400);
  159. digitalWrite(ledpin[y], LOW);
  160. delay(100);
  161. }
  162. }
  163. }
  164. }
  165. input();
  166.  
  167. }
  168.  
  169.  
  170. }
  171.  
  172.  
  173.  
  174. void input() { //Function for allowing user input and checking input against the generated array
  175.  
  176. for (int x=0; x <= turn;)
  177. { //Statement controlled by turn count
  178.  
  179. for(int y=0; y<4; y++)
  180. {
  181.  
  182. buttonstate = digitalRead(button[y]);
  183.  
  184. if (buttonstate == LOW && button[y] == 2)
  185. { //Checking for button push
  186. digitalWrite(ledpin[0], HIGH);
  187. speakerpin.play(NOTE_G3, 100);
  188. delay(200);
  189. digitalWrite(ledpin[0], LOW);
  190. inputArray[x] = 1;
  191. delay(250);
  192. Serial.print(" ");
  193. Serial.print(1);
  194. if (inputArray[x] != randomArray[x]) { //Checks value input by user and checks it against
  195. fail(); //the value in the same spot on the generated array
  196. } //The fail function is called if it does not match
  197. x++;
  198. }
  199. if (buttonstate == LOW && button[y] == 3)
  200. {
  201. digitalWrite(ledpin[1], HIGH);
  202. speakerpin.play(NOTE_A3, 100);
  203. delay(200);
  204. digitalWrite(ledpin[1], LOW);
  205. inputArray[x] = 2;
  206. delay(250);
  207. Serial.print(" ");
  208. Serial.print(2);
  209. if (inputArray[x] != randomArray[x]) {
  210. fail();
  211. }
  212. x++;
  213. }
  214.  
  215. if (buttonstate == LOW && button[y] == 4)
  216. {
  217. digitalWrite(ledpin[2], HIGH);
  218. speakerpin.play(NOTE_B3, 100);
  219. delay(200);
  220. digitalWrite(ledpin[2], LOW);
  221. inputArray[x] = 3;
  222. delay(250);
  223. Serial.print(" ");
  224. Serial.print(3);
  225. if (inputArray[x] != randomArray[x]) {
  226. fail();
  227. }
  228. x++;
  229. }
  230.  
  231. if (buttonstate == LOW && button[y] == 5)
  232. {
  233. digitalWrite(ledpin[3], HIGH);
  234. speakerpin.play(NOTE_C4, 100);
  235. delay(200);
  236. digitalWrite(ledpin[3], LOW);
  237. inputArray[x] = 4;
  238. delay(250);
  239. Serial.print(" ");
  240. Serial.print(4);
  241. if (inputArray[x] != randomArray[x])
  242. {
  243. fail();
  244. }
  245. x++;
  246. }
  247. }
  248. }
  249. delay(500);
  250. turn++; //Increments the turn count, also the last action before starting the output function over again
  251. }
  252. void fail() { //Function used if the player fails to match the sequence
  253.  
  254. for (int y=0; y<=3; y++)
  255. { //Flashes lights for failure
  256.  
  257. digitalWrite(ledpin[0], HIGH);
  258. digitalWrite(ledpin[1], HIGH);
  259. digitalWrite(ledpin[2], HIGH);
  260. digitalWrite(ledpin[3], HIGH);
  261. speakerpin.play(NOTE_G2, 300);
  262. delay(200);
  263. digitalWrite(ledpin[0], LOW);
  264. digitalWrite(ledpin[1], LOW);
  265. digitalWrite(ledpin[2], LOW);
  266. digitalWrite(ledpin[3], LOW);
  267. speakerpin.play(NOTE_C2, 300);
  268. delay(200);
  269. lcd.clear();
  270. lcd.setCursor(0, 0);
  271. lcd.print("Game Over");
  272. }
  273. delay(2000);
  274. lcd.clear();
  275. lcd.setCursor(0, 0);
  276. lcd.print("High: " + (String) (highscore));
  277. // Serial.print(y);
  278. // Serial.println("");
  279. lcd.setCursor(9, 0);
  280. lcd.print("You: ");
  281. lcd.setCursor(14, 0);
  282. lcd.print(MyScore);
  283. // if(y > highscore) {
  284. // highscore = y;
  285. // }
  286. lcd.setCursor(0, 1);
  287. lcd.print("<-Press to play again!");
  288. // buttonstate = digitalRead(button[y]);
  289. while (digitalRead(button[0]) > 0 &&digitalRead(button[1]) > 0 && digitalRead(button[2]) > 0 && digitalRead(button[3]) > 0 )
  290. {
  291. digitalWrite(ledpin[0], HIGH);
  292. digitalWrite(ledpin[1], HIGH);
  293. digitalWrite(ledpin[2], HIGH);
  294. digitalWrite(ledpin[3], HIGH);
  295. delay(100);
  296. digitalWrite(ledpin[0], LOW);
  297. digitalWrite(ledpin[1], LOW);
  298. digitalWrite(ledpin[2], LOW);
  299. digitalWrite(ledpin[3], LOW);
  300. delay(100);
  301. }
  302. delay(1000);
  303.  
  304. turn = -1; //Resets turn value so the game starts over without need for a reset button
  305. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement