#include <Servo.h>
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
unsigned long previousMillis;
const unsigned long interval = 10; // 10 or 20 milliseconds are common values
float position = 0; // position of servo motor
int direction = 1; // direction for servo motor
int runCount = 0; // number of completed runs
void setup()
{
servo1.attach(4);
servo2.attach(5);
servo3.attach(6);
servo4.attach(7);
}
void loop()
{
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval && runCount < 1) // Continue only if runCount is less than 1
{
previousMillis = currentMillis;
float step;
step = (90.0 - fabs(90.0 - position)) / 90.0;
step = 0.1 + (pow(step, 1.5) * 2); // make a steep curve, with 0.1 as minimum
if (direction > 0)
{
position += step;
if (position >= 180.0)
{
position = 180.0;
direction = -1;
delay(10000);
}
}
else
{
position -= step;
if (position <= 0.0)
{
position = 0.0;
direction = 1;
delay(10000);
runCount++; // Increment the runCount after completing one full run
}
}
int angle = int(position);
servo1.write(angle);
servo2.write(angle);
servo3.write(angle);
servo4.write(angle);
}
}