How to make the loop wait for user input and then continue?

Hello there, I am learning C language for 6-7 months now and decided to start using Arduino. My goal now is to make a game on the arduino, which I have already made in C. The game is simple: depending on the number of attempts, the program generates a random number in a range - 1 attempt-[0;5] 2 attempts-[0;10] 3 attempts-[0;15]. There are three different keys which correspond to the number of attempts - 1st key-1, 2nd key-2, 3rd key-3. I have declared all my buttons and an intro message which is being displayed at the lcd screen I have in the beginning of the game and then when the void loop() starts, I want to make the program to wait for user input (i.e. button press). I have tried different variations of code, but none of them actually did what I want. For example:

if(i=5;i>0;i--){
    lcd.print("Hold a button now");

    if(button1=high){
        counter=1;
    }
    if(button2=high){
        counter=2;
    }       
    if(button3=high){
        counter=3;
    }

    delay(1000);
}
lcd.print("You have selected");
ldc.print(counter);
lcd.print("attempts.");

That is actually working, but if you hold a button WHILE the for loop is going on, the counter will remain with its initialized value (i.e. 0). If you hold the button before the loop actually starts going on and you don't hold it anymore while the loop is going the counter is changing. My conclusion is that the way arduino works is checking for some specific thing(if a button is pressed or no) only in the first time the for loop is working. I have tried another thing like

lcd.print("You have 5 seconds, hold a key now")
delay(4000);

if(button1=high){
        counter=1;
    }
    if(button2=high){
        counter=2;
    }       
    if(button3=high){
        counter=3;
    }
delay(1000);

lcd.print("The time has passed!");
lcd.clear();
lcd.print("You have selected");
ldc.print(counter);
lcd.print("attempts.");

which is also working in the same way. I have also tried some variations of while loops and if-else if-else structures but can't make that. Any ideas how I can make that?

P.S. English is not my first language, so I am sorry if there are some grammatical mistakes and if something is not clear I will try to restructure my sentences. Also, I can post the arduino code or the C code of the game if needed. Thank you all in advance!

according to your first coding. in the for loop. i’d prefer you to use “if else” function instead of “if” function. and then for the “else” part. you may put this.
else{
i++;
}
so that if there’s no input, it will still loop but the value of ‘i’ still remains.

Please post the entire program and not just a snippet. For example, there is no way to know what 'high' means.

I do know that this

if (button1=high)

sets button1 to high. The equality operator is TWO equal signs!

vaj4088:

I know these things, sorry for the mistakes, I wrote these codes now just for example because I dont have all of the versions I have tried saved somewhere. high (actually HIGH) means the state of the button (if its pressed or not). Here is the code:

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int button1=0;
int button2=0;
int button3=0;
int button4=0;

int i=0;
int counter=0;

void setPins(){
    pinMode(7, INPUT);
    pinMode(8, INPUT);
    pinMode(9, INPUT);
    pinMode(10, INPUT);
    Serial.begin(9600);
    lcd.begin(16, 2);
}

void message(int period){
    lcd.print("Hello, guess");
      lcd.setCursor(0,1);
    lcd.print("the numbers!");
      delay(period);
      lcd.clear();
    lcd.print("According to the");
      lcd.setCursor(0,1);
    lcd.print("attemps, the numbers are");
      delay(period);
      lcd.clear();
    lcd.print("in the following ");
      lcd.setCursor(0, 1);
    lcd.print("intervals:");
      delay(period);
      lcd.clear();
    lcd.print("1-[0;5] 2-[0;10]");
      lcd.setCursor(0,1);
    lcd.print("    3-[0;15]");
      delay(period);
      lcd.clear();
    lcd.print("Choose how many");
      lcd.setCursor(0,1);
    lcd.print("attemps you want.");
      delay(period);  
}

void initialize(){
    button1=digitalRead(7);
    button2=digitalRead(8);
    button3=digitalRead(9);
    button4=digitalRead(10); 

    Serial.print(button1);
    Serial.print(button2);
    Serial.print(button3);
    Serial.print(button4);
    Serial.println();
}
  
void setup() {
    setPins();
    message(3000);  
}

void loop() {
   initialize();
   delay(2000);  
}

The loop part is empty because I don’t really have any working variation for what I want to do.

If you just want the program to wait for a button press before it starts you can put something like this in setup()

while (digitalRead(buttonPin) == HIGH) {
}

This assumes the use of INPUT_PULLUP

However if you want to get user input in the course of the program you should not be thinking in terms of "making the loop wait". Have a look at how user input is done in Planning and Implementing a Program

It is very unlikely that you will have a successful game program if you use the delay() function. That link also illustrates how to use millis() to manage timing without blocking.

In general your goal should be to allow the loop() function to repeat hundreds or thousands of times per second.

...R

+1 for what Robin2 wrote.