Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.26 KB | None | 0 0
  1. // Ver 1.8
  2.  
  3. int level = 0; // Used as a test to make sure the sequence output corresponds with the level
  4. int levelDisp = 1; //Used simply to show the level in the serial monitor
  5. /* I used the levelDisp variable because the level starts from zero internally
  6. but i want the system to show that it is infact starting from 1*/
  7.  
  8. boolean startGame = false; // This boolean when set to true starts the gameand
  9. //begins giving out random sequences
  10. boolean gameWin = false;
  11.  
  12. int highScore = 0; // Used to keep track of the High Score
  13. int currentScore = 0; // Used to keep track of the players current score
  14.  
  15. int led1 = 3; // Pins for Leds 1 - 4
  16. int led2 = 4;
  17. int led3 = 5;
  18. int led4 = 6;
  19.  
  20. int button1 = 8; // Pins for Buttons 1 - 4
  21. int button2 = 9;
  22. int button3 = 10;
  23. int button4 = 11;
  24.  
  25. int piezo = 2; // Pin for the piezo
  26.  
  27. int startButton = 12; // Pin for the start button
  28.  
  29. int button1Read = LOW; // Makes sure that button 1 - 4 states are all set to LOW
  30. int button2Read = LOW;
  31. int button3Read = LOW;
  32. int button4Read = LOW;
  33.  
  34. int startButtonRead = LOW; // Makes sure that the start button state is LOW
  35.  
  36. /* (randomOutput/ playerInput)Basically for every level there is a new name given
  37. to the sequence. E.g. level one will have a randomOutput[1] and a playerInput[1]
  38. the output will have a sequence assigned to it
  39. and then the code will check if the player input matches the sequence assigned to the corresponding output.*/
  40.  
  41. int randomOutput[100]; // Used for the output array of sequences max currently set at 100
  42. int playerInput[100]; //Used for the output array of sequences max currently set at 100
  43.  
  44. int maxLevel = 0;
  45.  
  46. int d1 = 50; // Delays have been defined to save time if one needs to change the delay on several points in the code.
  47. int d2 = 100;
  48. int d3 = 200; // This delay in particular (d3) if altered changed the speed of the random sequence output. The shorter it is set the harder the game becomes.
  49. int d4 = 500;
  50.  
  51. void setup(){ //First essential function the setup
  52. Serial.begin(9600); // Begins communications with the serial monitor
  53.  
  54. pinMode(led1, OUTPUT); //Sets Led's 1 - 4 to OUTPUT
  55. pinMode(led2, OUTPUT);
  56. pinMode(led3, OUTPUT);
  57. pinMode(led4, OUTPUT);
  58. pinMode(button1, INPUT); // Sets buttons 1 - 4 to INPUT
  59. pinMode(button2, INPUT);
  60. pinMode(button3, INPUT);
  61. pinMode(button4, INPUT);
  62.  
  63. randomSeed(analogRead(0)); // Used to create more randomness by reading the voltage at pin 0 ( which is not in use in this code )
  64. Serial.println(" "); // Serial.println displays texts and after the line of text it enters one line down in the serial monitor.
  65. Serial.print(" ***** HIGH SCORE: ");
  66. Serial.print(highScore); // Here we are calling upon the highScore variable. This will display the current high score in the serial monitor.
  67. Serial.println(" ***** ");
  68. Serial.println("");
  69.  
  70. Serial.println("Press and hold start to begin :D ");
  71.  
  72. while(startGame == false){ //While startGame is set to false the game will not initiate.
  73.  
  74. for (int a = 0; a <= 255; a++){
  75. // This for loop is used in combination with the PWM feature of the board( Pulse Width Modulation)
  76. analogWrite(led1, a); // I used this to change the brightness of the light from bright to dim.
  77. digitalWrite(led2, HIGH); // The yellow led is connected to pin 4 which does not support PWM thats why it doesnt display the
  78. analogWrite(led3, a); // effect. All pins that support PWM have a ~ next to them e.g. 3~, 5~, 6~.
  79. analogWrite(led4, a);
  80. delay(10);
  81.  
  82. if(a == 255){
  83. digitalWrite(led2, LOW);
  84. delay(100);
  85. }
  86.  
  87. startButtonRead = digitalRead(startButton);
  88. }
  89. if(startButtonRead == HIGH){ // If start button is pressed
  90. startGame = true; //startGame is set to true
  91.  
  92. digitalWrite(led1, HIGH); // These led's flash when startGame = true
  93. delay(d2);
  94. digitalWrite(led2, HIGH);
  95. digitalWrite(led1, LOW);
  96. delay(d2);
  97. digitalWrite(led3, HIGH);
  98. digitalWrite(led2, LOW);
  99. delay(d2);
  100. digitalWrite(led4, HIGH);
  101. digitalWrite(led3, LOW);
  102. delay(d2);
  103. digitalWrite(led4, LOW);
  104. delay(1000);
  105. }
  106. }
  107. while(startGame == true){
  108. for(int a = 0; a <= 100; a++){// This for loop is used to loop through the functions.
  109. systemOutput();
  110. systemInput();
  111. }
  112. }
  113. }
  114.  
  115. void loop(){ // Had to include the void loop as the arduino gave a compiling error if the void loop was not present even though it is not in use in the code currently.
  116. }
  117.  
  118. void systemOutput(){ // This function is used to give out sequences assigned to different variable headings.
  119. for(int a = level; a <= level; a++){ // Sets it for this function that a is equal to to the level or it can be less than but it can never exceed the level and after every pass a goes up by 1.
  120. Serial.println("");
  121. Serial.print("Your current score: ");
  122. Serial.print(currentScore); // Displays current score.
  123. Serial.println("");
  124. Serial.print("level: ");
  125. Serial.print(levelDisp); // Displays current level.
  126. Serial.println("");
  127. Serial.println("");
  128. Serial.println("Sequence output...");
  129.  
  130.  
  131. randomOutput[a] = random(1, 5); // Picks a random number between 1 - 4 ( 5 is is never choosen.
  132.  
  133. for(int b = 0; b <= level; b++){ /* B is used to assign a number in the variable in randomOutput.. randomOutput[b] = randomOutput[1] level
  134. two randomOutput[1] + randomOutput[2] .. etc. the previous sequence is carried forward into the next game. */
  135. Serial.print(randomOutput[b]); // Prints it on the screen E.g. for level 4 it could be 1433 for level 5 it would be somthing like 14332 only one number is added to the end.
  136.  
  137. if(randomOutput[b] == 1){ // If randomOutput is = to 1
  138. tone(piezo, 261, 100); // Tone is played
  139. digitalWrite(led1, HIGH); // Led 1 is set to HIGH
  140. delay(d3); // A delay of 0.2sec
  141. digitalWrite(led1, LOW); // Then Led 1 is set to LOW
  142. delay(d2);
  143. }
  144. if(randomOutput[b] == 2){
  145. tone(piezo, 294, 100);
  146. digitalWrite(led2, HIGH);
  147. delay(d3);
  148. digitalWrite(led2, LOW);
  149. delay(d2);
  150. }
  151. if(randomOutput[b] == 3){
  152. tone(piezo, 349, 100);
  153. digitalWrite(led3, HIGH);
  154. delay(d3);
  155. digitalWrite(led3, LOW);
  156. delay(d2);
  157. }
  158. if(randomOutput[b] == 4){
  159. tone(piezo, 392, 100);
  160. digitalWrite(led4, HIGH);
  161. delay(d3);
  162. digitalWrite(led4, LOW);
  163. delay(d2);
  164. }
  165. }
  166. }
  167. }
  168.  
  169. void systemInput(){ // This function just checks against the sequences given out in the systemOutput function.
  170. Serial.println("");
  171. Serial.println("");
  172. Serial.println("Sequence input...");
  173.  
  174.  
  175. for(int b = 0; b <= level;){
  176. button1Read = digitalRead(button1); //Tells the system where the button state information will be taken from.
  177. button2Read = digitalRead(button2);
  178. button3Read = digitalRead(button3);
  179. button4Read = digitalRead(button4);
  180.  
  181. if(button1Read == HIGH){ //If button 1 is pressed then do the following
  182. tone(piezo, 261, 100);
  183. digitalWrite(led1, HIGH);
  184. delay(d3);
  185. digitalWrite(led1, LOW);
  186. playerInput[b] = 1; // If playerInput[b] is equal to 1 then it proceeds to the next step **
  187. delay(d1);
  188. Serial.print(1);
  189.  
  190. if(playerInput[b] != randomOutput[b]){ // If playerInput and the randomOutput are not equal to each other then it does the following.
  191. oopsieDaisy(); // System goes to the oopsieDaisy function if the 'if' statement requierments are met.
  192. }
  193. b++; // ** The sysytem prooceeds to this section where one is added to b and it increases in length
  194. }
  195. if(button2Read == HIGH){
  196. tone(piezo, 294, 100);
  197. digitalWrite(led2, HIGH);
  198. delay(d3);
  199. digitalWrite(led2, LOW);
  200. playerInput[b] = 2;
  201. delay(d1);
  202. Serial.print(2);
  203.  
  204. if(playerInput[b] != randomOutput[b]){
  205. oopsieDaisy();
  206. }
  207. b++;
  208. }
  209. if(button3Read == HIGH){
  210. tone(piezo, 349, 100);
  211. digitalWrite(led3, HIGH);
  212. delay(d3);
  213. digitalWrite(led3, LOW);
  214. playerInput[b] = 3;
  215. delay(d1);
  216. Serial.print(3);
  217.  
  218. if(playerInput[b] != randomOutput[b]){
  219. oopsieDaisy();
  220. }
  221. b++;
  222. }
  223. if(button4Read == HIGH){
  224. tone(piezo, 392, 100);
  225. digitalWrite(led4, HIGH);
  226. delay(d3);
  227. digitalWrite(led4, LOW);
  228. playerInput[b] = 4;
  229. delay(d1);
  230. Serial.print(4);
  231.  
  232. if(playerInput[b] != randomOutput[b]){
  233. oopsieDaisy();
  234. }
  235. b++;
  236. }
  237. }
  238. delay(d4);
  239. level++; // At the end of the function if the player has accomplished the level successfuly then the level is increased by 1.
  240. levelDisp++; // The level displayed on the serial monitor is also increased by 1.
  241. currentScore = currentScore + 100; // The current score is increased by one.
  242.  
  243. if(levelDisp == 11){ //Sets what level you want to win the game at.
  244. gameWin = true; // (Always set to one level higher than your last level.
  245. yayYouWon();
  246. }
  247. else{
  248. Serial.println("");
  249. Serial.println("");
  250. Serial.println("");
  251. }
  252. }
  253.  
  254. void oopsieDaisy(){ // This function is called when the player fails to match a sequence output.
  255. for(int oohPrettyLights = 0; oohPrettyLights <= 6; oohPrettyLights++){
  256. digitalWrite(led1, HIGH);
  257. digitalWrite(led2, HIGH);
  258. digitalWrite(led3, HIGH);
  259. digitalWrite(led4, HIGH);
  260. delay(50);
  261. digitalWrite(led1, LOW);
  262. digitalWrite(led2, LOW);
  263. digitalWrite(led3, LOW);
  264. digitalWrite(led4, LOW);
  265. delay(50);
  266. }
  267. tone(piezo, 262, 100);
  268. delay(400);
  269. tone(piezo, 262,400);
  270. delay(400);
  271. tone(piezo, 262,600);
  272. delay(400);
  273. tone(piezo, 294,1700);
  274. delay(400);
  275. tone(piezo, 294,400);
  276. delay(400);
  277. tone(piezo, 294,900);
  278. delay(400);
  279. tone(piezo, 262,1000);
  280. delay(400);
  281. tone(piezo, 294, 1000);
  282. delay(400);
  283. tone(piezo, 262, 600);
  284. delay(400);
  285. tone(piezo, 262, 500);
  286. delay(d4);
  287.  
  288. startGame = false; // The startGame boolean is set to false meaning that it will reset the whole game
  289. // and the player will have to press start to change the boolean to true in order to play again.
  290.  
  291. delay(d2);
  292. Serial.println(" ");
  293. Serial.println(" ");
  294. Serial.println("|------------------------------| ");
  295. Serial.println("|-----------GAME OVER----------| ");
  296. Serial.println("|------------------------------| ");
  297. scoreSystem(); //Takes player to the score system function
  298. }
  299.  
  300. void scoreSystem(){ // This function is used to help regulate the High Score at the end of the game.
  301.  
  302. if(currentScore > highScore){ // If the player's current score 'currentScore' is higher than the previous high score 'highScore'
  303. highScore = currentScore + 0; // The high score is then set to the players current score.
  304. //I added a + 0 at the end of it because it stopped a bug in the code.
  305. delay(200);
  306.  
  307. for(int a = 1; a <= 50; a++){ // This for loop is to enter blank lines on serial to clear screen
  308. Serial.println(" "); // Strictly for aperance on serial monitor.
  309. }
  310.  
  311. Serial.println(" ");
  312. Serial.println(" Congratulations");
  313. Serial.println("you have beaten the High Score :D");
  314. Serial.print(" The new High Score is: ");
  315. Serial.println(highScore); // Shows the new high score
  316. delay(1000);
  317. delay(d1);
  318. theBigReset(); // goes to 'theBigReset' function
  319. }
  320. else{ // If the player has not beat the previous high score then their score for that
  321. //game is simply displayed.
  322.  
  323. for(int a = 1; a <= 50; a++){ // This for loop is to enter blank lines on serial to clear screen
  324. Serial.println(" "); // Strictly for aperance on serial monitor.
  325. }
  326.  
  327. delay(d2);
  328. Serial.println(" ");
  329. Serial.print(" You scored : ");
  330. Serial.print(currentScore);
  331. Serial.println(" points in that game!");
  332. delay(d1);
  333. theBigReset();
  334. }
  335. }
  336. /* This function has been put into place to help minimise any glitches in the game.
  337. This function resets all variables listed (currentScore, levelDisp, level) to prepare for the next
  338. game. The variables could have been reset in the oopsieDaisy function or at the end of a failed level
  339. but to help make the code simple and minimise risk of any errors and to make it easier to fix bugs
  340. and or make any changes to the reset values, they were all added in this one function. */
  341. void theBigReset(){
  342. Serial.println(" ");
  343. currentScore = -100;
  344. levelDisp = 0;
  345. level = -1;
  346. delay(400);
  347. setup(); //The game then at last returns to the setup to begin all over again. Once the start button is pressed ofcourse.
  348. }
  349. void yayYouWon(){ // This function is called upon for when you win the game
  350. for(int a = 0; a <= 10; a++){ // This for loop add's 50 blank lines into the serial monitor.
  351. Serial.println(" ");
  352. }
  353. Serial.println(" Congratulations You've won the game :D !!!!! ");
  354. Serial.println("");
  355. Serial.println(" Press and hold start to restart the game!");
  356. for(int a = 0; a <= 4; a++){
  357. Serial.println(" ");
  358. }
  359. startButtonRead = LOW;
  360. while(gameWin == true){
  361.  
  362. for(int b = 1; b > -1; b++){
  363. digitalWrite(led1, HIGH); // These led's flash when startGame = true
  364. delay(d2);
  365. digitalWrite(led2, HIGH);
  366. digitalWrite(led1, LOW);
  367. delay(d2);
  368. digitalWrite(led3, HIGH);
  369. digitalWrite(led2, LOW);
  370. delay(d2);
  371. digitalWrite(led4, HIGH);
  372. digitalWrite(led3, LOW);
  373. delay(d2);
  374. digitalWrite(led4, LOW);
  375. delay(1000);
  376.  
  377. startButtonRead = digitalRead(startButton);
  378.  
  379. if(startButtonRead == HIGH){
  380. gameWin = false;
  381. startGame = false;
  382. scoreSystem();
  383. }
  384. }
  385. }
  386. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement