4 buttons, each button prints a sequence of 4 bits. Need some guidance.

I have 4 buttons (pull-up), I want each button to transmit one sequence of 4 bits.
(ie. button1=0001, button2=0010)

I made something that seems to work, but it's not as responsive as I would like. Does anyone have any suggestions?

int button_1 = 5;
int button_2 = 6;
int button_3 = 7;
int button_4 = 8;
int bit_1;
int bit_2;
int bit_3;
int bit_4;
int debounceTime=20;
int lastButtonState = 1;
boolean function_executed = false;
long unsigned int lastPress;


//my four digit print function
//button variable selects which button to listen to
//b1,b2,b3,b4 are the four bits of data

void print_Binary(int button, int b1, int b2, int b3, int b4){
  
  int buttonState = digitalRead(button);
  
  if((millis() - lastPress) > debounceTime){
    lastPress = millis();
    if(buttonState == 0 && lastButtonState == 1 && function_executed == false){

      bit_1 = b1; 
      bit_2 = b2; 
      bit_3 = b3; 
      bit_4 = b4;
      Serial.print(bit_1);Serial.print(bit_2);Serial.print(bit_3);Serial.println(bit_4);
      
      lastButtonState = 0;
      function_executed = true;

    }
    
//function won't loop until all buttons are released  

      if(digitalRead(button_1) == 1 && digitalRead(button_2) == 1 && digitalRead(button_3) == 1 && digitalRead(button_4) == 1){
      function_executed = false;
      lastButtonState = 1;
}
}
}

void setup() {

Serial.begin(9600);
pinMode(button_1, INPUT);
pinMode(button_2, INPUT);
pinMode(button_3, INPUT);
pinMode(button_4, INPUT);

}

void loop() {

print_Binary(button_1, 0, 0, 0, 1);
print_Binary(button_2, 0, 0, 1, 0);
print_Binary(button_3, 0, 0, 1, 1);
print_Binary(button_4, 0, 1, 0, 0);
  
}

Eventually I want this sequence to be written to the EEPROM and stored as a passcode.

Thank you very much!

seems like print_Binary() needs to wait for a specific button press before printing 4 values and then wait for the button to be released.

both a button press and release can be detected by waiting for a change in button state. A press or release depends on the state of the button after a change.

a simple way to debounce a button is to wait, delay (10) after a change. Another questions is do the button inputs need to be configured with pullups, INPUT_PULLUP.

so wait (while) for a button change and the button is the pressed state (LOW ?), print your values and the again wait for a button change in the released state (HIGH ?) Do you really need to all buttons to be un-pressed?

Thank you, it works great and is much simpler.

int button_1 = 5;
int button_2 = 6;
int button_3 = 7;
int button_4 = 8;
int bit_1;
int bit_2;
int bit_3;
int bit_4;
int lastButtonState = 1;
boolean function_executed = false;
long unsigned int lastPress;
int buttonState = 1;

void print_Binary(int button, int b1, int b2, int b3, int b4){
  
  int buttonState = digitalRead(button);
  while(buttonState == 0){
      bit_1 = b1; 
      bit_2 = b2; 
      bit_3 = b3; 
      bit_4 = b4;
      Serial.print(bit_1);Serial.print(bit_2);Serial.print(bit_3);Serial.println(bit_4);
  
  while(buttonState == 0){
      buttonState = digitalRead(button);
      delay(10);
    }
  }
}

void setup() {

Serial.begin(9600);
pinMode(button_1, INPUT_PULLUP);
pinMode(button_2, INPUT_PULLUP);
pinMode(button_3, INPUT_PULLUP);
pinMode(button_4, INPUT_PULLUP);

}

void loop() {

print_Binary(button_1, 0, 0, 0, 1);
print_Binary(button_2, 0, 0, 1, 0);
print_Binary(button_3, 0, 0, 1, 1);
print_Binary(button_4, 0, 1, 0, 0);
  
}

Hope this is right.