Using millis() to give an input time window

Hi all!

I am trying to write a project that displays a number to my OLED screen, and then takes the binary input of the user and compares it to this number. I am using a WeMos D1 R1 board, and one of the extension boards with 7-segment displays/LEDs/8 buttons.

I have got all of the aspects of the code to work individually, in particular a random number generator and the button input that converts the binary number being inputted into an int value. The problem I’m having is that I want to set a time limit to how long the user has to input the binary number. Before I was using delay(), which would only read the input if I pressed it at the right time, a small window either just before or after the detay function. I’m trying to use millis() to time this instead.

I’ve played around with the code lots, and one of two things tends to happen:
It either flies through the numbers at lightning speed, or it goes through at the pace that I would want it to go through, but doesn’t register my input. Any help or direction is greatly appreciated! I haven’t included bits of code that I don’t think are relevant (ie setting up the font size for the display, the games instruction manual for the user, etc.).

// To set up the button input:

byte buttons;


#define TM1638_STB D5
#define TM1638_CLK D6
#define TM1638_DIO D7


TM1638 module(TM1638_DIO, TM1638_CLK, TM1638_STB);


// to set up the timer
unsigned long startTimer; 
const unsigned long timePeriod = 5000;


int screenNumber = 0;     // The number that will show up on screen
int binNumber = 0;                 // The number that the user will input


void setup()
{ 
randomSeed(analogRead(0)); // Used to set up a random number generator
}


void loop()
{
buttons=module.getButtons(); // Get the input from the extension board
display.clearDisplay();
display.setCursor(0,0);
screenNumber = random(1, levelUp);   // Randomly generate a number
display << "Number:  " << screenNumber << endl;
display.display();
display << "You chose: ";


startTimer = millis();


binNumber = buttons;
    
while(millis() > startTimer && millis() < (startTimer + timePeriod))
{
display << binNumber << endl;
display.display();
delay(3000);
}

Thanks in advance for any help!!

Does the snippet of youd full code work as you described?

I see Only you are in the while() or not

The way to program a timeout is like this pseudo code

void loop() 
   if (questionState = 'I') {
      code to ask the question
      questionState = 'W'; // W for Waiting  A for answered I = idle
      timeQuestionAsked = millis();
   }

   if (millis() - timeQuestionAsked >= maxPermittedTime)
       questionState = 'I'; // back to idle for anotther try
       Serial.println("You were too slow");
   }

   if (questionState == 'W') {
     code to take the answer
     if answer arrives {
          questionState = 'A'
     }
   }

   if (questionState == 'A') {
    code to check the answer
    questionState = 'I'; // to start over again
   }
}

...R