How do I code the motion in this animation using 4 servo motors?

I want my servo motors to move in the same motion and direction at the same time but with different start times. This is the data of how I want it to move.


This is the code I have as of now;

#include <Servo.h>
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;
int pos = 180;
unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 200; //the value is a number of milliseconds, ie 1 second

void setup() {
  // put your setup code here, to run once:
  int pos = 180;
  servo1.attach(1);
  servo2.attach(3);
  servo3.attach(5);
  servo4.attach(7);
  servo5.attach(9);
  servo1.write(pos);
  servo2.write(pos);
  servo3.write(pos);
  servo4.write(pos);
  servo5.write(pos);
  startMillis = millis();
}

void loop() {
  // put your main code here, to run repeatedly:
  unsigned long currentMillis = millis();
  servo5.write(0);
  for (pos = 180; pos >= 80; pos -= 1){
    if (currentMillis - startMillis >= period)
    {
     servo2.write(pos);
    }
    servo1.write(pos);
    delay(20);
  }
  for (pos = 80; pos <= 180; pos += 1){
  servo1.write(pos);
  delay(20);
 }
}

Please do not cross post.

The ServoEasing library may be helpful to get the sine motion.

@phosphosaurus ,

Your two topics on the same or similar subject have been merged.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.

Looking at the table and the graph, the time goes from 0 to 20. That will be the basis.
Either use a table, perhaps with interpolation, or calculate the position with math.

Using math, there is a motion from 180 to 80 and back and then later on once more. A if-statement can be used to check the time. A total of 16 if-statements are needed.

Do you understand the sketch ? Can you add the other servo motors ?

// For: https://forum.arduino.cc/t/how-do-i-code-the-motion-in-this-animation-using-4-servo-motors/1037603/

#include <Servo.h>

Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;

float seconds = 0.0;                 // walk from 0 to 20 seconds
float maxSeconds = 20.0;             // maximum number of seconds for sequence    

unsigned long previousMillis;
const unsigned long interval = 20;  // interval for millis timer

void setup() 
{
  servo1.attach(1);
  servo2.attach(3);
  servo3.attach(5);
  servo4.attach(7);
  servo5.attach(9);

  // Set initial positions
  servo1.write(180);
  servo2.write(180);
  servo3.write(180);
  servo4.write(180);
  servo5.write(180);
}

void loop() 
{
  unsigned long currentMillis = millis();

  // Make a millis timer
  if (currentMillis - previousMillis >= interval)
  {
    previousMillis = currentMillis;

    // The seconds go from 0 to 20, then it restarts at 0, and so on.
    seconds += (float) interval / 1000.0;   // interval is in milliseconds
    if( seconds >= maxSeconds)              // start over again ?
      seconds = 0.0;

    // --------------------------------------------------
    // servo1
    // --------------------------------------------------
    if( seconds >= 1.0 and seconds < 3.0)
    {
      // From 1 to 3 seconds, servo1 from 180 to 80
      // time range is 2 seconds. time offset is 1 second
      // servo angle range is 100 degrees
      // 
      // From seconds to angle: 
      //   divide by time range, multiply by angle range, solve offset
      float pos = 180.0 - ((seconds - 1.0) / 2.0 * 100.0);
      servo1.write((int) pos);
    }
    else if( seconds >= 3.0 and seconds < 5.0)
    {
      // from 3 to 5 seconds servo 1 from 80 to 180
      float pos = 80.0 + ((seconds - 3.0) / 2.0 * 100.0);
      servo1.write((int) pos);
    }
    else if( seconds >= 16.0 and seconds < 18.0)
    {
      float pos = 180.0 - ((seconds - 16.0) / 2.0 * 100.0);
      servo1.write((int) pos);
    }
    else if( seconds >= 18.0 and seconds < 20.0)
    {
      float pos = 80.0 + ((seconds - 18.0) / 2.0 * 100.0);
      servo1.write((int) pos);
    }

    // --------------------------------------------------
    // servo2
    // --------------------------------------------------
  }
}

This sketch in Wokwi:

consider - only half your sequence. write a script to generate the servoOps table

#undef MyHW
#ifdef MyHW
struct Servo {
    byte _pin;

    void attach (int pin)  { _pin = pin; }
    void write  (int pos)  { 
        Serial.print ("write: ");
        Serial.print (_pin);
        Serial.print (" ");
        Serial.println (pos);
    }
};

#else
# include <Servo.h>
#endif

byte  PinServo [] = { 1, 3, 5, 7, 9 };
#define Nservo sizeof(PinServo)

Servo servo [Nservo];

struct ServoOp {
    Servo        *servo;
    unsigned long msec;
    int           pos;
    bool          done;
};

ServoOp servoOps [] = {
    { &servo [0],  2000, 130 },
    { &servo [0],  3000,  80 },
    { &servo [0],  4000, 130 },

    { &servo [1],  3000, 130 },
    { &servo [1],  4000,  80 },
    { &servo [1],  4000, 130 },

    { &servo [2],  4000, 130 },
    { &servo [2],  5000,  80 },
    { &servo [2],  6000, 130 },

    { &servo [3],  5000, 130 },
    { &servo [3],  6000,  80 },
    { &servo [3],  7000, 130 },

    { &servo [4],  6000, 130 },
    { &servo [4],  7000,  80 },
    { &servo [4],  8000, 130 },
};
#define NservoOps   (sizeof(servoOps)/sizeof(ServoOp))

unsigned long msec0;

// -----------------------------------------------------------------------------
void
loop (void)
{
    unsigned long msec = millis ();

    bool done = true;

    ServoOp *s = servoOps;
    for (unsigned n = 0; n < NservoOps; n++, s++)  {
        if (! s->done)  {
            done = false;

            if ( (msec - msec0) > s->msec)  {
                s->servo->write (s->pos);
                s->done = true;
            }
        }
    }

    if (done) {
        msec0 = msec;
        for (unsigned n = 0; n < NservoOps; n++)
            servoOps [n].done = false;
    }
}

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

    for (unsigned n = 0; n < Nservo; n++)  {
        servo [n].attach (PinServo [n]);
        servo [n].write  (180);
    }

    msec0 = millis ();
}