Advertisement
legocoachandy

Simon - full code

Jul 21st, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.61 KB | None | 0 0
  1. #include <Adafruit_CircuitPlayground.h>
  2.  
  3. #define MAX_SEQUENCE 31
  4. #define NO_BUTTON -1
  5. #define FAILURE_TONE 100
  6. #define SEQUENCE_DELAY 800
  7. #define GUESS_TIMEOUT 3000
  8. #define DEBOUNCE 250
  9. #define CAP_THRESHOLD 10
  10.  
  11. uint8_t simonSequence[MAX_SEQUENCE];
  12. uint8_t skillLevel;
  13. uint8_t sequenceLength;
  14. uint8_t currentStep;
  15. unsigned long startGuessTime;
  16. int8_t guess;
  17. bool playSound;
  18.  
  19. struct button {
  20. uint8_t capPad[2];
  21. uint8_t pixel[3];
  22. uint32_t color;
  23. uint16_t freq;
  24. } simonButton[] = {
  25. { {3, 2}, {0, 1, 2}, 0x00FF00, 415 }, // GREEN
  26. { {0, 1}, {2, 3, 4}, 0xFFFF00, 252 }, // YELLOW
  27. { {12, 6}, {5, 6, 7}, 0x0000FF, 209 }, // BLUE
  28. { {9, 10}, {7, 8, 9}, 0xFF0000, 310 }, // RED
  29. };
  30.  
  31. ///////////////////////////////////////////////////////////////////////////////
  32. void chooseSkillLevel() {
  33. while (!CircuitPlayground.rightButton()) {
  34. if (CircuitPlayground.leftButton()) {
  35. skillLevel = skillLevel + 1;
  36. if (skillLevel > 4)
  37. skillLevel = 1;
  38.  
  39. CircuitPlayground.clearPixels();
  40. for (int p = 0; p < skillLevel; p++) {
  41. CircuitPlayground.setPixelColor(p, 0xFFFFFF);
  42. }
  43.  
  44. delay(DEBOUNCE);
  45. }
  46. }
  47. }
  48.  
  49. ///////////////////////////////////////////////////////////////////////////////
  50. void newGame() {
  51. // Set game sequence length based on skill level
  52. switch (skillLevel) {
  53. case 1:
  54. sequenceLength = 8;
  55. break;
  56. case 2:
  57. sequenceLength = 14;
  58. break;
  59. case 3:
  60. sequenceLength = 20;
  61. break;
  62. case 4:
  63. sequenceLength = 31;
  64. break;
  65. }
  66.  
  67. // Populate the game sequence
  68. for (int i = 0; i < sequenceLength; i++) {
  69. simonSequence[i] = random(4);
  70. }
  71.  
  72. // We start with the first step in the sequence
  73. currentStep = 1;
  74. }
  75.  
  76. ///////////////////////////////////////////////////////////////////////////////
  77. void showSequence() {
  78. // Set tone playback duration based on current sequence length
  79. uint16_t toneDuration;
  80. if (currentStep <= 5) {
  81. toneDuration = 420;
  82. } else if (currentStep <= 13) {
  83. toneDuration = 320;
  84. } else {
  85. toneDuration = 220;
  86. }
  87.  
  88. // Play back sequence up to current step
  89. for (int i = 0; i < currentStep; i++) {
  90. delay(50);
  91. indicateButton(simonSequence[i], toneDuration);
  92. }
  93. }
  94.  
  95. ///////////////////////////////////////////////////////////////////////////////
  96. void indicateButton(uint8_t b, uint16_t duration) {
  97. CircuitPlayground.clearPixels();
  98. for (int p = 0; p < 3; p++) {
  99. CircuitPlayground.setPixelColor(simonButton[b].pixel[p], simonButton[b].color);
  100. }
  101.  
  102. if (playSound)
  103. CircuitPlayground.playTone(simonButton[b].freq, duration);
  104. else
  105. delay(duration);
  106.  
  107. CircuitPlayground.clearPixels();
  108. }
  109.  
  110. ///////////////////////////////////////////////////////////////////////////////
  111. void gameLost(int b) {
  112. // Show button that should have been pressed
  113. for (int p = 0; p < 3; p++) {
  114. CircuitPlayground.setPixelColor(simonButton[b].pixel[p], simonButton[b].color);
  115. }
  116.  
  117. // Play sad sound :(
  118. if(playSound)
  119. CircuitPlayground.playTone(FAILURE_TONE, 1500);
  120.  
  121. // And just sit here until reset
  122. while (true) {}
  123. }
  124.  
  125. ///////////////////////////////////////////////////////////////////////////////
  126. void gameWon() {
  127. // Play 'razz' special victory signal
  128. for (int i = 0; i < 3; i++) {
  129. indicateButton(3, 100); // RED
  130. indicateButton(1, 100); // YELLOW
  131. indicateButton(2, 100); // BLUE
  132. indicateButton(0, 100); // GREEN
  133. }
  134. indicateButton(3, 100); // RED
  135. indicateButton(1, 100); // YELLOW
  136.  
  137. // Change tones to failure tone
  138. for (int b = 0; b < 4; b++)
  139. simonButton[b].freq = FAILURE_TONE;
  140.  
  141. // Continue for another 0.8 seconds
  142. for (int i = 0; i < 2; i++) {
  143. indicateButton(2, 100); // BLUE
  144. indicateButton(0, 100); // GREEN
  145. indicateButton(3, 100); // RED
  146. indicateButton(1, 100); // YELLOW
  147. }
  148.  
  149. // Change tones to silence
  150. for (int b = 0; b < 4; b++) simonButton[b].freq = 0;
  151.  
  152. // Loop lights forever
  153. while (true) {
  154. indicateButton(2, 100); // BLUE
  155. indicateButton(0, 100); // GREEN
  156. indicateButton(3, 100); // RED
  157. indicateButton(1, 100); // YELLOW
  158. }
  159. }
  160.  
  161. ///////////////////////////////////////////////////////////////////////////////
  162. uint8_t getButtonPress() {
  163. for (int b = 0; b < 4; b++) {
  164. for (int p = 0; p < 2; p++) {
  165. if (CircuitPlayground.readCap(simonButton[b].capPad[p]) > CAP_THRESHOLD) {
  166. indicateButton(b, DEBOUNCE);
  167. return b;
  168. }
  169. }
  170. }
  171. return NO_BUTTON;
  172. }
  173.  
  174. ///////////////////////////////////////////////////////////////////////////////
  175. void setup() {
  176. // Initialize the Circuit Playground
  177. CircuitPlayground.begin();
  178.  
  179. playSound = CircuitPlayground.slideSwitch();
  180.  
  181. // Set play level
  182. skillLevel = 1;
  183. CircuitPlayground.clearPixels();
  184. CircuitPlayground.setPixelColor(0, 0xFFFFFF);
  185. chooseSkillLevel();
  186.  
  187. // Sowing the seeds of random
  188. randomSeed(millis());
  189.  
  190. // Create game
  191. newGame();
  192. }
  193.  
  194. ///////////////////////////////////////////////////////////////////////////////
  195. void loop() {
  196. // Show sequence up to current step
  197. showSequence();
  198.  
  199. // Read player button presses
  200. for (int s = 0; s < currentStep; s++) {
  201. startGuessTime = millis();
  202. guess = NO_BUTTON;
  203. while ((millis() - startGuessTime < GUESS_TIMEOUT) && (guess == NO_BUTTON)) {
  204. guess = getButtonPress();
  205. }
  206. if (guess != simonSequence[s]) {
  207. gameLost(simonSequence[s]);
  208. }
  209. }
  210.  
  211. currentStep++;
  212. if (currentStep > sequenceLength) {
  213. delay(SEQUENCE_DELAY);
  214. gameWon();
  215. }
  216. delay(SEQUENCE_DELAY);
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement