Advertisement
pleasedontcode

"Interactive Game" rev_02

Feb 22nd, 2025
383
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: "Interactive Game"
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2025-02-22 15:13:42
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The LCD shows a random target (100-900). Adjust */
  21.     /* the potentiometer to match it. A 10s timer starts. */
  22.     /* Press the button to check. If matched, "You Win!" */
  23.     /* Else, "Try Again!" If time runs out, "Time's Up!" */
  24.     /* Press to restart. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  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. const uint8_t buttonPin = 6; // button pin
  41.  
  42. /***** DEFINITION OF ANALOG INPUT PINS *****/
  43. const uint8_t Buzzer_Potentiometer_Vout_PIN_A0      = A0;
  44. const uint8_t potPin = A0; // potentiometer pin (this is already defined above)
  45.  
  46. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  47. const uint8_t Leds_LED_PIN_D3       = 3;
  48. const uint8_t Standard_LCD1602_D5_PIN_D7        = 7; // buzzer pin is defined below
  49. const uint8_t Passive_PassiveBuzzer_Signal_PIN_D10      = 10;
  50.  
  51. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  52. /***** used to store raw data *****/
  53. bool    Leds_LED_PIN_D3_rawData     = 0;
  54. bool    Standard_LCD1602_D5_PIN_D7_rawData      = 0;
  55. bool    Passive_PassiveBuzzer_Signal_PIN_D10_rawData        = 0;
  56.  
  57. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  58. /***** used to store data after characteristic curve transformation *****/
  59. float   Leds_LED_PIN_D3_phyData     = 0.0;
  60. float   Standard_LCD1602_D5_PIN_D7_phyData      = 0.0;
  61. float   Passive_PassiveBuzzer_Signal_PIN_D10_phyData        = 0.0;
  62.  
  63. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  64. // Initialize the LCD pins
  65. LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // This line is compatible with the LiquidCrystal library
  66.  
  67. int targetValue; // variable to store the target value
  68. int currentValue; // variable to store the current value of the potentiometer
  69. int buzzerPin = 7; // buzzer pin (this conflicts with Standard_LCD1602_D5_PIN_D7)
  70. int timer = 10000; // timer in milliseconds
  71. unsigned long startTime; // variable to store the start time of the game
  72. bool gameStarted = false; // variable to track if the game has started
  73.  
  74. void setup(void)
  75. {
  76.     // put your setup code here, to run once:
  77.  
  78.     pinMode(Button_PushButton_PIN_D2,   INPUT_PULLUP);
  79.     pinMode(Buzzer_Potentiometer_Vout_PIN_A0,   INPUT);
  80.  
  81.     pinMode(Leds_LED_PIN_D3,     OUTPUT);
  82.     pinMode(Standard_LCD1602_D5_PIN_D7,  OUTPUT);
  83.     pinMode(Passive_PassiveBuzzer_Signal_PIN_D10,    OUTPUT);
  84.    
  85.     // USER CODE START
  86.     lcd.begin(16, 2); // initialize the LCD with 16 columns and 2 rows
  87.     randomSeed(analogRead(0)); // seed the random number generator with a random value from the potentiometer
  88.     targetValue = random(100, 900); // generate a random target value between 100 and 900
  89.     lcd.print("Target: "); // print the target label on the LCD
  90.     lcd.print(targetValue); // print the target value on the LCD
  91.     pinMode(buttonPin, INPUT); // set the button pin as input
  92.     pinMode(buzzerPin, OUTPUT); // set the buzzer pin as output
  93.     // USER CODE END
  94. }
  95.  
  96. void loop(void)
  97. {
  98.     // put your main code here, to run repeatedly:
  99.     updateOutputs(); // Refresh output data
  100.  
  101.     // USER CODE START
  102.     if (!gameStarted) { // if the game has not started
  103.         currentValue = analogRead(potPin); // read the current value of the potentiometer
  104.         lcd.setCursor(0, 1); // set the cursor to the second row of the LCD
  105.         lcd.print("Current: "); // print the current label on the LCD
  106.         lcd.print(currentValue); // print the current value on the LCD
  107.     }
  108.  
  109.     if (digitalRead(buttonPin) == HIGH) { // if the button is pressed
  110.         if (gameStarted) { // check if the game has started
  111.             if (currentValue == targetValue) { // if the current value matches the target value
  112.                 lcd.clear(); // clear the LCD
  113.                 lcd.print("You Win!"); // print "You Win!" on the LCD
  114.                 digitalWrite(buzzerPin, HIGH); // turn on the buzzer
  115.                 delay(1000); // wait for 1 second
  116.                 digitalWrite(buzzerPin, LOW); // turn off the buzzer
  117.             } else { // if the current value does not match the target value
  118.                 lcd.clear(); // clear the LCD
  119.                 lcd.print("Try Again!"); // print "Try Again!" on the LCD
  120.             }
  121.             gameStarted = false; // set gameStarted to false to start a new game
  122.         } else { // if the game has not started
  123.             startTime = millis(); // store the current time as the start time
  124.             gameStarted = true; // set gameStarted to true to indicate the game has started
  125.         }
  126.     }
  127.  
  128.     if (gameStarted && (millis() - startTime >= timer)) { // if the timer has run out
  129.         lcd.clear(); // clear the LCD
  130.         lcd.print("Time's Up!"); // print "Time's Up!" on the LCD
  131.         gameStarted = false; // set gameStarted to false to start a new game
  132.     }
  133.     // USER CODE END
  134. }
  135.  
  136. void updateOutputs()
  137. {
  138.     digitalWrite(Leds_LED_PIN_D3, Leds_LED_PIN_D3_rawData);
  139.     digitalWrite(Standard_LCD1602_D5_PIN_D7, Standard_LCD1602_D5_PIN_D7_rawData);
  140.     digitalWrite(Passive_PassiveBuzzer_Signal_PIN_D10, Passive_PassiveBuzzer_Signal_PIN_D10_rawData);
  141. }
  142.  
  143. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement