replaceing delay with timer interrupt

How can replace delay by timer interrupt ? my task is to make two servo work at the same time like multitasking

#include <Servo.h>
#define LEFT_DEFAULT {9, 2, 7, false, gate_left}
#define RIGHT_DEFAULT {10, 3, 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()
{
 
  //Setup the control and state of the Right gate
  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);
  

  //Setup the control and state of the Left gate
  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, FALLING);
  attachInterrupt(digitalPinToInterrupt(left.pin_switch), left_trigger, FALLING);
  digitalWrite(right.pin_switch, HIGH);
  digitalWrite(left.pin_switch, HIGH);

}

//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(){

      right.servo.write(right.ref.OPEN);
      delay(2000);
      right.servo.write(right.ref.CLOSE);
      delay(15);
}

void left_trigger(){
      left.servo.write(left.ref.OPEN);
      delay(2000);
      left.servo.write(left.ref.CLOSE);
      delay(15);
 
}

How can replace delay by timer interrupt ?

That doesn't make sense.

delay() causes the program to accomplish nothing for a fixed period of time.

A timer interrupt (almost) guarantees that something happens at fixed intervals,

They are NOT interchangeable.

my task is to make two servo work at the same time like multitasking

NOTHING is going to make the Arduino do two things AT THE SAME TIME. The Arduino can do two things in very quick succession, making it appear to slow humans that they are happening "at the same time", but they really aren't.

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

}

That is NOT what that code does.

You should NOT be using delay() in your program. At some point, there is a need to open a gate.

At some other point in time, there may be a need to close the gate.

That other point in time never happens is the gate is never opened. If the gate is open, and has been open long enough, then it is time to close it.

The blink without delay example shows you EVERYTHING you need to get rid of delay()s in your code.

The functions delay() and delayMicroseconds() block the Arduino until they complete.
Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.

...R

Can you clarify what's the purpose of the project? It's hard to help without knowing.

That being said, delay() is a function you shouldn't use, as it blocks the whole program. Easier than using interruptions, you can use millis() as in the BlinkWithoutDelay example, or download a Timer library (there are plenty in internet).

Moreover, the way of doing many things at the same time is to divide them in small pieces and put them inside a loop, so Arduino will do a small part of each at a great speed, and it will seem as if it was all made at once.
If what you want is to control many servos depending on the reading of a button, I suggest you research about a concept called "state chart" and try to use that kind of logic.

marcozzxx810:
How can replace delay by timer interrupt ? my task is to make two servo work at the same time like multitasking

Run both servos off one wire. You may have to put the PWM signal through a BJT of some sort, if one Arduino output cannot drive both servos.

You can but servo Y harness from Amazon.

https://smile.amazon.com/s/ref=nb_sb_ss_c_1_7?url=search-alias%3Daps&field-keywords=servo+y+harness&sprefix=servo+y%2Caps%2C210&crid=2ZY5SYKGBM8LW