Advertisement
pleasedontcode

**Color Game** rev_01

Nov 29th, 2024
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: **Color Game**
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-11-29 14:21:10
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* there is a problem, when I tried this program on */
  21.     /* physical Arduino, the color writings got stuck */
  22.     /* after 2 turns, and also to switch from one color */
  23.     /* to another there is a bug that consists of */
  24.     /* pressing countless times the buttons corresponding */
  25.     /* to the colo */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28. /* START CODE */
  29.  
  30. /****** DEFINITION OF LIBRARIES *****/
  31. #include <LiquidCrystal.h>  //https://github.com/arduino-libraries/LiquidCrystal
  32. #include <EasyButton.h> //https://github.com/evert-arias/EasyButton
  33.  
  34. /****** FUNCTION PROTOTYPES *****/
  35. void setup(void);
  36. void loop(void);
  37.  
  38. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  39. const uint8_t button_PushButton_PIN_D2      = 2;
  40.  
  41. // Additional button pins
  42. const uint8_t yellowButton = 6;
  43. const uint8_t greenButton = 7;
  44. const uint8_t blueButton = 8;
  45. const uint8_t redButton = 9;
  46.  
  47. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  48. LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // Initialize the LCD
  49.  
  50. // Variables for timing
  51. int inter = A0; // This pin is defined but not used in the provided code
  52. int valint = 0;
  53.  
  54. // Time variables
  55. unsigned long startTime;
  56. unsigned long endTime;
  57. unsigned long elapsedTime;
  58. unsigned long totalElapsedTime = 0; // Total elapsed time
  59.  
  60. // Color definitions
  61. int yellow = 0;
  62. int green = 1;
  63. int blue = 2;
  64. int red = 3;
  65.  
  66. int currentColor;
  67. int buttonPressCount = 0; // Button press counter
  68.  
  69. // Button state variables
  70. bool yellowPressed = false;
  71. bool greenPressed = false;
  72. bool bluePressed = false;
  73. bool redPressed = false;
  74.  
  75. void setup(void)
  76. {
  77.     // put your setup code here, to run once:
  78.     pinMode(button_PushButton_PIN_D2, INPUT_PULLUP);
  79.  
  80.     // Initialize the LCD
  81.     lcd.begin(16, 2);
  82.  
  83.     // Set the switch pin
  84.     pinMode(inter, INPUT);
  85.  
  86.     // Define button pins
  87.     pinMode(yellowButton, INPUT_PULLUP);
  88.     pinMode(greenButton, INPUT_PULLUP);
  89.     pinMode(blueButton, INPUT_PULLUP);
  90.     pinMode(redButton, INPUT_PULLUP);
  91.  
  92.     // Set the random seed
  93.     randomSeed(analogRead(0));
  94. }
  95.  
  96. void loop(void)
  97. {
  98.     // put your main code here, to run repeatedly:
  99.  
  100.     if (buttonPressCount >= 10) { // Check if the count has reached 10
  101.         lcd.clear();
  102.         lcd.setCursor(0, 0);
  103.         lcd.print("Game Over");
  104.  
  105.         // Show total time on the display
  106.         lcd.setCursor(0, 1);
  107.         lcd.print("Total: ");
  108.         lcd.print(totalElapsedTime);
  109.         lcd.print(" ms");
  110.  
  111.         // Wait for a button to be pressed to restart
  112.         while (digitalRead(yellowButton) == HIGH &&
  113.                digitalRead(greenButton) == HIGH &&
  114.                digitalRead(blueButton) == HIGH &&
  115.                digitalRead(redButton) == HIGH) {
  116.             // Wait until a button is pressed
  117.         }
  118.  
  119.         // Restart the game
  120.         buttonPressCount = 0;      // Reset the counter
  121.         totalElapsedTime = 0;      // Reset total time
  122.         lcd.clear();
  123.         lcd.print("Restarting...");
  124.         delay(1000);
  125.     }
  126.  
  127.     // Generate a random number between 0 and 3
  128.     currentColor = random(0, 4);
  129.  
  130.     // Show the current color on the LCD
  131.     lcd.clear();
  132.     if (currentColor == yellow) {
  133.         lcd.print("Yellow");
  134.     } else if (currentColor == green) {
  135.         lcd.print("Green");
  136.     } else if (currentColor == blue) {
  137.         lcd.print("Blue");
  138.     } else if (currentColor == red) {
  139.         lcd.print("Red");
  140.     }
  141.  
  142.     // Start the timer
  143.     startTime = millis();
  144.  
  145.     // Wait for the corresponding button to be pressed
  146.     while (true) {
  147.         if (currentColor == yellow && digitalRead(yellowButton) == LOW) {
  148.             yellowPressed = true;
  149.         } else if (currentColor == green && digitalRead(greenButton) == LOW) {
  150.             greenPressed = true;
  151.         } else if (currentColor == blue && digitalRead(blueButton) == LOW) {
  152.             bluePressed = true;
  153.         } else if (currentColor == red && digitalRead(redButton) == LOW) {
  154.             redPressed = true;
  155.         }
  156.  
  157.         // Break the loop if the correct button is pressed
  158.         if (yellowPressed || greenPressed || bluePressed || redPressed) {
  159.             break;
  160.         }
  161.     }
  162.  
  163.     // Stop the timer
  164.     endTime = millis();
  165.  
  166.     // Calculate the elapsed time for this press and add it to the total time
  167.     elapsedTime = endTime - startTime;
  168.     totalElapsedTime += elapsedTime;
  169.  
  170.     // Increment the button press counter
  171.     buttonPressCount++;
  172.  
  173.     // Reset button state variables
  174.     yellowPressed = false;
  175.     greenPressed = false;
  176.     bluePressed = false;
  177.     redPressed = false;
  178.  
  179.     // Delay to avoid overlaps
  180.     delay(1000);
  181. }
  182.  
  183. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement