Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: **Color Game**
- - Source Code NOT compiled for: Arduino Uno
- - Source Code created on: 2024-11-29 14:21:10
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* there is a problem, when I tried this program on */
- /* physical Arduino, the color writings got stuck */
- /* after 2 turns, and also to switch from one color */
- /* to another there is a bug that consists of */
- /* pressing countless times the buttons corresponding */
- /* to the colo */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <LiquidCrystal.h> //https://github.com/arduino-libraries/LiquidCrystal
- #include <EasyButton.h> //https://github.com/evert-arias/EasyButton
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t button_PushButton_PIN_D2 = 2;
- // Additional button pins
- const uint8_t yellowButton = 6;
- const uint8_t greenButton = 7;
- const uint8_t blueButton = 8;
- const uint8_t redButton = 9;
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // Initialize the LCD
- // Variables for timing
- int inter = A0; // This pin is defined but not used in the provided code
- int valint = 0;
- // Time variables
- unsigned long startTime;
- unsigned long endTime;
- unsigned long elapsedTime;
- unsigned long totalElapsedTime = 0; // Total elapsed time
- // Color definitions
- int yellow = 0;
- int green = 1;
- int blue = 2;
- int red = 3;
- int currentColor;
- int buttonPressCount = 0; // Button press counter
- // Button state variables
- bool yellowPressed = false;
- bool greenPressed = false;
- bool bluePressed = false;
- bool redPressed = false;
- void setup(void)
- {
- // put your setup code here, to run once:
- pinMode(button_PushButton_PIN_D2, INPUT_PULLUP);
- // Initialize the LCD
- lcd.begin(16, 2);
- // Set the switch pin
- pinMode(inter, INPUT);
- // Define button pins
- pinMode(yellowButton, INPUT_PULLUP);
- pinMode(greenButton, INPUT_PULLUP);
- pinMode(blueButton, INPUT_PULLUP);
- pinMode(redButton, INPUT_PULLUP);
- // Set the random seed
- randomSeed(analogRead(0));
- }
- void loop(void)
- {
- // put your main code here, to run repeatedly:
- if (buttonPressCount >= 10) { // Check if the count has reached 10
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Game Over");
- // Show total time on the display
- lcd.setCursor(0, 1);
- lcd.print("Total: ");
- lcd.print(totalElapsedTime);
- lcd.print(" ms");
- // Wait for a button to be pressed to restart
- while (digitalRead(yellowButton) == HIGH &&
- digitalRead(greenButton) == HIGH &&
- digitalRead(blueButton) == HIGH &&
- digitalRead(redButton) == HIGH) {
- // Wait until a button is pressed
- }
- // Restart the game
- buttonPressCount = 0; // Reset the counter
- totalElapsedTime = 0; // Reset total time
- lcd.clear();
- lcd.print("Restarting...");
- delay(1000);
- }
- // Generate a random number between 0 and 3
- currentColor = random(0, 4);
- // Show the current color on the LCD
- lcd.clear();
- if (currentColor == yellow) {
- lcd.print("Yellow");
- } else if (currentColor == green) {
- lcd.print("Green");
- } else if (currentColor == blue) {
- lcd.print("Blue");
- } else if (currentColor == red) {
- lcd.print("Red");
- }
- // Start the timer
- startTime = millis();
- // Wait for the corresponding button to be pressed
- while (true) {
- if (currentColor == yellow && digitalRead(yellowButton) == LOW) {
- yellowPressed = true;
- } else if (currentColor == green && digitalRead(greenButton) == LOW) {
- greenPressed = true;
- } else if (currentColor == blue && digitalRead(blueButton) == LOW) {
- bluePressed = true;
- } else if (currentColor == red && digitalRead(redButton) == LOW) {
- redPressed = true;
- }
- // Break the loop if the correct button is pressed
- if (yellowPressed || greenPressed || bluePressed || redPressed) {
- break;
- }
- }
- // Stop the timer
- endTime = millis();
- // Calculate the elapsed time for this press and add it to the total time
- elapsedTime = endTime - startTime;
- totalElapsedTime += elapsedTime;
- // Increment the button press counter
- buttonPressCount++;
- // Reset button state variables
- yellowPressed = false;
- greenPressed = false;
- bluePressed = false;
- redPressed = false;
- // Delay to avoid overlaps
- delay(1000);
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement