Problem with Change loop to interrupt

My task is to turn all the loop code to interrupt. However, after adding the interrupt the program become not working.

Original Code

#include <Servo.h>
#define LEFT_DEFAULT {9, 5, 7, false, gate_left}
#define RIGHT_DEFAULT {10, 6, 8, false, gate_right}

//The reference position for the servo
struct Gate_Ref{
  int OPEN;
  int CLOSE;
};
const Gate_Ref gate_left = {130, 50}, gate_right = {90, 170};

enum State {VACANT, OCCUPIED};
struct Device {
  int pin_servo; //the motor controlling the gate.
  int pin_switch;
  int pin_indicator; //one LED, turned on when the gate is opened, turned off otherwise
  bool occupied;
  Gate_Ref ref;
  Servo servo;
};
Device right = RIGHT_DEFAULT;
Device left = LEFT_DEFAULT;

//Exmamine if the button is pressed, which indicates the parking slot is occcupied
//@return TRUE if the button is pressedif the button is pressed, FALSE otherwise
bool check_occupied(const int& pin_switch){
  int first = digitalRead(pin_switch); // reads LOW if the button is pressed
  delay(15);
  int second = digitalRead(pin_switch); // read second time in case of glitches
  if (first != second)
    return check_occupied(pin_switch);
  else 
    return !first; //TRUE if the button is pressed
}

void setup()
{
  pinMode(right.pin_switch, INPUT_PULLUP);
  pinMode(right.pin_servo, OUTPUT);
  pinMode(right.pin_indicator, OUTPUT);
  digitalWrite(right.pin_indicator, LOW);
  right.servo.attach(right.pin_servo);
  right.servo.write(right.ref.CLOSE);
  delay(15);
  
  pinMode(left.pin_switch, INPUT_PULLUP);
  pinMode(left.pin_servo, OUTPUT);
  pinMode(left.pin_indicator, OUTPUT);
  digitalWrite(left.pin_indicator, LOW);
  left.servo.attach(left.pin_servo);
  left.servo.write(left.ref.CLOSE);
  delay(15);
  
  //Serial.begin(115200);
}

//Open the gate if the button state is swithced from OCCUPIED to VACANT, and close the gate automatically after 2 seconds.
void loop(){
  if (!right.occupied){
    right.occupied = check_occupied(right.pin_switch);
    if (right.occupied){
      digitalWrite(right.pin_indicator, HIGH);
      right.servo.write(right.ref.OPEN);
      delay(2000);
      right.servo.write(right.ref.CLOSE);
      delay(15);
      digitalWrite(right.pin_indicator, LOW);
    }
  } else {
    right.occupied = check_occupied(right.pin_switch);
  }
  if (!left.occupied){
    left.occupied = check_occupied(left.pin_switch);
    if (left.occupied){
      digitalWrite(left.pin_indicator, HIGH);
      left.servo.write(left.ref.OPEN);
      delay(2000);
      left.servo.write(left.ref.CLOSE);
      delay(15);
      digitalWrite(left.pin_indicator, LOW);
    }
  } else {
    left.occupied = check_occupied(left.pin_switch);
  }
}

Changed to interrupt

#include <Servo.h>
#define LEFT_DEFAULT {9, 5, 7, false, gate_left}
#define RIGHT_DEFAULT {10, 6, 8, false, gate_right}

//The reference position for the servo
struct Gate_Ref{
  int OPEN;
  int CLOSE;
};
const Gate_Ref gate_left = {130, 50}, gate_right = {90, 170};

enum State {VACANT, OCCUPIED};
struct Device {
  int pin_servo; //the motor controlling the gate.
  int pin_switch;
  int pin_indicator; //one LED, turned on when the gate is opened, turned off otherwise
  bool occupied;
  Gate_Ref ref;
  Servo servo;
};
Device right = RIGHT_DEFAULT;
Device left = LEFT_DEFAULT;

