Advertisement
belrey10

SIMON

Jun 8th, 2020
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.79 KB | None | 0 0
  1.   const int led_red = 1;         // Output pins for the LEDs
  2.   const int led_blue = 2;
  3.   const int led_yellow = 3;
  4.   const int led_green = 4;
  5.   const int buzzer = 5;      // Output pin for the buzzer
  6.   const int red_button = 9;      // Input pins for the buttons
  7.   const int blue_button = 10;
  8.   const int yellow_button = 11;
  9.   const int green_button = 12;   // Pin 13 is special - didn't work as planned
  10.   long sequence[20];             // Array to hold sequence
  11.   int count = 0;                 // Sequence counter
  12.   long input = 5;                // Button indicator
  13.   int wait = 500;                // Variable delay as sequence gets longer
  14.  
  15.   /*
  16.   playtone function taken from Oomlout sample
  17.   takes a tone variable that is half the period of desired frequency
  18.   and a duration in milliseconds
  19.   */
  20.  
  21.   void playtone(int tone, int duration) {
  22.     for (long i = 0; i < duration * 1000L; i += tone *2) {
  23.       digitalWrite(buzzer, HIGH);
  24.       delayMicroseconds(tone);
  25.       digitalWrite(buzzer, LOW);
  26.       delayMicroseconds(tone);
  27.     }
  28.   }
  29.  
  30.   /*
  31.   functions to flash LEDs and play corresponding tones
  32.   very simple - turn LED on, play tone for .5s, turn LED off
  33.   */
  34.  
  35.   void flash_red() {
  36.     digitalWrite(led_red, HIGH);
  37.     playtone(2273,wait);             // low A
  38.     digitalWrite(led_red, LOW);
  39.   }
  40.  
  41.   void flash_blue() {
  42.     digitalWrite(led_blue, HIGH);
  43.     playtone(1700,wait);             // D
  44.     digitalWrite(led_blue, LOW);
  45.   }
  46.  
  47.   void flash_yellow() {
  48.     digitalWrite(led_yellow, HIGH);
  49.     playtone(1275,wait);             // G
  50.     digitalWrite(led_yellow, LOW);
  51.   }
  52.  
  53.   void flash_green() {
  54.     digitalWrite(led_green, HIGH);
  55.     playtone(1136,wait);             // high A
  56.     digitalWrite(led_green, LOW);
  57.  
  58.   }
  59.  
  60.   // a simple test function to flash all of the LEDs in turn
  61.  
  62.   void runtest() {
  63.  
  64.     flash_red();
  65.     flash_green();
  66.     flash_yellow();
  67.     flash_blue();  
  68.  
  69.   }
  70.  
  71.   /* a function to flash the LED corresponding to what is held
  72.   in the sequence
  73.   */
  74.  
  75.   void squark(long led) {
  76.  
  77.     switch (led) {
  78.         case 0:
  79.           flash_red();
  80.           break;
  81.         case 1:
  82.           flash_green();
  83.           break;
  84.         case 2:
  85.           flash_yellow();
  86.           break;
  87.         case 3:
  88.           flash_blue();
  89.           break;
  90.       }
  91.  
  92.       delay(50);
  93.   }
  94.  
  95.   // function to congratulate winning sequence
  96.   void congratulate() {
  97.  
  98.     digitalWrite(led_red, HIGH);       // turn all LEDs on
  99.     digitalWrite(led_green, HIGH);
  100.     digitalWrite(led_yellow, HIGH);
  101.     digitalWrite(led_blue, HIGH);
  102.     playtone(1014,250);                // play a jingle
  103.     delay(25);    
  104.  
  105.     playtone(1014,250);
  106.     delay(25);    
  107.  
  108.     playtone(1014,250);
  109.     delay(25);    
  110.  
  111.     playtone(956,500);
  112.     delay(25);    
  113.  
  114.     playtone(1014,250);
  115.     delay(25);
  116.  
  117.     playtone(956,500);
  118.     delay(2000);  
  119.  
  120.     digitalWrite(led_red, LOW);        // turn all LEDs off
  121.     digitalWrite(led_green, LOW);
  122.     digitalWrite(led_yellow, LOW);
  123.     digitalWrite(led_blue, LOW);
  124.     resetCount();                      // reset sequence
  125.  
  126.   }
  127.   // function to reset after winning or losing
  128.  
  129.   void resetCount() {
  130.     count = 0;
  131.     wait = 500;
  132.   }
  133.  
  134.   // function to build and play the sequence
  135.  
  136.   void playSequence() {
  137.  
  138.     sequence[count] = random(4);       // add a new value to sequence
  139.     for (int i = 0; i < count; i++) {  // loop for sequence length
  140.       squark(sequence[i]);             // flash/beep
  141.  
  142.     }
  143.     wait = 500 - (count * 15);         // vary delay
  144.     count++;                           // increment sequence length
  145.   }
  146.  
  147.   // function to read sequence from player
  148.  
  149.   void readSequence() {
  150.    for (int i=1; i < count; i++) {               // loop for sequence length
  151.       while (input==5){                          // wait until button pressed
  152.  
  153.         if (digitalRead(red_button) == LOW) {    // Red button
  154.           input = 0;
  155.         }
  156.         if (digitalRead(green_button) == LOW) {  // Green button
  157.           input = 1;
  158.         }
  159.         if (digitalRead(yellow_button) == LOW) { // Yellow button
  160.           input = 2;
  161.         }
  162.         if (digitalRead(blue_button) == LOW) {   // Blue button
  163.           input = 3;
  164.         }
  165.       }
  166.  
  167.       if (sequence[i-1] == input) {              // was it the right button?
  168.         squark(input);                           // flash/buzz
  169.         if (i == 20) {                           // check for correct sequence of 20
  170.           congratulate();                        // congratulate the winner
  171.         }
  172.  
  173.       }
  174.         else {
  175.  
  176.           playtone(4545,1000);                   // low tone for fail
  177.           squark(sequence[i-1]);                 // double flash for the right colour
  178.           squark(sequence[i-1]);
  179.           resetCount();                          // reset sequence
  180.       }
  181.  
  182.     input = 5;                                   // reset input
  183.  
  184.     }
  185.   }  
  186.  
  187.  
  188.  
  189.   // standard sketch setup function
  190.  
  191.   void setup() {
  192.  
  193.     pinMode(led_red, OUTPUT);      // configure LEDs and buzzer on outputs
  194.     pinMode(led_green, OUTPUT);
  195.     pinMode(led_yellow, OUTPUT);
  196.     pinMode(led_blue, OUTPUT);
  197.     pinMode(buzzer, OUTPUT);
  198.     pinMode(red_button, INPUT);    // configure buttons on inputs
  199.     digitalWrite(red_button, HIGH);// turn on pullup resistors
  200.     pinMode(green_button, INPUT);
  201.     digitalWrite(green_button, HIGH);
  202.     pinMode(yellow_button, INPUT);
  203.     digitalWrite(yellow_button, HIGH);
  204.     pinMode(blue_button, INPUT);
  205.     digitalWrite(blue_button, HIGH);
  206.     randomSeed(analogRead(5));     // random seed for sequence generation
  207.     //runtest();
  208.   }
  209.  
  210.   // standard sketch loop function
  211.  
  212.   void loop() {
  213.     playSequence();  // play the sequence
  214.     readSequence();  // read the sequence
  215.     delay(1000);     // wait a sec
  216.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement