Help needed with button assignment

I am new to arduino and programming and Im trying to make a basic program that has 2 buttons and 2 LEDS.

This is what I want to do:

If I press Button1, L1 is ON and goes to function next()
if I press Button2, L2 is ON with L1, waits 2 seconds then turns both off and returns to the beginning of the loop.

I have tried it and only L1 comes on when Button1 is pressed and goes to the next() function but doesnt turnon L2 when Button2 is pressed and the rest of the code.

Can anyone tell me whats wrong please?

int L1 = 13;
int stateofL1 = L1;
int L2  = 12;
int Green = 11;
int Button1 = 7;
int Button2 = 6;


void setup()
{
  pinMode(L1, OUTPUT);
  pinMode(L2, OUTPUT);
  pinMode(Button1, INPUT);
  pinMode(Button2, INPUT);
}



void loop()
{
   int Press1 = digitalRead(Button1);
   int Press2 = digitalRead(Button2);
   if(Press1 == HIGH)
   {
     digitalWrite(L1, HIGH);
     next();
   }
}

void next()
{
  
  int Press2 = digitalRead(Button2);
  if(Press2 == HIGH)
  {
    digitalWrite(L2, HIGH);
    delay(2000);
    digitalWrite(L1, LOW);
    digitalWrite(L2, LOW);
  }
  
}

Try pressing button 2, and then pressing button 1, and see what happens.

It is a question of speed. The software is running too fast to detect the change in button 2. Think about something that is executing in less than a microsecond and you will understand if you trace the logic of what you have done.

I pressed Button2 then 1, Nothing happened with 2 but with 1 L1 came on. Im not really sure why its not working. If I press button1, L1 comes on and should go to next() where it should wait for button2 to be pressed and carry out the statement then go back to the start of the loop. Ive literally started looking into C programming a few days ago and cant really see why its not working.

Any suggestions?

where it should wait for button2

There's nothing in your code that says it will wait.
If you want a wait (though not a good one because it blocks), you could use a while loop.

Sorry, I should have been more precise when I said:

Try pressing button 2, and then pressing button 1, and see what happens.

I meant "Try pressing button 2 and holding it, and then pressing button 1, and see what happens."

Where exactly do you think it is waiting? It starts next(), checks for button 2. If it is not on, exits next. As per previous message, think of the speed at which this is happening.

I tried this but it didnt work.

void next()
{
  
  int Press2 = digitalRead(Button2);
  
  do
  {
      if(Press2 == HIGH)
      {
        digitalWrite(L2, HIGH);
        
      }
  }
  while(Press2 == LOW);
    
}

I'm a little confused as to what you are exactly trying to do.

If you press button 1, led 1 goes on. but if you press button 2, led 2 will only go on if led 1 is on as well?

yeah, eventually I want a 9 button keypad that takes 4 digits, checks the code and turns on green light for right or red for wrong. with every digit an LED will come on in a row of 4.

My recommendation in that you should investigate finite ste machine and implement one for this assignment. You are going down a blind alley if you want to scale this to more buttons.

I think you are making this harder than it needs to be. You can just read in the states and then see if its the right sequence. very simple

Im pulling my hair out trying to do something thats simple written down and Im not seeing how the code doesnt do what I want. Can someone have a look and see why this inst working. very frustrating.

int L1 = 13;
int L2 = 12;
int Button1 = 7;

void setup()
{
  pinMode(L1, OUTPUT);
  pinMode(L2, OUTPUT);
  pinMode(Button1, INPUT);
  Serial.begin(9600);
}

void loop()
{
  int P1 = digitalRead(Button1);
  
  do
  {
   Serial.print("press 1");
   delay(3000);
  }
  
   while(P1 == LOW);
  
  if(P1 == HIGH)
  {
    digitalWrite(L1, HIGH);
  }
}

If the switch is low when you first read it, you'll never exit the while loop.

AWOL:
If the switch is low when you first read it, you'll never exit the while loop.

Yea, The while loops needs the same syntax as an if statement.

While (##%$@^@)
{
#$%^#$^
#$^#$^@&
}

not sure what you mean, I changed while(P1 == LOW); to HIGH and still nothing

You get stuck in the loop still. The condition isn't the problem, its the code that is supposed to be executed for that while loop.
You can't have a while loop with a condition without any way for the program to leave it.

You have to have code like this

while(button1 = LOW)
{
led1 = high
}
else
{
led1 = low
}

Or something like that, You are getting into an infinite loop!

You have to have code like this

while(button1 = LOW)
{
led1 = high
}
else
{
led1 = low
}

No, you really, really don’t.
And anyway, while doesn’t have an else clause.

You need to read the pin the button is attached to inside the loop.