//Exmamine if the button is pressed, which indicates the parking slot is occcupied
//@return TRUE if the button is pressedif the button is pressed, FALSE otherwise
bool check_occupied(const int& pin_switch){
  int first = digitalRead(pin_switch); // reads LOW if the button is pressed
  delay(15);
  int second = digitalRead(pin_switch); // read second time in case of glitches
  if (first != second)
    return check_occupied(pin_switch);
  else 
    return !first; //TRUE if the button is pressed
}

void setup()
{
  pinMode(right.pin_switch, INPUT_PULLUP);
  pinMode(right.pin_servo, OUTPUT);
  pinMode(right.pin_indicator, OUTPUT);
  digitalWrite(right.pin_indicator, LOW);
  right.servo.attach(right.pin_servo);
  right.servo.write(right.ref.CLOSE);
  delay(15);
  
  pinMode(left.pin_switch, INPUT_PULLUP);
  pinMode(left.pin_servo, OUTPUT);
  pinMode(left.pin_indicator, OUTPUT);
  digitalWrite(left.pin_indicator, LOW);
  left.servo.attach(left.pin_servo);
  left.servo.write(left.ref.CLOSE);
  delay(15);
  attachInterrupt(digitalPinToInterrupt(right.pin_switch), right_trigger, CHANGE)
  attachInterrupt(digitalPinToInterrupt(left.pin_switch), left_trigger, CHANGE)
  //Serial.begin(115200);
}

//Open the gate if the button state is swithced from OCCUPIED to VACANT, and close the gate automatically after 2 seconds.
void loop(){
  
  
}

void right_trigger(){
	if (!right.occupied){
    right.occupied = check_occupied(right.pin_switch);
    if (right.occupied){
      digitalWrite(right.pin_indicator, HIGH);
      right.servo.write(right.ref.OPEN);
      delay(2000);
      right.servo.write(right.ref.CLOSE);
      delay(15);
      digitalWrite(right.pin_indicator, LOW);
    }
  } else {
    right.occupied = check_occupied(right.pin_switch);
  }
}

void left_trigger(){
	if (!left.occupied){
    left.occupied = check_occupied(left.pin_switch);
    if (left.occupied){
      digitalWrite(left.pin_indicator, HIGH);
      left.servo.write(left.ref.OPEN);
      delay(2000);
      left.servo.write(left.ref.CLOSE);
      delay(15);
      digitalWrite(left.pin_indicator, LOW);
    }
  } else {
    left.occupied = check_occupied(left.pin_switch);
  }
}

marcozzxx810:
Can you guys correct my code ....

What is wrong with it? If you don't tell us what it does, and how that differs from what you expect, there isn't a hope in hell that the changes we suggest (I'm refraining, and, believe me, that is extremely hard) will get you the improvement you want.

@PaulS
I have reposting my problem. Sorry for that

bool check_occupied(const int& pin_switch){

Why does this function take a reference to an int? Surely you don't expect to change the value in pin_switch.

    return check_occupied(pin_switch);

Why does the function call itself again?

  attachInterrupt(digitalPinToInterrupt(right.pin_switch), right_trigger, CHANGE)
  attachInterrupt(digitalPinToInterrupt(left.pin_switch), left_trigger, CHANGE)

Those pin numbers are 5 and 6. Which Arduino has interrupts on those pins?

      delay(2000);

In an ISR? Dream on.

marcozzxx810:
My task is to turn all the loop code to interrupt.

Don't try to change all the loop code to the code for an Interrupt Service Routine.

Change the loop() code into two separate functions and use the ISR to raise a flag to let loop() know that the ISR has triggered. Then loop() can call the appropriate function. Something like this

void rightISR() {
   rightTriggerFlag == true;
}

void loop() {
  if (rightTriggerFlag
     rightTriggerFunction();
  }
}

And at the end of the rightTriggerFunction() set rightTriggerFlag back to false

Note that if your functions take more than a few millisecs to complete then loop() will be unable to keep up.

Have a look at how things are done in Several Things at a Time. Note how each function runs very briefly and returns to loop() so the next one can be called. Long running processes are achieved a tiny piece at a time. And there may be dozens of calls to some functions before it is actually time for anything to happen.

...R

void rightISR() {
   rightTriggerFlag == true;
}

Oops...

  if (rightTriggerFlag

Oops...