Advertisement
starfoxleader

Simon Says Arduino Code

Dec 6th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 11.28 KB | None | 0 0
  1. #define PLAYER_WAIT_TIME 2000 // The time allowed between button presses - 2s
  2.  
  3. byte sequence[100];           // Storage for the light sequence
  4. byte curLen = 0;              // Current length of the sequence
  5. int score = 0;                // Score counter
  6. byte inputCount = 0;          // The number of times that the player has pressed a (correct) button in a given turn
  7. byte lastInput = 0;           // Last input from the player
  8. byte expRd = 0;               // The LED that's suppose to be lit by the player
  9. bool btnDwn = false;          // Check if a button is pressed
  10. bool wait = false;            // Waiting for the user to press a button
  11. bool resetFlag = false;       // Used to indicate to the program that once the player lost
  12.  
  13.  
  14.  
  15. // Arduino joystick pin numbers
  16. const int SW_pin = 0; // digital pin connected to switch output
  17. const int X_pin = 2; // analog pin connected to X output
  18. const int Y_pin = 3; // analog pin connected to Y output
  19.  
  20.  
  21. byte noPins = 4;              // Number of buttons/LEDs
  22.  
  23. byte pins[] = {2, 3, 5, 6}; // Button input pins and LED output pins - change these values if you want to connect your buttons to other pins
  24. // The number of elements must match noPins below
  25.  
  26. long inputTime = 0;           // Timer variable for the delay between user inputs
  27.  
  28. void setup() {
  29.  
  30.   Serial.begin(9600);         // Start Serial monitor. This can be removed too as long as you remove all references to Serial below
  31.   //joystick reading
  32.   pinMode(SW_pin, INPUT);
  33.   digitalWrite(SW_pin, HIGH);
  34.  
  35.   //LDR setup
  36.   pinMode(A5, INPUT);
  37.   pinMode(5, OUTPUT);
  38.  
  39.   Reset();
  40.  
  41.   // define pins for score display
  42.  
  43.   pinMode(7, OUTPUT);
  44.   pinMode(8, OUTPUT);
  45.   pinMode(9, OUTPUT);
  46.   pinMode(10, OUTPUT);
  47.   pinMode(11, OUTPUT);
  48.   pinMode(12, OUTPUT);
  49.   pinMode(13, OUTPUT);
  50.  
  51. }
  52.  
  53.  
  54. /// Sets all the pins as either INPUT or OUTPUT based on the value of 'dir'
  55.  
  56. void setPinDirection(byte dir) {
  57.   for (byte i = 0; i < noPins; i++) {
  58.     pinMode(pins[i], dir);
  59.   }
  60. }
  61.  
  62. //send the same value to all the LED pins
  63. void writeAllPins(byte val) {
  64.   for (byte i = 0; i < noPins; i++) {
  65.     digitalWrite(pins[i], val);
  66.   }
  67. }
  68.  
  69. /// Flashes all the LEDs together
  70. /// freq is the blink speed - small number -> fast | big number -> slow
  71.  
  72. void flash(short freq) {
  73.   setPinDirection(OUTPUT); /// We're activating the LEDS now
  74.   for (int i = 0; i < 5; i++) {
  75.     writeAllPins(HIGH);
  76.     delay(freq);
  77.     writeAllPins(LOW);
  78.     delay(freq);
  79.   }
  80. }
  81.  
  82. ///This function resets all the game variables to their default values
  83.  
  84. void Reset() {
  85.   flash(500);
  86.   curLen = 0;
  87.   inputCount = 0;
  88.   lastInput = 0;
  89.   expRd = 0;
  90.   score = 0;
  91.   digitalWrite(7, LOW);
  92.   digitalWrite(8, HIGH);
  93.   digitalWrite(9, HIGH);
  94.   digitalWrite(10, HIGH);
  95.   digitalWrite(11, HIGH);
  96.   digitalWrite(12, HIGH);
  97.   digitalWrite(13, HIGH);
  98.   btnDwn = false;
  99.   wait = false;
  100.   resetFlag = false;
  101. }
  102.  
  103.  
  104. /// User lost
  105. void Lose() {
  106.   flash(50);
  107. }
  108.  
  109.  
  110. /// The arduino shows the user what must be memorized
  111. /// Also called after losing to show you what you last sequence was
  112.  
  113. void playSequence() {
  114.   //Loop through the stored sequence and light the appropriate LEDs in turn
  115.   for (int i = 0; i < curLen; i++) {
  116.     Serial.print("Seq: ");
  117.     Serial.print(i);
  118.     Serial.print("Pin: ");
  119.     Serial.println(sequence[i]);
  120.     digitalWrite(sequence[i], HIGH);
  121.     delay(500);
  122.     digitalWrite(sequence[i], LOW);
  123.     delay(250);
  124.   }
  125. }
  126.  
  127.  
  128. /// The events that occur upon a loss
  129.  
  130. void DoLoseProcess() {
  131.   Lose();             // Flash all the LEDS quickly (see Lose function)
  132.   delay(1000);
  133.   playSequence();     // Shows the user the last sequence - So you can count remember your best score - Mine's 22 by the way :)
  134.   delay(1000);
  135.   Reset();            // Reset everything for a new game
  136. }
  137.  
  138. ///
  139. /// Game Logic
  140. ///
  141. void loop() {
  142.  
  143.   if (!wait) {
  144.     //****************//
  145.     // Arduino's turn //
  146.     //****************//
  147.     setPinDirection(OUTPUT);                      // We're using the LEDs
  148.  
  149.     randomSeed(analogRead(A0));                   // https://www.arduino.cc/en/Reference/RandomSeed
  150.     sequence[curLen] = pins[random(0, noPins)];   // Put a new random value in the next position in the sequence -  https://www.arduino.cc/en/Reference/random
  151.     curLen++;                                     // Set the new Current length of the sequence
  152.  
  153.     playSequence();                               // Show the sequence to the player
  154.  
  155.     wait = true;                                  // Set Wait to true as it's now going to be the turn of the player
  156.     inputTime = millis();                         // Store the time to measure the player's response time
  157.   } else {
  158.     //***************//
  159.     // Player's turn //
  160.     //***************//
  161.     setPinDirection(INPUT);                       // We're using the buttons
  162.  
  163.     //joystick readings
  164.     //LDR reading to pin 2, the green LED in my setup
  165.     if (analogRead(X_pin) < 400) { //pushing up
  166.       digitalWrite(2, HIGH);
  167.     }
  168.  
  169.     if (analogRead(X_pin) > 400) { //release up
  170.       digitalWrite(2, LOW);
  171.     }
  172.  
  173.     //LDR reading to pin 3, the blue LED in my setup
  174.     int val = analogRead(A5);
  175.     Serial.println(val);
  176.     if (val < 950) {
  177.       digitalWrite(3, HIGH);
  178.     }
  179.     if (val > 950){
  180.       digitalWrite(3, LOW);
  181.     }
  182.  
  183.     if (millis() - inputTime > PLAYER_WAIT_TIME) { // If the player takes more than the allowed time,
  184.       DoLoseProcess();                            // Lose process
  185.       return;
  186.     }
  187.  
  188.     if (!btnDwn) {                                //
  189.       expRd = sequence[inputCount];               // Find the value we expect from the player
  190.       Serial.print("Expected: ");                 // Serial Monitor Output - Should be removed if you removed the Serial.begin above
  191.       Serial.println(expRd);                      // Serial Monitor Output - Should be removed if you removed the Serial.begin above
  192.  
  193.       for (int i = 0; i < noPins; i++) {         // Loop through the all the pins
  194.         if (pins[i] == expRd)
  195.           continue;                               // Ignore the correct pin
  196.         if (digitalRead(pins[i]) == HIGH) {       // Is the button pressed
  197.           lastInput = pins[i];
  198.           resetFlag = true;                       // Set the resetFlag - this means you lost
  199.           btnDwn = true;                          // This will prevent the program from doing the same thing over and over again
  200.           Serial.print("Read: ");                 // Serial Monitor Output - Should be removed if you removed the Serial.begin above
  201.           Serial.println(lastInput);              // Serial Monitor Output - Should be removed if you removed the Serial.begin above
  202.         }
  203.       }
  204.     }
  205.  
  206.     if (digitalRead(expRd) == 1 && !btnDwn)       // The player pressed the right button
  207.     {
  208.       inputTime = millis();                       //
  209.       lastInput = expRd;
  210.       inputCount++;                               // The user pressed a (correct) button again
  211.       btnDwn = true;                              // This will prevent the program from doing the same thing over and over again
  212.       Serial.print("Read: ");                     // Serial Monitor Output - Should be removed if you removed the Serial.begin above
  213.       Serial.println(lastInput);                  // Serial Monitor Output - Should be removed if you removed the Serial.begin above
  214.     } else {
  215.       if (btnDwn && digitalRead(lastInput) == LOW) { // Check if the player released the button
  216.         btnDwn = false;
  217.         delay(20);
  218.         if (resetFlag) {                            // If this was set to true up above, you lost
  219.           DoLoseProcess();                          // So we do the losing sequence of events
  220.         }
  221.         else {
  222.           if (inputCount == curLen) {               // Has the player finished repeating the sequence
  223.             wait = false;                           // If so, this will make the next turn the program's turn
  224.             inputCount = 0;                         // Reset the number of times that the player has pressed a button
  225.  
  226.             score++;
  227.      
  228.       //7 segment display for score 1-9
  229.             if (score == 0) {
  230.               digitalWrite(7, LOW);
  231.               digitalWrite(8, HIGH);
  232.               digitalWrite(9, HIGH);
  233.               digitalWrite(10, HIGH);
  234.               digitalWrite(11, HIGH);
  235.               digitalWrite(12, HIGH);
  236.               digitalWrite(13, HIGH);
  237.             }
  238.  
  239.             else if (score == 1) {
  240.               digitalWrite(7, LOW);
  241.               digitalWrite(8, LOW);
  242.               digitalWrite(9, LOW);
  243.               digitalWrite(10, HIGH);
  244.               digitalWrite(11, LOW);
  245.               digitalWrite(12, LOW);
  246.               digitalWrite(13, HIGH);
  247.             }
  248.  
  249.             else if (score == 2) {
  250.  
  251.               digitalWrite(7, HIGH);
  252.               digitalWrite(8, LOW);
  253.               digitalWrite(9, HIGH);
  254.               digitalWrite(10, HIGH);
  255.               digitalWrite(11, HIGH);
  256.               digitalWrite(12, HIGH);
  257.               digitalWrite(13, LOW);
  258.             }
  259.  
  260.             else if (score == 3) {
  261.               digitalWrite(7, HIGH);
  262.               digitalWrite(8, LOW);
  263.               digitalWrite(9, HIGH);
  264.               digitalWrite(10, HIGH);
  265.               digitalWrite(11, LOW);
  266.               digitalWrite(12, HIGH);
  267.               digitalWrite(13, HIGH);
  268.             }
  269.  
  270.             else if (score == 4) {
  271.               digitalWrite(7, HIGH);
  272.               digitalWrite(8, HIGH);
  273.               digitalWrite(9, LOW);
  274.               digitalWrite(10, HIGH);
  275.               digitalWrite(11, LOW);
  276.               digitalWrite(12, LOW);
  277.               digitalWrite(13, HIGH);
  278.             }
  279.  
  280.             else if (score == 5) {
  281.               digitalWrite(7, HIGH);
  282.               digitalWrite(8, HIGH);
  283.               digitalWrite(9, HIGH);
  284.               digitalWrite(10, LOW);
  285.               digitalWrite(11, LOW);
  286.               digitalWrite(12, HIGH);
  287.               digitalWrite(13, HIGH);
  288.             }
  289.  
  290.             else if (score == 6) {
  291.               digitalWrite(7, HIGH);
  292.               digitalWrite(8, HIGH);
  293.               digitalWrite(9, HIGH);
  294.               digitalWrite(10, LOW);
  295.               digitalWrite(11, HIGH);
  296.               digitalWrite(12, HIGH);
  297.               digitalWrite(13, HIGH);
  298.             }
  299.  
  300.             else if (score == 7) {
  301.               digitalWrite(7, LOW);
  302.               digitalWrite(8, LOW);
  303.               digitalWrite(9, HIGH);
  304.               digitalWrite(10, HIGH);
  305.               digitalWrite(11, LOW);
  306.               digitalWrite(12, LOW);
  307.               digitalWrite(13, HIGH);
  308.             }
  309.  
  310.             else if (score == 8) {
  311.               digitalWrite(7, HIGH);
  312.               digitalWrite(8, HIGH);
  313.               digitalWrite(9, HIGH);
  314.               digitalWrite(10, HIGH);
  315.               digitalWrite(11, HIGH);
  316.               digitalWrite(12, HIGH);
  317.               digitalWrite(13, HIGH);
  318.             }
  319.  
  320.             else if (score == 9) {
  321.               digitalWrite(7, HIGH);
  322.               digitalWrite(8, HIGH);
  323.               digitalWrite(9, HIGH);
  324.               digitalWrite(10, HIGH);
  325.               digitalWrite(11, LOW);
  326.               digitalWrite(12, LOW);
  327.               digitalWrite(13, HIGH);
  328.             }
  329.  
  330.  
  331.             delay(1500);
  332.           }
  333.         }
  334.       }
  335.     }
  336.   }
  337. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement