Advertisement
Guest User

Untitled

a guest
Jul 20th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.77 KB | None | 0 0
  1. /* CONSTANTS */
  2. /* RED */
  3. const int ledPinR = 2;
  4. const int bttnPinR = 6;
  5. const int cR = 0;
  6. const int tR = 310;
  7. /* GREEN */
  8. const int ledPinG = 3;
  9. const int bttnPinG = 7;
  10. const int cG = 1;
  11. const int tG = 415;
  12. /* BLUE */
  13. const int ledPinB = 4;
  14. const int bttnPinB = 8;
  15. const int cB = 2;
  16. const int tB = 209;
  17. /* YELLOW */
  18. const int ledPinY = 5;
  19. const int bttnPinY = 9;
  20. const int cY = 3;
  21. const int tY = 252;
  22. /* TONES */
  23. const int lose = 5;
  24. const int toneOff = 6;
  25. /* SPEAKER */
  26. const int spkrPin = 11;
  27. /* BUTTON */
  28. const int no_bttn = 24;
  29. const int ok_bttn = 23;
  30. /* RNG */
  31. int sequence [32];
  32.  
  33. /* SETUP */
  34. void setup() {
  35. /* Defines outputs and inputs */
  36. pinMode(ledPinR, OUTPUT);
  37. pinMode(bttnPinR, INPUT_PULLUP);
  38. pinMode(ledPinG, OUTPUT);
  39. pinMode(bttnPinG, INPUT_PULLUP);
  40. pinMode(ledPinB, OUTPUT);
  41. pinMode(bttnPinB, INPUT_PULLUP);
  42. pinMode(ledPinY, OUTPUT);
  43. pinMode(bttnPinY, INPUT_PULLUP);
  44. pinMode(spkrPin, OUTPUT);
  45. /* Makes sure all lights are initially off */
  46. digitalWrite(ledPinR, HIGH);
  47. digitalWrite(ledPinG, HIGH);
  48. digitalWrite(ledPinB, HIGH);
  49. digitalWrite(ledPinY, HIGH);
  50. }
  51.  
  52. /* RNG */
  53. void fillSequence() {
  54. int index = 0;
  55. while (index < 32) {
  56. sequence[index] = random(4);
  57. index++;
  58. }
  59. }
  60.  
  61. /* Initial Loop */
  62. void loop() {
  63. executeSimon();
  64. }
  65.  
  66. /* Turns on LED */
  67. void ledOn(int color) {
  68. if (color == cR) {
  69. digitalWrite(ledPinR, LOW);
  70. }
  71. else if (color == cG) {
  72. digitalWrite(ledPinG, LOW);
  73. }
  74. else if (color == cB) {
  75. digitalWrite(ledPinB, LOW);
  76. }
  77. else if (color == cY) {
  78. digitalWrite(ledPinY, LOW);
  79. }
  80. }
  81.  
  82. /* Turns off LED */
  83. void ledOff(int color) {
  84. if (color == cR) {
  85. digitalWrite(ledPinR, HIGH);
  86. }
  87. else if (color == cG) {
  88. digitalWrite(ledPinG, HIGH);
  89. }
  90. else if (color == cB) {
  91. digitalWrite(ledPinB, HIGH);
  92. }
  93. else if (color == cY) {
  94. digitalWrite(ledPinY, HIGH);
  95. }
  96. }
  97.  
  98. /* Plays the given tone */
  99. void playTone(int givenTone) {
  100. if (givenTone == cR) {
  101. tone(spkrPin, 310);
  102. }
  103. else if (givenTone == cG) {
  104. tone(spkrPin, 415);
  105. }
  106. else if (givenTone == cB) {
  107. tone(spkrPin, 209);
  108. }
  109. else if (givenTone == cY) {
  110. tone(spkrPin, 252);
  111. }
  112. else if (givenTone == lose) {
  113. tone(spkrPin, 42);
  114. }
  115. else if (givenTone == toneOff) {
  116. noTone(spkrPin);
  117. }
  118. }
  119.  
  120. /* Winning sequence execution */
  121. void onWin(int color) {
  122. int x = 6;
  123. while (x>0) {
  124. playTone(color);
  125. ledOn(color);
  126. delay(70);
  127. playTone(toneOff);
  128. ledOff(color);
  129. delay(20);
  130. x = x - 1;
  131. }
  132. }
  133.  
  134. /* Losing sequence execution */
  135. void onLose(int color) {
  136. int x = 6;
  137. while (x>0) {
  138. playTone(color);
  139. ledOn(color);
  140. delay(70);
  141. playTone(toneOff);
  142. ledOff(color);
  143. delay(20);
  144. x = x - 1;
  145. }
  146. }
  147.  
  148. /* Special Win Condition */
  149. void onWinExtra(int color) {
  150. playTone(color);
  151. ledOn(color);
  152. delay(200);
  153. playTone(toneOff);
  154. ledOff(color);
  155. delay(200);
  156. playTone(color);
  157. ledOn(color);
  158. delay(200);
  159. playTone(toneOff);
  160. ledOff(color);
  161. delay(200);
  162. playTone(color);
  163. ledOn(color);
  164. delay(200);
  165. playTone(toneOff);
  166. ledOff(color);
  167. delay(200);
  168. ledOn(cR);
  169. playTone(cR);
  170. delay(200);
  171. ledOff(cR);
  172. playTone(cG);
  173. ledOn(cG);
  174. delay(200);
  175. ledOff(cG);
  176. ledOn(cY);
  177. playTone(cY);
  178. delay(200);
  179. ledOff(cY);
  180. ledOn(cB);
  181. playTone(cB);
  182. delay(200);
  183. ledOff(cB);
  184. playTone(toneOff);
  185. delay(200);
  186. ledOn(cY);
  187. ledOn(cB);
  188. ledOn(cR);
  189. ledOn(cG);
  190. playTone(cY);
  191. playTone(cR);
  192. playTone(cG);
  193. playTone(cB);
  194. delay(200);
  195. playTone(toneOff);
  196. ledOff(cY);
  197. ledOff(cB);
  198. ledOff(cR);
  199. ledOff(cG);
  200. }
  201.  
  202. /* Special Lose Condition */
  203. void onLoseExtra(int color) {
  204. playTone(color);
  205. ledOn(color);
  206. delay(200);
  207. playTone(toneOff);
  208. ledOff(color);
  209. delay(200);
  210. playTone(color);
  211. ledOn(color);
  212. delay(200);
  213. playTone(toneOff);
  214. ledOff(color);
  215. delay(200);
  216. playTone(color);
  217. ledOn(color);
  218. delay(200);
  219. playTone(toneOff);
  220. ledOff(color);
  221. delay(200);
  222. ledOn(cR);
  223. playTone(cR);
  224. ledOn(cY);
  225. playTone(cY);
  226. delay(200);
  227. ledOff(cR);
  228. ledOff(cY);
  229. ledOn(cG);
  230. playTone(cG);
  231. ledOn(cB);
  232. playTone(cB);
  233. delay(200);
  234. ledOff(cG);
  235. ledOff(cB);
  236. ledOn(cR);
  237. playTone(cR);
  238. ledOn(cY);
  239. playTone(cY);
  240. delay(200);
  241. ledOff(cR);
  242. ledOff(cY);
  243. ledOn(cG);
  244. playTone(cG);
  245. ledOn(cB);
  246. playTone(cB);
  247. delay(200);
  248. ledOff(cG);
  249. ledOff(cB);
  250. playTone(toneOff);
  251. }
  252.  
  253. /*Returns the pressed button */
  254. int getButton() {
  255. int isPressedR = digitalRead(bttnPinR);
  256. int isPressedG = digitalRead(bttnPinG);
  257. int isPressedB = digitalRead(bttnPinB);
  258. int isPressedY = digitalRead(bttnPinY);
  259. int button = no_bttn;
  260. if (isPressedR == LOW) {
  261. button = cR;
  262. }
  263. else if (isPressedG == LOW) {
  264. button = cG;
  265. }
  266. else if (isPressedB == LOW) {
  267. button = cB;
  268. }
  269. else if (isPressedY == LOW) {
  270. button = cY;
  271. }
  272. return button;
  273. }
  274.  
  275. /* Attracts the player / Difficulty Screen */
  276. int attract() {
  277. int button = no_bttn;
  278. long int oldTime = millis();
  279. long int newTime;
  280. while (button == no_bttn) {
  281. newTime = millis();
  282. if (newTime > (oldTime + 2500)) {
  283. ledOff(cR);
  284. ledOff(cB);
  285. ledOff(cG);
  286. ledOff(cY);
  287. oldTime = millis();
  288. }
  289. else if (newTime > (oldTime + 2000)) {
  290. ledOn(cG);
  291. }
  292. else if (newTime > (oldTime + 1500)) {
  293. ledOn(cR);
  294. }
  295. else if (newTime > (oldTime + 1000)) {
  296. ledOn(cB);
  297. }
  298. else if (newTime > (oldTime + 500)) {
  299. ledOn(cY);
  300. }
  301. button = getButton();
  302. }
  303. ledOff(cR);
  304. ledOff(cG);
  305. ledOff(cB);
  306. ledOff(cY);
  307. return button;
  308. }
  309.  
  310. /* Correct Button Pressed Checker */
  311. int getButtonPlay(int correctColor) {
  312. int button = no_bttn;
  313. long int startTime = millis();
  314. long int elapsedTime = 0;
  315. while (elapsedTime < 3000) {
  316. int buttonPress = getButton();
  317. if (buttonPress != no_bttn) {
  318. if (correctColor == buttonPress) {
  319. ledOn(correctColor);
  320. playTone(correctColor);
  321. while (getButton() == correctColor) {
  322. }
  323. ledOff(correctColor);
  324. playTone(toneOff);
  325. button = ok_bttn;
  326. break;
  327. }
  328. else {
  329. break;
  330. }
  331. }
  332.  
  333. elapsedTime = millis() - startTime;
  334. }
  335. return button;
  336. }
  337.  
  338. /* Plays the LED that gradually speeds up */
  339. void ledPlay(int color, int sequence) {
  340. ledOn(color);
  341. playTone(color);
  342. if (sequence < 6) {
  343. delay(420);
  344. }
  345. else if (sequence > 5 && sequence < 14) {
  346. delay(320);
  347. }
  348. else if (sequence > 13) {
  349. delay(220);
  350. }
  351. ledOff(color);
  352. playTone(toneOff);
  353. }
  354.  
  355. /* Plays the sequence in order */
  356. void playSequence(int colorsToPlay) {
  357. int index = 0;
  358. while (index <colorsToPlay) {
  359. ledPlay(sequence[index], colorsToPlay);
  360. delay(50);
  361. index++;
  362. }
  363. return;
  364. }
  365.  
  366. /* Obtains information regarding the button press of the player */
  367. int getButtonSequence(int colorsToPlay) {
  368. int index = 0;
  369. while (index < colorsToPlay) {
  370. int button = getButtonPlay(sequence [index]);
  371. if (button == no_bttn) {
  372. break;
  373. }
  374. index = index + 1;
  375. }
  376. return index;
  377. }
  378. /* Plays the game */
  379. void playSimon(int size) {
  380. int colorsToPlay = 1;
  381. int lastButton;
  382. while (colorsToPlay <= size) {
  383. playSequence(colorsToPlay);
  384. lastButton = getButtonSequence(colorsToPlay);
  385. if (lastButton != colorsToPlay) {
  386. onLoseExtra(sequence[lastButton]);
  387. delay(1000);
  388. return;
  389. }
  390. colorsToPlay++;
  391. delay(800);
  392. }
  393. onWinExtra(sequence[size-1]);
  394. delay(1000);
  395. }
  396.  
  397. /* Skill Level */
  398. void executeSimon() {
  399. int skill = attract();
  400. delay(1000);
  401. int size = (skill + 1) * 8;
  402. fillSequence();
  403. playSimon(size);
  404. delay(1000);
  405. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement