I have done the next code but there is a misstake somewhere because the servos don't work but I can't find it...
I want to move the servo1 from 0 degrees to 30 degrees, in the same time, the servo2 from 45 degrees to position 15.
Then servo2 from 15º to 35º.
After that, servo1 from 30º to 0º and servo2 from 35º to 45º.
first servo degree difference 30° - 0° = 30
second servo degree difference 45° - 15° = 30
which means the same amount of degree per time-unit
after that
first servo: 30 - 0 = 30
second servo 45 - 35 = 10
which means different amount of degrees per time-unit
first servo does 3 times more
which means in a loop
servo1--
servo2: if servo1 = 27 servo2--
servo2: if servo1 = 24 servo2--
.....
for checking is servo1_angle 30, 27, 24 ...
you can use the modulo-operator
servo1_angle % 3 == 0
best regards Stefan
This does not work as you suppose. The two for loops are NOT executed in parallel, but one inside the other. That means for every loop step in the outer loop, the inner for loop is completely executed.
You may consider to use a library that can move the servos slowly by itself, e.g. my MobaTools library. Then you need no for loops to move the servos slowly. And the servos can move really at the same time.
I have checked your servo examples in MobaTools but I can't find the example with more than one servo.
Do you have any example to control two servos in parallel movement?
In the mobatools you set the servospeed prior to the servo-move.
Then you call a single time the final position
and the slowly moving is done by the library in the background
Yes, unfortunately there is no such example. I will create one for the next version.
But it's really straightforward: Simply create to servo instances and execute to servo.write() statements directly one after the other. The servos will move at the same time. Be aware, that the write()-statements are non blocking. Your sketches continues to run while the servos are moving. But you can ask in your sketch wether the servos have reached their target position.
#include <MobaTools.h>
int pos1 = 0;
int pos2 = 0;
MoToServo servo1;
MoToServo servo2;
void setup()
{
servo1.attach(9);
servo1.setSpeedTime( 4000 ); // ms to move from 0 ... 180
servo2.attach(10);
servo2.setSpeedTime( 3000 ); // ms to move from 0 ... 180
// Move servos to inital position ( this will always be done in max speed! )
servo1.write(0);
servo2.write(45);
}
void loop()
{
// simple example to move servos in parallel:
servo1.write(30);
servo2.write(15);
// write() is not blocking, so wait till servos reach target position
while ( servo1.moving() || servo2.moving() );
delay(100);
servo2.write(35);
while ( servo2.moving() );
delay(100);
servo1.write(0);
servo2.write(45);
while ( servo1.moving() || servo2.moving() );
// 1 second delay before starting over
delay( 1000 );
}
I never tried this, but I don't think it's a problem. You can safely try. If it works with servo.h it should work with MobaTools too. Both use the same HW-components of an AVR Arduino.