// Forum: https://forum.arduino.cc/t/how-to-move-2-servos-in-different-directions-with-speed-control/1091643
// This Wokwi project: https://wokwi.com/projects/356961353402635265
#include <Servo.h>
Servo myServo1;
Servo myServo2;
unsigned long interval = 15; // servo update every 15 ms.
float position = 0;
float delta = 1.0; // controls direction and speed
void setup()
{
myServo1.attach(5);
myServo2.attach(6);
}
void loop()
{
// Read the potentiometer and convert it to the delta value.
// Keep 'delta' positive or negative to continue in the same direction.
// I don't know the maximum speed of the servo motor, the value
// of 150 was choosen because it looked good.
int rawADC = analogRead( A0);
if( delta > 0.0)
delta = (float) rawADC / 150.0;
else
delta = -((float) rawADC / 150.0);
// Set the new positions of the servo motors.
myServo1.write( (int) position);
myServo2.write( (int) (180.0 - position));
// Calculate new position.
position += delta;
if( position > 180.0)
{
delta = -delta;
position = 180.0;
}
if( position < 0.0)
{
delta = -delta;
position = 0.0;
}
// Slow down the sketch to run the loop every 15 ms.
delay( interval);
}