#include <ESP32Servo.h>
Servo myservo1;
Servo myservo2;
Servo myservo3;
int pos = 0;
void setup() {
Serial.begin(9600);
myservo1.attach(18);
myservo2.attach(19);
myservo3.attach(21);
}
void loop() {
circularEaseInOut(0, 180, 2000);
circularEaseInOut(180, 0, 2000);
}
void circularEaseInOut(int startAngle, int endAngle, int duration) {
int steps = 100; // number of steps for the ease-in-out motion or say resolution of easing
int delayTime = duration / steps; //delay for each step
for (int i = 0; i <= steps; i++) {
float t = float(i) / steps;
float easingFunction1 = circularEaseInOutFunction(t);
float easingFunction2 = sineEaseInOutFunction(t);
float easingFunction3 = exponentialEaseInOutFunction(t);
int currentAngle1 = startAngle + easingFunction1 * (endAngle - startAngle);
int currentAngle2 = startAngle + easingFunction2 * (endAngle - startAngle);
int currentAngle3 = startAngle + easingFunction3 * (endAngle - startAngle);
myservo1.write(currentAngle1);
myservo2.write(currentAngle2);
myservo3.write(currentAngle3);
Serial.println(currentAngle1);
delay(delayTime);
}
}
// Circular ease-in-out function
float circularEaseInOutFunction(float t) {
t *= 2.0;
if (t < 1.0) {
return -0.5 * (sqrt(1.0 - t * t) - 1.0);
} else {
t -= 2.0;
return 0.5 * (sqrt(1.0 - t * t) + 1.0);
}
}
// Sine ease-in-out function
float sineEaseInOutFunction(float t) {
return -0.5 * (cos(3.14 * t) - 1.0);
}
// Exponential ease-in-out function
float exponentialEaseInOutFunction(float t) {
t *= 2.0;
if (t < 1.0) {
return 0.5 * pow(2.0, 10.0 * (t - 1.0));
} else {
t -= 1.0;
return 0.5 * (-pow(2.0, -10.0 * t) + 2.0);
}
}