trouble reading two buttons correctly

Hi, I'm trying to read two buttons one after the other like this

start loop
wait until first button is pressed
runs code
wait until second button is pressed
back to start

Without the while the code seems to respond to each button being pressed but after adding whiles it only works when both buttons are pressed together

const int BUTTON1 = 4 ;
const int BUTTON2 = 2;
const int LED1 = 13;
const int LED2 = 13;
int BUTTONstate1 = 0;
int BUTTONstate2 = 0;

void loop()
{
  BUTTONstate1 = digitalRead(BUTTON1);
 
digitalWrite(LED1, LOW);
  
//while (digitalRead(BUTTON1) == LOW) {}  // WAIT FOR BUTTON PRESS
  
  if (BUTTONstate1 == HIGH)
  {
    digitalWrite(LED1, HIGH);
    
  } 
  else{
    digitalWrite(LED1, LOW);
  }
 delay (1000);// WON'T NEED THIS 



   BUTTONstate2 = digitalRead(BUTTON2);
   
digitalWrite(LED1, LOW);
   
  // while (digitalRead(BUTTON2) == LOW) {}  // WAIT FOR BUTTON PRESS
   
  
  if (BUTTONstate2 == HIGH)
  {
    digitalWrite(LED1, HIGH);
    delay (3000);
  } 
  else{
    digitalWrite(LED1, LOW);
  }
 
}

without the whiles button1 makes the light come on for a second
the second button makes it light for 3 seconds
so far so good.

I now need it wait for the button press before moving on but when I add whiles it only lights up when both buttons are pressed and I can't see why this happens.

Can anyone see my error?

Thanks.

i think your code had many extra unnecessary statements

start loop
wait until first button is pressed
runs code
wait until second button is pressed
back to start

i think the following does what you intended

#if 0
const int BUTTON1 = 4 ;
const int BUTTON2 = 2;
const int LED1 = 13;
const int LED2 = 13;
int BUTTONstate1 = 0;
int BUTTONstate2 = 0;

#else
const int BUTTON1 = A1 ;
const int BUTTON2 = A2;

const int LED1 = 10;
const int LED2 = 11;

#define ON   LOW
#define OFF  HIGH
#define PRESSED   LOW
#endif

// wait for button 1 to be pressed
// turn LED 1 on for 1 second, then off
// turn LED 2 on for 3 second, then off
// wait for button 2 to be pressed

void loop()
{
    while (digitalRead(BUTTON1) != PRESSED)
        ;

    digitalWrite(LED1, ON);
    delay (1000);
    digitalWrite(LED1, OFF);

    digitalWrite(LED2, ON);
    delay (3000);
    digitalWrite(LED2, OFF);

    // is this really needed, just wait for button 1
    while (digitalRead(BUTTON2) != PRESSED)
        ;
}

void setup (void)
{
    pinMode (BUTTON1, INPUT_PULLUP);
    pinMode (BUTTON2, INPUT_PULLUP);

    digitalWrite (LED1, OFF);
    digitalWrite (LED2, OFF);

    pinMode (LED1, OUTPUT);
    pinMode (LED2, OUTPUT);
}

Thanks getting a little better but still not quite right.

Yours does detect each button but it also allows multiple press of each button and code still loops it won't run code assigned to each key press.

what I need is to

wait until button 1 pressed light led (run 1st code)
wait until button 2 pressed light led (run 2nd code)
wait until button 1 pressed light led (run 1st code)
wait until button 2 pressed light led (run 2nd code)

I don't want to run the same code again until the other code has has been run.

Look at the State Change Detection sketch that comes with the IDE.

aarg:
Look at the State Change Detection sketch that comes with the IDE.

Ok I looked at it but hard to see how to apply it too two buttons and it doesn't explain why the whiles are not doing what they I think they should do.

If I add delay between the LED commands you can clearly see the whiles are being bypassed as the leds will simply flash on and off all the time with no buttons pressed.

digitalWrite(LED1, ON);
delay (300);
digitalWrite(LED1, OFF);

delay (300);// shows the whiles are not halting the loop.

digitalWrite(LED2, ON);
delay (300);
digitalWrite(LED2, OFF);

also I'm actualy only using one LED so led1 and led2 are the same.

I have found a method that seems to work for me, I took bits from several codes and put them together.

If I press the buttons one after the other they increment a variable number (Presses) when it reaches a value of ten it turns off a relay in theory I haven't tried the relay yet.

/* the function */

int Presses = 0;
int relay = 9; // relay turns trigger signal - active high;

void buttonWait(int buttonPin){
  int buttonState = 0;
  while(1){
    buttonState = digitalRead(buttonPin);
    if (buttonState == HIGH) {
      return;
    }
  }
}

void buttonWait2(int buttonPin){
  int buttonState = 0;
  while(1){
    buttonState = digitalRead(buttonPin);
    if (buttonState == HIGH) {
      return;
    }
  }
}

void setup() {
  // put your setup code here, to run once:
  pinMode(LED_BUILTIN, OUTPUT); // initialize digital pin LED_BUILTIN as an output.
    // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  digitalWrite (relay, HIGH); // relay conduction;
  }

void loop() {
  // put your main code here, to run repeatedly:
  buttonWait(2); // wait for button press on pin 2
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on
  Presses++;
  delay(300); //wait for a second
  
  buttonWait2(4); // wait for another button press on pin 2
  digitalWrite(LED_BUILTIN, LOW);   // turn the LED off
 
  delay(300); //wait for a second
 Serial.println(Presses);
  delay(1);        // delay in between reads for stability
  
   if (Presses == 10)
  {
    digitalWrite (relay, LOW); // relay switch is turned off;
   Presses++;Presses++;Presses++;Presses++;// to jump the count
  } 
}

Obviously it's badly written but it may get me by.

How are your buttons wired? Do you have pullup or pulldown resistors on the input pins so they are not floating from LOW to HIGH and everywhere in between when the buttons are not pressed? Obviously, one of the button pins is connected to an input pin (4, 2), what is the other button pin connected to?

I do have resistors pulldown I think but it may be easier to use internal but I'm a bit unsure about this.
I don't seem to be getting any bad numbers in the count so it seems to be ok.

2nd pic after video clip shows how it's wired

I have got my whiles working now so it should be much easier.

const int BUTTON1 = 2;
const int BUTTON2 = 4;
const int LED1 = 13;

#define ON   HIGH
#define OFF  LOW


void loop()
{
    while (digitalRead(BUTTON1) == OFF) {};
    digitalWrite(LED1, ON);
   
    delay (2000);
       
    while (digitalRead(BUTTON2) == OFF) {};
    digitalWrite(LED1, OFF);
    
    delay (2000);
}

void setup (void)
{
    pinMode (BUTTON1,INPUT);
    pinMode (BUTTON2,INPUT);
       digitalWrite (LED1, OFF);
  pinMode (LED1, OUTPUT);
}