Advertisement
Danwoo

SIK_Circuit_2C_SimonSays

Nov 1st, 2020
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.   SparkFun Inventor’s Kit
  3.   Circuit 2C-Simon Says
  4.  
  5.   The Simon Says game flashes a pattern using LED lights, then the player must repeat the pattern.
  6.  
  7.   This sketch was written by SparkFun Electronics, with lots of help from the Arduino community.
  8.   This code is completely free for any use.
  9.  
  10.   View circuit diagram and instructions at: https://learn.sparkfun.com/tutorials/sparkfun-inventors-kit-experiment-guide---v41
  11.   Download drawings and code at: https://github.com/sparkfun/SIK-Guide-Code
  12. */
  13.  
  14. //set the pins where the buttons, LEDs and buzzer connect
  15. int button[] = {2, 4, 6, 8};  //red is button[0], yellow is button[1], green is button[2], blue is button[3]
  16. int led[] = {3, 5, 7, 9};     //red is led[0], yellow is led[1], green is led[2], blue is led[3]
  17. int tones[] = {262, 330, 392, 494};   //tones to play with each button (c, e, g, b)
  18.  
  19. int roundsToWin = 10;         //number of rounds the player has to play before they win the game (the array can only hold up to 16 rounds)
  20. int buttonSequence[16];       //make an array of numbers that will be the sequence that the player needs to remember
  21.  
  22. int buzzerPin = 10;           //pin that the buzzer is connected to
  23.  
  24. int pressedButton = 4;        //a variable to remember which button is being pressed. 4 is the value if no button is being pressed.
  25. int roundCounter = 1;         //keeps track of what round the player is on
  26.  
  27.  
  28. long startTime = 0;           //timer variable for time limit on button press
  29. long timeLimit = 2000;        //time limit to hit a button
  30.  
  31. boolean gameStarted = false;      //variable to tell the game whether or not to play the start sequence
  32.  
  33. void setup() {
  34.  
  35.   //set all of the button pins to input_pullup (use the built-in pull-up resistors)
  36.   pinMode(button[0], INPUT_PULLUP);
  37.   pinMode(button[1], INPUT_PULLUP);
  38.   pinMode(button[2], INPUT_PULLUP);
  39.   pinMode(button[3], INPUT_PULLUP);
  40.  
  41.   //set all of the LED pins to output
  42.   pinMode(led[0], OUTPUT);
  43.   pinMode(led[1], OUTPUT);
  44.   pinMode(led[2], OUTPUT);
  45.   pinMode(led[3], OUTPUT);
  46.  
  47.   pinMode(buzzerPin, OUTPUT);   //set the buzzer pin to output
  48. }
  49.  
  50. void loop() {
  51.  
  52.   if (gameStarted == false) {   //if the game hasn't started yet
  53.     startSequence();            //flash the start sequence
  54.     roundCounter = 0;           //reset the round counter
  55.     delay(1500);                //wait a second and a half
  56.     gameStarted = true;         //set gameStarted to true so that this sequence doesn't start again
  57.   }
  58.  
  59.   //each round, start by flashing out the sequence to be repeated
  60.   for (int i = 0; i <= roundCounter; i++) { //go through the array up to the current round number
  61.     flashLED(buttonSequence[i]);          //turn on the LED for that array position and play the sound
  62.     delay(200);                           //wait
  63.     allLEDoff();                          //turn all of the LEDs off
  64.     delay(200);
  65.   }
  66.  
  67.   //then start going through the sequence one at a time and see if the user presses the correct button
  68.   for (int i = 0; i <= roundCounter; i++) { //for each button to be pressed in the sequence
  69.  
  70.     startTime = millis();                 //record the start time
  71.  
  72.     while (gameStarted == true) { //loop until the player presses a button or the time limit is up (the time limit check is in an if statement)
  73.  
  74.       pressedButton = buttonCheck();      //every loop check to see which button is pressed
  75.  
  76.       if (pressedButton < 4) {            //if a button is pressed... (4 means that no button is pressed)
  77.  
  78.         flashLED(pressedButton);          //flash the LED for the button that was pressed
  79.  
  80.         if (pressedButton == buttonSequence[i]) { //if the button matches the button in the sequence
  81.           delay(250);                   //leave the LED light on for a moment
  82.           allLEDoff();                  //then turn off all of the lights and
  83.           break;                        //end the while loop (this will go to the next number in the for loop)
  84.  
  85.         } else {                          //if the button doesn't match the button in the sequence
  86.           loseSequence();               //play the lose sequence (the loose sequence stops the program)
  87.           break;                        //when the program gets back from the lose sequence, break the while loop so that the game can start over
  88.         }
  89.  
  90.       } else {                            //if no button is pressed
  91.         allLEDoff();                      //turn all the LEDs off
  92.       }
  93.  
  94.       //check to see if the time limit is up
  95.       if (millis() - startTime > timeLimit) { //if the time limit is up
  96.         loseSequence();                       //play the lose sequence
  97.         break;                                //when the program gets back from the lose sequence, break the while loop so that the game can start over
  98.       }
  99.     }
  100.   }
  101.  
  102.   if (gameStarted == true) {
  103.     roundCounter = roundCounter + 1;      //increase the round number by 1
  104.  
  105.     if (roundCounter >= roundsToWin) {              //if the player has gotten to the 16th round
  106.       winSequence();                      //play the winning song
  107.     }
  108.  
  109.     delay(500);                           //wait for half a second between rounds
  110.   }
  111.  
  112. }
  113.  
  114. //----------FUNCTIONS------------
  115.  
  116. //FLASH LED
  117. void flashLED (int ledNumber) {
  118.   digitalWrite(led[ledNumber], HIGH);
  119.   tone(buzzerPin, tones[ledNumber]);
  120. }
  121.  
  122. //TURN ALL LEDS OFF
  123. void allLEDoff () {
  124.   //turn all the LEDs off
  125.   digitalWrite(led[0], LOW);
  126.   digitalWrite(led[1], LOW);
  127.   digitalWrite(led[2], LOW);
  128.   digitalWrite(led[3], LOW);
  129.   //turn the buzzer off
  130.   noTone(buzzerPin);
  131. }
  132.  
  133. //CHECK WHICH BUTTON IS PRESSED
  134. int buttonCheck() {
  135.   //check if any buttons are being pressed
  136.   if (digitalRead(button[0]) == LOW) {
  137.     return 0;
  138.   } else if (digitalRead(button[1]) == LOW) {
  139.     return 1;
  140.   } else if (digitalRead(button[2]) == LOW) {
  141.     return 2;
  142.   } else if (digitalRead(button[3]) == LOW) {
  143.     return 3;
  144.   } else {
  145.     return 4; //this will be the value for no button being pressed
  146.   }
  147. }
  148.  
  149. //START SEQUENCE
  150. void startSequence() {
  151.  
  152.   randomSeed(analogRead(A0));   //make sure the random numbers are really random
  153.  
  154.   //populate the buttonSequence array with random numbers from 0 to 3
  155.   for (int i = 0; i <= roundsToWin; i++) {
  156.     buttonSequence[i] = round(random(0, 4));
  157.   }
  158.  
  159.   //flash all of the LEDs when the game starts
  160.   for (int i = 0; i <= 3; i++) {
  161.  
  162.     tone(buzzerPin, tones[i], 200); //play one of the 4 tones
  163.  
  164.     //turn all of the leds on
  165.     digitalWrite(led[0], HIGH);
  166.     digitalWrite(led[1], HIGH);
  167.     digitalWrite(led[2], HIGH);
  168.     digitalWrite(led[3], HIGH);
  169.  
  170.     delay(100);         //wait for a moment
  171.  
  172.     //turn all of the leds off
  173.     digitalWrite(led[0], LOW);
  174.     digitalWrite(led[1], LOW);
  175.     digitalWrite(led[2], LOW);
  176.     digitalWrite(led[3], LOW);
  177.  
  178.     delay(100);   //wait for a moment
  179.  
  180.   } //this will repeat 4 times
  181. }
  182.  
  183. //WIN SEQUENCE
  184. void winSequence() {
  185.  
  186.   //turn all the LEDs on
  187.   for (int j = 0; j <= 3; j++) {
  188.     digitalWrite(led[j], HIGH);
  189.   }
  190.  
  191.   //play the 1Up noise
  192.   tone(buzzerPin, 1318, 150);   //E6
  193.   delay(175);
  194.   tone(buzzerPin, 1567, 150);   //G6
  195.   delay(175);
  196.   tone(buzzerPin, 2637, 150);   //E7
  197.   delay(175);
  198.   tone(buzzerPin, 2093, 150);   //C7
  199.   delay(175);
  200.   tone(buzzerPin, 2349, 150);   //D7
  201.   delay(175);
  202.   tone(buzzerPin, 3135, 500);   //G7
  203.   delay(500);
  204.  
  205.   //wait until a button is pressed
  206.   do {
  207.     pressedButton = buttonCheck();
  208.   } while (pressedButton > 3);
  209.   delay(100);
  210.  
  211.   gameStarted = false;   //reset the game so that the start sequence will play again.
  212.  
  213. }
  214.  
  215. //LOSE SEQUENCE
  216. void loseSequence() {
  217.  
  218.   //turn all the LEDs on
  219.   for (int j = 0; j <= 3; j++) {
  220.     digitalWrite(led[j], HIGH);
  221.   }
  222.  
  223.   //play the 1Up noise
  224.   tone(buzzerPin, 130, 250);   //E6
  225.   delay(275);
  226.   tone(buzzerPin, 73, 250);   //G6
  227.   delay(275);
  228.   tone(buzzerPin, 65, 150);   //E7
  229.   delay(175);
  230.   tone(buzzerPin, 98, 500);   //C7
  231.   delay(500);
  232.  
  233.   //wait until a button is pressed
  234.   do {
  235.     pressedButton = buttonCheck();
  236.   } while (pressedButton > 3);
  237.   delay(200);
  238.  
  239.   gameStarted = false;   //reset the game so that the start sequence will play again.
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement