Advertisement
belrey10

Lesson #10: Whack an LED

Nov 2nd, 2024 (edited)
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.   const int led_red = 2;         // Output pins for the LEDs
  2.   const int led_blue = 3;
  3.   const int led_yellow = 4;
  4.   const int led_green = 5;
  5.   const int red_button = 12;     // Input pins for the buttons
  6.   const int blue_button = 11;
  7.   const int yellow_button = 10;
  8.   const int green_button = 9;  
  9.   int count = 0;                 // Hit (score) counter
  10.   int playTime = 10;             // Amount of "Moles" to pop out.
  11.  
  12.  
  13.   /*
  14.   functions to flash LEDs and pop up that "Mole" - (aka LED).
  15.   */
  16.   void flash_red() {
  17.     digitalWrite(led_red, HIGH);
  18.     delay(1000);
  19.     if (digitalRead(red_button) == LOW) {    // Red button
  20.           count=count+1;
  21.           Serial.println(count);
  22.     }
  23.     digitalWrite(led_red, LOW);
  24.   }
  25.  
  26.   void flash_blue() {
  27.     digitalWrite(led_blue, HIGH);
  28.     delay(1000);
  29.         if (digitalRead(blue_button) == LOW) {    // Blue button
  30.           count=count+1;
  31.           Serial.println(count);
  32.     }
  33.     digitalWrite(led_blue, LOW);
  34.   }
  35.  
  36.   void flash_yellow() {
  37.     digitalWrite(led_yellow, HIGH);
  38.     delay(1000);
  39.         if (digitalRead(yellow_button) == LOW) {    // Yellow button
  40.           count=count+1;
  41.           Serial.println(count);
  42.     }
  43.     digitalWrite(led_yellow, LOW);
  44.   }
  45.  
  46.   void flash_green() {
  47.     digitalWrite(led_green, HIGH);
  48.     delay(1000);
  49.         if (digitalRead(green_button) == LOW) {    // Green button
  50.           count=count+1;
  51.           Serial.println(count);
  52.     }
  53.     digitalWrite(led_green, LOW);
  54.   }
  55.  
  56.   /* a function to select which mole to popup
  57.   */
  58.   void MolePopup(long led) {
  59.     switch (led) {
  60.         case 0:
  61.           flash_red();
  62.           break;
  63.         case 1:
  64.           flash_green();
  65.           break;
  66.         case 2:
  67.           flash_yellow();
  68.           break;
  69.         case 3:
  70.           flash_blue();
  71.           break;
  72.       }
  73.       delay(50);
  74.   }
  75.  
  76.   // function to end the game (repeats 5 times)
  77.   void GameEnd() {
  78.     for(int i = 0; i < 5; i++) {
  79.       digitalWrite(led_red, HIGH);     // turn all LEDs on
  80.       digitalWrite(led_green, HIGH);
  81.       digitalWrite(led_yellow, HIGH);
  82.       digitalWrite(led_blue, HIGH);
  83.       delay(1000);
  84.       digitalWrite(led_red, LOW);        // turn all LEDs off
  85.       digitalWrite(led_green, LOW);
  86.       digitalWrite(led_yellow, LOW);
  87.       digitalWrite(led_blue, LOW);
  88.       delay(1000);      
  89.     }
  90.     count = 0;                         // reset score
  91.   }
  92.  
  93.   // function to build and play the randomized sequence
  94.   void playGame() {
  95.     for (int i = 0; i < playTime; i++) {  // loop for length of playTime
  96.       MolePopup(random(4));             // Have a random "mole" pop up.
  97.     }                      
  98.   }
  99.  
  100.   // standard sketch setup function
  101.   void setup() {
  102.     pinMode(led_red, OUTPUT);         // configure LEDs on outputs
  103.     pinMode(led_green, OUTPUT);
  104.     pinMode(led_yellow, OUTPUT);
  105.     pinMode(led_blue, OUTPUT);
  106.     pinMode(red_button, INPUT);       // configure buttons on inputs
  107.     digitalWrite(red_button, HIGH);   // turn on pullup resistors
  108.     pinMode(green_button, INPUT);
  109.     digitalWrite(green_button, HIGH);
  110.     pinMode(yellow_button, INPUT);
  111.     digitalWrite(yellow_button, HIGH);
  112.     pinMode(blue_button, INPUT);
  113.     digitalWrite(blue_button, HIGH);
  114.     randomSeed(analogRead(5));    // random seed for sequence generation
  115.    
  116.     Serial.begin(9600);           // Where the score is displayed
  117.   }
  118.  
  119.   // standard sketch loop function
  120.   void loop() {
  121.     playGame();      // play the game
  122.     delay(1000);     // wait a sec
  123.     GameEnd();       // Play the game END flashing sequence.
  124.   }
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement