Simultaneous pushes on input buttons

Hello everybody.

I found an interesting schematic on internet(see attached file) that works very well.
Plugged to an analog read, four buttons activate their following OUTPUT. (S1 to Led1, S2 to Led2, etc...)

That works great but there is a major default , when pushing 2 or more buttons simultaneously the following inputs are ON, so several outputs are ON.

My question is, how to prevent several inputs to be activated as i want only one input (The first activated) to be ON even simultaneous pushing on 2 or more buttons.

Here is the code, maybe a function is enough i don't know.

int lcd_key     = 0;
int analog  = 0;
        

int read_buttons(){               // read the buttons
    analog = analogRead(0);       // read the analog input connected to pin 0


    if (analog > 1000) return 0; 

    
    if (analog < 50)   return 1;  
    if (analog < 250)  return 2; 
    if (analog < 450)  return 3; 
    if (analog < 650)  return 4; 
   

    return 0;                // when all others fail, return this.
}

void setup(){
   pinMode(12, OUTPUT);
   pinMode(11, OUTPUT); 
   pinMode(10, OUTPUT); 
   pinMode(9, OUTPUT);  
}
 
void loop(){
   
   lcd_key = read_buttons();   // read the buttons

   switch (lcd_key){               

       case 1:{             
            digitalWrite(12, HIGH);
            break;
       }
       case 2:{
             digitalWrite(11, HIGH);
             break;
       }    
       case 3:{
             digitalWrite(10, HIGH);
             break;
       }
       case 4:{
             digitalWrite(9, HIGH);
             break;
       }
       case 0:{
             digitalWrite(12, LOW);
             digitalWrite(11, LOW);
             digitalWrite(10, LOW);
             digitalWrite(9, LOW);
             break;
       }
       
   }
}

Check if lcd_key or read_button is 0, like this:

int rb = read_buttons();
if ((lcd_key == 0) || (rb == 0)) lcd_key = rb;

EDIT: First suggestion would have blocked button releases..

the first button pushed works but after it's all blocked. the first led stays on and the other buttons make Nothing.

OP's Diagram.

Mouldolas:
the first button pushed works but after it's all blocked. the first led stays on and the other buttons make Nothing.

Sorry, I made a bummer and edited my previous post to correct it :slight_smile:

Thank you very much. It works.

Can you explain a little how these two lines work.
I'm totally new and i need to understand.

That's OK I understood.

Thank you.

Mouldolas:
Thank you very much. It works.

Can you explain a little how these two lines work.
I'm totally new and i need to understand.

When lcd_key is 0 then a button press is allowed and when rb is 0 then all buttons have been released, in both cases it is allowed to modify the value of lcd_key. "(lcd_key==0)||(rb==0)" can be translated to this pseudocode which may be easier to understand:

if (lcd_key is zero) or (rb is zero) then lcd_key may be set to value of rb