Need help! something wrong with my program :(

Hello, I was trying to make a mini game, I have two random number, and you have to press a button to guess if the second number is larger or smaller than the first one, if you guessed right, you continue to guess the next number, if you guessed wrong, it will tell your total guesses, I tried to make this program, but when I run it, I have to press between two buttons to let it give me the next number, I can’t press the same button twice or more, and the total guess time fixed on 0, can anyone help me with this pls? thx

Here is what I made:

/*

*/
int guessTimes;
int guessOne = random(10);
int guessTwo = random(10);
int guessCheck;
const int buttonReset = 5;
const int buttonLarge = 6;
const int buttonSmall = 7;
const int dataPin = 8;
const int latchPin = 10;
const int clockPin = 11;
int dataArray[10] = {0B11111100, 0B01100000, 0B11011010, 0B11110010, 0B01100110, 0B10110110, 0B10111110, 0B11100000, 0B11111110, 0B11110110};

void setup()
{
Serial.begin(9600);
pinMode(buttonReset,INPUT_PULLUP);
pinMode(buttonLarge,INPUT_PULLUP);
pinMode(buttonSmall,INPUT_PULLUP);
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);

}

void loop()
{
for(guessTimes = 0; guessTimes < 0; guessTimes++);
{
digitalWrite(latchPin,LOW);
shiftOut(dataPin,clockPin, MSBFIRST ,dataArray[guessOne]);
digitalWrite(latchPin,HIGH);
int guessCheck = guessOne - guessTwo;
while(digitalRead(buttonLarge) == HIGH && digitalRead(buttonSmall) == HIGH)
{}
if(digitalRead(buttonLarge) == LOW)
{
if(guessCheck <= 0)
{
guessOne = guessTwo;
guessTwo = random(10);
Serial.println(“Correct, guess next”);
}
else
{
Serial.print("Wrong, total guesses = ");
Serial.println(guessTimes);
guessTimes = -2;
}
}

if(digitalRead(buttonSmall) == LOW)
{
if(guessCheck >= 0)
{
guessOne = guessTwo;
guessTwo = random(10);
Serial.println(“Correct, guess next”);
}

else
{
Serial.print("Wrong, total guesses = ");
Serial.println(guessTimes);
guessTimes = -2;
}

}
}
}

  for (guessTimes = 0; guessTimes < 0; guessTimes++);

The semicolon is the only code dependant on the for loop. The next code block will be executed unconditionally

In addition, the for loop says
Set guessTimes to zero
While guessTimes is less than zero
execute the code dependant on the for loop (ie, the semicolon in this case)
Then increment guessTimes

How many times will the for loop run ?
How many times do you want it to run ?

UKHeliBob:

  for (guessTimes = 0; guessTimes < 0; guessTimes++);

The semicolon is the only code dependant on the for loop. The next code block will be executed unconditionally

In addition, the for loop says
Set guessTimes to zero
While guessTimes is less than zero
execute the code dependant on the for loop (ie, the semicolon in this case)
Then increment guessTimes

How many times will the for loop run ?
How many times do you want it to run ?

I want it to loop as many times as the user want, as long as their guess is right
And now after delete that semicolon my program is not working anymore :frowning:

I want it to loop as many times as the user want, as long as their guess is right

Then don't use a for loop. Let the loop() function do the looping

UKHeliBob:
Then don't use a for loop. Let the loop() function do the looping

Thank you, I deleted that, now I just have to figure out how to make this program run once when the button is pressed, right now it just keep running when I keep one of the button pressed

Okay thank you for those helped, I figuered I just need to make a little delay for debouncing the button :slight_smile:

better than a delay()
Use a state-change to determine when the button is up or down
the it only responds when the input changes from one to the other, not all the time the button is pressed