Script carries on before 2nd servo command completed

New here, HI!

Two servos, two doors. They open as they should in the initial loop statement. Before the command to close the doors happens, the LED lights up when it shouldn't until the command to close them takes place. Eventually, the doors close, LED goes out and it starts again. If they run without the LED, they open and close as they should (test). It's like the code to close the doors doesn't even exist until the script is complete. Been working on this all day! Servos powered from Nano. Any help would be appreciated. Thanks in advance.

#include <Servo.h>
#include <Wire.h>

Servo servoR;  
Servo servoL;  

int servoCloseL = 100;
int servoCloseR = 1;
int servoOpenL = 1;
int servoOpenR = 100;
int doorDelay = 50; 


void setup() {

servoR.attach(2); 
servoL.attach(3);  
servoR.write(servoCloseR);
pinMode(12,OUTPUT);  //LED

}

void loop() {
 
 servoR.attach(2);  
 servoL.attach(3); 
 servoR.write(servoOpenR);
 servoL.write(servoOpenL);

delay(10000);

servoR.attach(2);  
servoL.attach(3);  
servoL.write(servoCloseL);
delay(doorDelay);
servoR.write(servoCloseR);
delay(5000);
digitalWrite(12,HIGH);
delay(5000);
digitalWrite(12,LOW);
}

You only need to call the attach function once in setup.

Why does the code attach() the servos in loop() ?
Why is no initial position of servoL in setup() ?

You could write() the initial positions in setup() before you attach() the servos and they will move to those positions when attached

Testing. Made those adjustments, however, they have no affect on the issue.

Please post your amended sketch in a new reply here

#include <Servo.h>
#include <Wire.h>

Servo servoR;  
Servo servoL;  

int servoCloseL = 100;
int servoCloseR = 1;
int servoOpenL = 1;
int servoOpenR = 100;
int doorDelay = 50; 


void setup() {

servoR.attach(2); 
servoL.attach(3);  
servoR.write(servoCloseR);
servoR.write(servoCloseL);
pinMode(12,OUTPUT);  //LED

}

void loop() {
 
 servoR.write(servoOpenR);
 servoL.write(servoOpenL);

delay(10000);
 // LED turns on about here for some reason
servoL.write(servoCloseL);
delay(doorDelay);
servoR.write(servoCloseR);
delay(5000);
digitalWrite(12,HIGH);
delay(5000);
digitalWrite(12,LOW);
}

Here is a version of your sketch with debugging statements added which seems to work for me

Note that I changed the LED used to suit my test system

Have you got a current limiting resistor in series with the LED ?

Not usually a good idea. Disconnect the servos and run my sketch. Do the debug messages appear at the right times ?

Observation. The LED seems to go off at the same time the doors close regardless of how long I set the last delay.

Run my test sketch with timestamps turned on in the Serial monitor and post about 20 lines of output here, using code tags when you do

Yes, resistor. I don't see your amended code.

I am not sure what happened but here it is

#include <Servo.h>
#include <Wire.h>

Servo servoR;
Servo servoL;

int servoCloseL = 100;
int servoCloseR = 1;
int servoOpenL = 1;
int servoOpenR = 100;
int doorDelay = 50;
const byte ledPin = 13;

void setup()
{
    Serial.begin(115200);
    servoR.attach(2);
    servoL.attach(3);
    servoR.write(servoCloseR);
    servoR.write(servoCloseL);
    pinMode(ledPin, OUTPUT);  //LED
}

void loop()
{
    Serial.println("opening");
    servoR.write(servoOpenR);
    servoL.write(servoOpenL);

    delay(10000);
    // LED turns on about here for some reason
    Serial.println("closing L");
    servoL.write(servoCloseL);
    delay(doorDelay);
    Serial.println("closing R");
    servoR.write(servoCloseR);
    delay(5000);
    Serial.println("turning on LED");
    digitalWrite(ledPin, HIGH);
    delay(5000);
    Serial.println("turning off LED");
    digitalWrite(ledPin, LOW);
}

Change the LED pin and baud rate to suit yourself

Initial Close
opening (pause, no opening)
closing L (opening)
closing R (Opening)

turning on LED (LED Turns on seconds later)
turning off LED (LED off, doors close)

Doors Opening (New Loop) (Doors Close)

It's like the Open/Closed are reversed but they're not. Works fine in Initial Setup. Reversing made no difference.

Please follow the above instructions

Text output is as it should be but doesn't sync with what's actually happening. See attached video.

Opening
closing L
closing R
turning on LED
turning off LED
opening
closing L
closing R
turning on LED
turning off LED
opening
closing L
closing R
turning on LED
turning off LED
opening
closing L
closing R
turning on LED
turning off LED
opening
closing L
closing R
turning on LED
turning off LED
opening

Video: https://youtu.be/NaJp3rtu-DQ

I would still be interested in seeing the timestamps in the Serial monitor

07:42:08.802 -> opening
07:42:13.795 -> closing L
07:42:13.843 -> closing R
07:42:18.862 -> turning on LED
07:42:23.884 -> turning off LED
07:42:23.884 -> opening
07:42:28.880 -> closing L
07:42:28.926 -> closing R
07:42:33.962 -> turning on LED
07:42:38.949 -> turning off LED
07:42:38.949 -> opening
07:42:43.915 -> closing L
07:42:44.009 -> closing R
07:42:49.024 -> turning on LED
07:42:54.008 -> turning off LED
07:42:54.008 -> opening
07:42:59.022 -> closing L
07:42:59.064 -> closing R
07:43:04.080 -> turning on LED
07:43:09.109 -> turning off LED
07:43:09.109 -> opening
07:43:14.094 -> closing L
07:43:14.141 -> closing R
07:43:19.181 -> turning on LED
07:43:24.188 -> turning off LED

How do you have the LED wired? Does it turn on when the output if LOW, or when the output is HIGH?

Please verify that the close position on the servos is actually closing the doors, and the open position is actually opening the doors. Would be confusing if you got that reversed.

LED wired with resistor on pin 12. The doors are actually opening and closing as per video. LED is just coming on too early midway while doors are open and turning off when it should be coming on, when doors close.

is resistor/led connected to 5V or GND?

I can see the doors are opening and closing, but are they opening and closing as the code intends, or is the operation reversed so that they close when you send the commands to open and open when you send the commands to close?

The LED is supposed to come on after the doors close. Instead, it comes on 5 seconds after doors open and goes off when doors close.