Saving a sequence of switches in an array

I'm making a useless box with 5 switches. The switches need to be closed by the box in the same order they are switched on by the user.

The switches in the code shown below do not switch off in the same order they are switched on. For your understanding, I deleted anything that had to do with servo 2, since only servo 1 is responsible for moving to the switch position.

#include <Servo.h>

const int  buttonPin1 = 2;
const int  buttonPin2 = 3;
const int  buttonPin3 = 4;
const int  buttonPin4 = 5;
const int  buttonPin5 = 6;

int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;

Servo myservo1;

                            
int pos1=0;


void setup()
{
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);
  pinMode(buttonPin5, INPUT);

  myservo1.attach(9);   //servo 1 on pin 9  (bottom servo)


  myservo1.write(pos1); //set servo to 0 position

}


void loop()
{
  buttonState1 = digitalRead(buttonPin1); //read all switches
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);
  buttonState4 = digitalRead(buttonPin4);
  buttonState5 = digitalRead(buttonPin5);

  if (buttonState1 == HIGH){   //move to switch position if switch is on
    myservo1.write(36);
    delay(1500);
  }

  else if (buttonState2 == HIGH){ //move to switch position if switch is on
    myservo1.write(72);
    delay(1500);
    
  }

  else if (buttonState3 == HIGH){ //move to switch position if switch is on
    myservo1.write(108);
    delay(1500);

  }

  else if (buttonState4 == HIGH){ //move to switch position if switch is on
    myservo1.write(144);
    delay(1500);

  }

  else if (buttonState5 == HIGH){ //move to switch position if switch is on
    myservo1.write(180);
    delay(1500);

  }


}

To try and solve this, I made an array that saves whenever a switch is on, but this doesn't work at all and I don't know why. I feel like I'm close to the solution but I can't get this to work >.<

#include <Servo.h>

const int  buttonPin1 = 2;
const int  buttonPin2 = 3;
const int  buttonPin3 = 4;
const int  buttonPin4 = 5;
const int  buttonPin5 = 6;

int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;

Servo myservo1;
                            
int pos1=0;

void setup()
{
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);
  pinMode(buttonPin5, INPUT);

  myservo1.attach(9);   //servo 1 on pin 9  (bottom servo)

  myservo1.write(pos1); //set servos to 0 position

}

int  order[100];  
int i=0;          //counter

void checker(){

void loop()
{
  buttonState1 = digitalRead(buttonPin1); 
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);
  buttonState4 = digitalRead(buttonPin4);
  buttonState5 = digitalRead(buttonPin5);

if (buttonState1){
  order[i]=30;
  i++;
}

else if (buttonState2){
  order[i]=60;
  i++;
}

else if (buttonState3){
  order[i]=90;
  i++;
}

else if (buttonState4){
  order[i]=120;
  i++;
}

else if (buttonState4){
  order[i]=150;
  i++;
}

    myservo1.write(order[i-1]);
    delay(500);
}

To try and solve this, I made an array that saves whenever a switch is on, but this doesn't work at all and I don't know why.

That logic is just too convoluted to follow. When switch A BECOMES pressed, put A in the next position of the order array and increment the index.

You currently read the state of every switch on every pass through loop(), and then is the state IS pressed, you deal with the switch being pressed. You should only deal with the switch when it BECOMES pressed.

Each switch has an order in which it needs to be turned off. 1 means it is next in line to be turned off, 0 means it is already off.

There is a variable named 'maxOrder' holding the maximum order number of any button.

Each loop, check each switch to see if it has become turned on or turned off since last time through the loop.

When a switch becomes turned off (either by the box itself or by the human): every switch that has an order greater than the order of the turned-off switch has it's order reduced by 1, and maxOrder is decreased by 1. The switch that was turned off has its order set to zero.

When a switch becomes turned on, then maxOrder is incremented, and the switch's order is set to maxOrder.

Next in the loop, if the turner-offer is ready to turn of a switch, and maxOrder > 0, then loop through the switches to find the one whose order is 1. Tell the turner-offer to turn off that switch. Don't touch the order stuff - rely on the switch off being sensed to do that.

Finally in the loop, handle the turner-offerer.

Incideantlly, I like to bundle things like multiple identical buttons into structs.

struct Button {
  const byte pin;
  int state = HIGH; // assume pin high
  int order = 0; // off

  Button(byte attach) : pin(attach) {}

  void setup() {
    pinMode(pin, INPUT_PULLUP);
  }

  loop() {
  }

};

this allows you to easily define an array of buttons:

Button buttons[] = {Button(2), Button(3), Button(4), Button(5), Button(6)};

To set them up: loop through them in setup()

void setup() {
  for(int i = 0; i<sizeof(buttons)/sizeof(buttons[0]); i++) {
    buttons(i).setup();
  }
}

And in loop() :

void loop() {
  for(int i = 0; i<sizeof(buttons)/sizeof(buttons[0]); i++) {
    buttons(i).loop();
  }

  if(handIsBusy) {
    // give the hand a time slice
    doHand();
  }
  else {
    // work out if the hand needs to be started
  }
}

PaulMurray's approach is the right approach, this is how I normally handle multiple buttons.

You may want to consider using a vector instead of an array. Whenever a switch is hit, pushback a new element and use a for loop to iterate through the vector in order from first to last.