Advertisement
pleasedontcode

**Game Interface** rev_01

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