I need help for servo without delay

I'm making a kind of automatic feeder. So when it's time to eat, the servo will open (180°) the lid of the feed container and the feed will be forwarded to the dc motor which will be a feed launcher. When the dc motor reaches its peak speed (let's say 150), the servo will close (0°) and the dc motor will slowly decrease its speed. I hope my language is understandable

my code won't open the servo because there is no delay() for the servo to open. if i use delay there. the servo will be closed immediately before the dc motor rotates

your words aren't matching what the code appears to do

most servos take very little time (0.1 sec) to move. do you believe this is correct?

I added prints to your code. The output shows that the motor shouldn't reach a speed of 150 for about 10 sec (39.44.067 - 39.34.067) after the servo moves to 180

shouldn't the servo have moved to 180 within 0.1 sec of starting the motor?

06:39:34.067 -> servo 180
06:39:34.067 -> motor at speed 0
06:39:44.251 -> motor at speed 150
06:39:45.281 -> servo 0
06:39:45.281 -> motor at speed 150
06:39:55.477 -> motor at speed 0
// #include <ESP32Servo.h>
#include <Servo.h>

Servo myservo;
int pos = 0;

int R_PWM = 13;
int L_PWM = 27;
int R_EN = 14;
int L_EN = 12;

void kasihMakan ()
{
    myservo.write (180);
    Serial.println ("servo 180");

    Serial.println ("motor at speed 0");
    for (int speed = 0; speed <= 150; speed = speed + 3) {
        analogWrite (R_PWM, speed);
        delay (200);
    }
    Serial.println ("motor at speed 150");

    delay (1000);

    myservo.write (0);
    Serial.println ("servo 0");

    Serial.println ("motor at speed 150");
    for (int speed = 150; speed >= 0; speed = speed - 3) {
        analogWrite (R_PWM, speed);
        delay (200);
    }

    Serial.println ("motor at speed 0");
    delay (1000);
}

void setup ()
{
    Serial.begin (9600);

    myservo.attach (26);

    pinMode (R_PWM, OUTPUT);
    pinMode (L_PWM, OUTPUT);
    pinMode (R_EN, OUTPUT);
    pinMode (L_EN, OUTPUT);

    digitalWrite (R_EN, HIGH);
    digitalWrite (L_EN, HIGH);

    kasihMakan ();
}


void loop () {
}

I expect servo and DC motor to run together. but I don't know what the correct code is. servo opens when DC motor is running, and closes when DC motor stops

what about something like this?

    for (int n = 0; n < 180; n += 3)  {
        myservo.write (n);
        analogWrite   (R_PWM, n);
        delay (200);
    }

If it's like that, wouldn't the servo take too long to open it? And it would cause the feed to be stuck at the beginning of the opening process.

it would be better if the servo could open instantly.

but I will use it as a backup code if I haven't found a solution by the end. Thanks gcjr

but that's exactly what your code does?

yes, with the note that the weaknesses are like this.

It would be better if it could be like this

not clear.

how long does it take the servo to move?
how long is instantly? 1 sec, 1 msec, 1 usec?

under 1 second. directly open is better

are you saying the above code takes longer that 1 sec to move the servo 180 deg?

The code I made at the beginning did not work properly. Because at the beginning it was initialized in position 0° (int pos = 0;). So if there is only the command myservo.write (180); without the delay(); function, it will not work. Sorry if I misunderstood

pos = 0; does nothing. It's not used by the library or your code.

When you do the myservo.attach the servo will be set to the 90° position.

So if there is only the command myservo.write (180); without the delay(); function, it will not work.

So put in the delay.

I want that to be the case.

But, the servo and DC motor should move together.

Why do they need to move together?
First open the lid then start the motor.
When done, stop the moter and then close the lid

do you want the servo to move after the motor starts speeding up but before it reaches full speed

what about something like this

    int speed = 0
    for ( ; speed <= 50; speed = speed + 3) {
        analogWrite(R_PWM, speed);
        delay(200);
    }

    myservo.write(180);

    for ( ; speed <= 150; speed = speed + 3) {
        analogWrite(R_PWM, speed);
        delay(200);
    }

Without delay(), the servo won't open as far as I know.

Sorry, this is my fault. I mean the servo moves to 180° when the DC motor just starts rotating.

Because, my purpose of using a DC motor for the feed launcher is to spread the feed evenly. From the nearest to the furthest side of the pond, all are covered. I hope you can imagine it.

I don't use a load cell to measure the weight. I just use time calculations. So if the servo and DC motor don't move together, it's feared that the measurements won't be correct.

And yes, I've tried it many times.

servo.write() doesn't need a delay to function.

so if a delay is needed, then why not?

    int speed = 0
    for ( ; speed <= 50; speed = speed + 3) {
        analogWrite(R_PWM, speed);
        delay(200);
    }

    myservo.write(180);
    delay (500);

    for ( ; speed <= 150; speed = speed + 3) {
        analogWrite(R_PWM, speed);
        delay(200);
    }

I have tried it many times. With or without delay, the result is as I explained before. Servo needs delay, but delay will delay all programs. So servo and DC motor cannot work together