Trying to program a specific code which "unlocks" my led

Hello, i just bought myself an arduino and i’m trying to do different exercises.
I’m actually blocked on one where i have a red led and a green led with 2 push buttons. I want the red led to be ON by default and the green led to be OFF. I have to make a combinaison with my butons to turn on my green led. exemple : push left buton, then push the two buttons, then push the right buton and then the left button again.

Here’s the way i made it :


( the Leds are not in the right direction but i didn’t find how to rotate it :confused: :confused: )

And here is my code (i made a boolean board with 4 elements which are initialized as false)
Each time i press the right button we go to the next while loop and put a true on the case of the board. If i make the correct combination, my green led is ON and if i press the left button again my board is set to false for each case and my red led should turn ON again.

#include <stdio.h> 
#include <stdlib.h>
   
int pin_du_bouton_gauche = 5; // left button pin
int pin_du_bouton_droit = 4;   // right button pin
int i;

boolean tableau[3];

int pin_led_rouge = 2;  // red led pin
int pin_led_verte = 3;   // green led pint
 

void  setup(){

    pinMode(pin_du_bouton_gauche,  INPUT); 
    pinMode(pin_du_bouton_droit,  INPUT);             
    pinMode(pin_led_rouge,  OUTPUT);  
    pinMode(pin_led_verte,  OUTPUT);  
    tableau[0]=false;
    tableau[1]=false;
    tableau[2]=false;
    tableau[3]=false;
    
    
}
                                                                       
void  loop()  {
  digitalWrite(pin_led_rouge, HIGH);
  
  while(tableau[0] == false){
    if(digitalRead(pin_du_bouton_gauche) == HIGH && digitalRead(pin_du_bouton_droit == LOW)){
      tableau[0]=true;
    }}

  while(tableau[1] == false){
    if(digitalRead(pin_du_bouton_gauche) == LOW && digitalRead(pin_du_bouton_droit == HIGH)){
      tableau[1]=true;
    }}
  while(tableau[2] == false){
    if(digitalRead(pin_du_bouton_gauche) == LOW && digitalRead(pin_du_bouton_droit == HIGH)){
      tableau[2]=true;
    }}
  while(tableau[3] == false){
    if(digitalRead(pin_du_bouton_gauche) == HIGH && digitalRead(pin_du_bouton_droit == LOW)){
      tableau[3]=true;
    }}

  digitalWrite(pin_led_rouge, LOW);
  digitalWrite(pin_led_verte, HIGH);

  
  while(tableau[0]==true && tableau[1]==true && tableau[2]==true && tableau[3]==true){
    if(digitalRead(pin_du_bouton_droit) == HIGH && digitalRead(pin_du_bouton_droit == LOW)){
        for(i=0;i<=3;i++){
          tableau[i] = false;}}
        }

}

My code doesn’t work on my Arduino and i tried for hours to resolve it. My red led is always turned on and i can’t resolve it ! Thanks in advance for your answers !

boolean tableau[3];

This is a 3 element array, with indices from 0 to 2.

    tableau[0]=false;
    tableau[1]=false;
    tableau[2]=false;
    tableau[3]=false;

You just walked off the end of the array.

  while(tableau[0] == false){
    if(digitalRead(pin_du_bouton_gauche) == HIGH && digitalRead(pin_du_bouton_droit == LOW)){
      tableau[0]=true;
    }}

EVERY } goes on a new line BY ITSELF. I prefer that every { be on a line by itself.

My code doesn't work on my Arduino

If, after fixing these issues, the code still doesn't work, for whatever your definition of work is, you need to describe what it actually does and how that differs from what you expect.

I'd ditch the external resistors, use INPUT_PULLUP for the mode, and expect HIGH to mean released and LOW to mean pressed.

-> ok so when i create a boolean array with boolean tableau[3]
I'll have tableau[0], tableau[1] and tableau[2] ?

i also put the { correctly.
Then :
pinMode(pin_du_bouton_gauche, INPUT_PULLUP);
pinMode(pin_du_bouton_droit, INPUT_PULLUP);

and changed all my HIGH by LOW and LOW by high. (the ones which were related to my buttons.)
I also put a print in every while loop. No i see that when i launch my arduino without pressing any button i directly enter in the 3rd loop, like if he didn't cared about the press of the buttons. for exemple, he passes throught the tow first conditions :

if(digitalRead(pin_du_bouton_gauche) == LOW && digitalRead(pin_du_bouton_droit == HIGH))
if(digitalRead(pin_du_bouton_gauche) == LOW && digitalRead(pin_du_bouton_droit == LOW))
like if he only cared about the part before the &&

pyjama11:
-> ok so when i create a boolean array with boolean tableau[3]
I'll have tableau[0], tableau[1] and tableau[2] ?

Yes, correctly.

All arrays in the C/C++ programming language are zero based.
So if you define an array with 3 elements, valid array index is then from 0 to 2.
Lowest array index: always 0
Highest array index: number of elements defined minus one
If you access your array with an invalid index, you create a "buffer overflow" and access memory locations in RAM, that don't belong to the array you defined.

if(digitalRead(pin_du_bouton_gauche) == LOW && digitalRead(pin_du_bouton_droit == HIGH))

should be:

if(digitalRead(pin_du_bouton_gauche) == LOW && digitalRead(pin_du_bouton_droit) == HIGH)

Thank jurs for this, i now remember i already saw it on the net but i forgot that there was the [0] as first value !

aarg, i think i have to thank you... I am so ashamed, i was thinking on my program for hours because of this little ')' which was bad placed on my condition.. Thank you !!!!!!!!!!!

Now it works ! thank to all of you !