Storing input from TM1638 buttons to a variable

Hi all!

I am trying to make a small project using a WeMos D1 R1 with a TM1638 8 button extension board and an OLED screen.

The project involves a random number generator that prints a number to the screen, and then the user has to input this number in binary on the extension board buttons. This is in a for loop so that every x iterations, an extra bit is added to the number that needs to be guessed (but this is secondary to the problem I’m having, just trying to put as much information as possible!) I’ve been going around for days trying to figure this out, running into problem after problem. I can read the the input and store it as an int, but I think what seems to be happening is that it doesn’t read the input/change the int value until the next iteration of the for loop, by which time it’s already generated a new number that needs to be guessed, or possibly it only reads the button input if I’m holding it at the beginning of the for loop, and then occasionally it just doesn’t seem to read the input at all!

I really appreciate any help or guidance in the right direction.

The code is:

// some stuff here to set up the screen, libraries etc

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


TM1638 module(TM1638_DIO, TM1638_CLK, TM1638_STB);


int screenNumber = 0;      // The number that will show up on screen
int binNumber;             // The number that the user will input
int levelUp = 4;           // The maximum number that will show up on screen
int levelNumber = 1;       // The level the player is on. Each new level adds 
                           // another binary bit

bool buttonIsPressed = 0;  // Set to 1 when a button on the board is pressed


void setup()
{
   // game instructions, intro screen etc here


 display.clearDisplay();
 display.setCursor(0,0);
 display.setTextSize(1);
 display.setTextColor(WHITE);   // set the text size and color


randomSeed(analogRead(0)); // Used for the random number generator
}

void loop()
{
for (int i = 1; i < 6 ; i++)
  {
    buttonIsPressed = 0;    
    binNumber = 0;               // Sets the input number back to 0 from previous iteration    
    display.clearDisplay();
    display.setCursor(0,0);
    display << "Level " << levelNumber << endl;
    screenNumber = random(1, levelUp);        // Generate a random number, show it on screen
    display << "Number " << i << ": " << screenNumber << endl;
    display.display();


    if (module.getButtons())            // When a button on the extension board is pressed
    {
       binNumber = module.getButtons();    // Set the user input number
       buttonIsPressed = 1;                // Sets that the button is pressed
    }
    
    delay(5000);
    
    if (buttonIsPressed == 1)
    {
      display << "Your number: " << binNumber << endl;
      display.display();

      if (binNumber == screenNumber)
      {
        // Well done, moves to next iteration, score counter increment etc
      }

      else if (binNumber != screenNumber)
      {
        // Game over
      }
    }
  }

// Move up a level after the iterations of the for loop
levelUp = levelUp * 2;
levelNumber++;

}