Problem in my simulation

I was asked to make a guess the number project, but I cannot understand the necessary components and how to connect them.
this is my code if there is any problem in it

// Pin definitions
const int LED_PIN = 13; // LED pin
const int BUTTON_PIN = 2; // Pushbutton pin

// Game variables
int secretNumber; // Secret number to be guessed
int attemptsRemaining = 5; // Number of attempts remaining

void setup() {
  pinMode(LED_PIN, OUTPUT); // Set LED pin as output
  pinMode(BUTTON_PIN, INPUT_PULLUP); // Set pushbutton pin as input with internal pull-up resistor
  Serial.begin(9600); // Initialize Serial Monitor
  randomSeed(analogRead(0)); // Seed the random number generator
  secretNumber = random(1, 101); // Generate a random secret number between 1 and 100
}

void loop() {
  if (attemptsRemaining > 0) {
    int guess = digitalRead(BUTTON_PIN); // Read the player's guess from the pushbutton input

    if (guess == secretNumber) {
      // Correct guess
      digitalWrite(LED_PIN, HIGH); // Blink the LED twice
      delay(500);
      digitalWrite(LED_PIN, LOW);
      delay(500);
      digitalWrite(LED_PIN, HIGH);
      delay(500);
      digitalWrite(LED_PIN, LOW);
      Serial.println("Congratulations! You guessed the number!");
      delay(3000);
       // Exit the loop
    } else if (guess > secretNumber) {
      // Guess is too high
      digitalWrite(LED_PIN, HIGH); // Blink the LED once
      delay(500);
      digitalWrite(LED_PIN, LOW);
      Serial.println("Too high! Try again.");
      delay(3000);
    } else {
      // Guess is too low
      digitalWrite(LED_PIN, HIGH); // Blink the LED once
      delay(500);
      digitalWrite(LED_PIN, LOW);
      Serial.println("Too low! Try again.");
      delay(3000);
    }

    attemptsRemaining--; // Decrement the number of attempts remaining
  } else {
    // Game over
    digitalWrite(LED_PIN, HIGH); // Blink the LED once
    delay(500);
    digitalWrite(LED_PIN, LOW);
    Serial.print("Game Over! The secret number was ");
    Serial.println(secretNumber);
     // Exit the loop
  }
}

Were you asked by an instructor? The only component I can see is a push button, and you can find a tutorial on this site, showing how to connect it. Really, in this sketch, the switch is just connected between pin 2 and GROUND.

I have to wonder, how good a simulation would be at using analogRead(0) as a random seed.

1 Like

You have guess as the state of the BUTTON_PIN which is 0 or 1. Then you compare that to
secretNumber which is from 1 to 100 . I don't think that is what you were going for.

Also 'guess' is declared inside the loop() function, the value will be undefined (i.e. discarded) every time loop() terminates.

This has the feel of a "cut and modified" program...

1 Like

Yeah, that too.

That happened when it was re-purposed.

Well, we all have to start somewhere. At least there's a forum.

1 Like

All I really had for the first few years was the BASIC language manual, a resident assembler on tape, and a 6502 data sheet. Oh, and 4k RAM. My guru became 1000's of km away, when I moved. There was no internet, and due to the remoteness of where I was, not even any mags or books.

Luckily, it's all logical, and time was really the main obstacle to getting things done.

These kids have it easy these days, lol.

This line says to connect an LED...

const int LED_PIN = 13; // LED pin

This line says to connect a pushbutton...

const int BUTTON_PIN = 2; // Pushbutton pin

This shows how to connect an LED...

This shows how to connect a pushbutton...

Then compile your code and upload it to your Arduino.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.