I have been playing with the example "sweep" sketch and have been able to move 2
servos simultaneously back and forth and I can vary the speed but I have not been able to
move the 2 servos in opposite directions while varying the speed.
I have spent hours moving code around, adding variables and changing things around to
get the servos to rotate in opposite directions while being able to change the speed, but to
no avail, Could someone guide me in the right direction?
This is the modified sketch I have tried.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
Servo myservo1; // twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
int pos1=7;
void setup() {
myservo.attach(5); // attaches the servo on pin 5 to the servo object
myservo.attach(6);
}
void loop() {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
It is not possible to do that with your sketch, but there is a other way.
When every servo motor is updated every 10ms or every 20ms, then each servo motor can be individually controlled. The position of each servo motor has to be calculated runtime. That requires some math.
Here is an example that I made in the Wokwi simulator: Servo Showcase - Wokwi ESP32, STM32, Arduino Simulator
I kept the math to a minimum and used the millis-timer of 10ms to create the different motions.
Start the simulation with the start button in the upper-middle of the screen. Then turn the knobs and click the buttons with the mouse.
// 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/356962335315431425
#include <Servo.h>
Servo myServo1;
Servo myServo2;
int position = 0;
int delta = +1; // controls direction and speed
void setup()
{
myServo1.attach(5);
myServo2.attach(6);
}
void loop()
{
// Set the new positions of the servo motors.
myServo1.write( position);
myServo2.write( 180 - position);
// Calculate new position.
position += delta;
if( position <= 0 or position >= 180)
delta = -delta;
// Control the speed with the delay, about 5 to 100ms.
delay( 15);
}
Try it in Wokwi here:
For a smooth speed control, I think that using 'float' variables is better:
// 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);
}
Thanks to everyone who responded. I used the sketch by Koepel and that is what I was looking for. I dont really understand how it works like the delta part, I played around with the speed and servos and its working.
Thanks again.
The position is moved forward with a certain amount. When 180 is reached then the position is decreased with a certain amount. That amount is in the 'delta' variable.