This is how I control the position of the servos. I know you can do for (pos = 0; pos <= 180; pos += 1) however I tried this for the servos, and I couldn't get them to move independently at the same time.
Which Arduino board are you using? If it's one of the supprted, you could give my MobaTools library a try. There are nearly the same methods as with Servo.h, but you can set the servo speed too.
The library can be installed by the library manager.
You can not do a for() loop on each servo in sequence if you want them to move at the same time. For each servo, you need to compare your current position to the end position, and if you aren't done, take one step and then move on to the next servo. Repeat until all servos are in their final position. Arrays would be your friend doing this exercise vs. variable1, variable2, etc.
I need all of them to move at the same time as well as I need a set of legs e.g leg1 and 2 to equal 180 in total as they are all connected to the same plate above connected by some rods
I don't know, because I don't know your code ( You should post it here ).
Mostly you only need to set the speed of the servos when changing from Servo.h to MobaTools.h. ( Of course there is no such method in the Servo.h library.
#include <Servo.h>
const byte LEG_COUNT = 6;
Servo leg[LEG_COUNT];
const byte servoPin[LEG_COUNT] = { 2,3,4,5,6,7 }; // whatever the real pins are
void setup() {
for( int i = 0; i < LEG_COUNT; ++i ) {
leg[i].attach(servoPin[i]);
}
}
int finalPosition[LEG_COUNT] = { 20, 160, 80, 110, 70, 130 }; // final position for each leg
void loop() {
for (int pos = 0; pos <= 180; ++pos) {
bool done = true; // assume we are done
for ( int i=0; i < LEG_COUNT; ++i ) {
if ( pos <= finalPosition[i] ) {
// need to move, so not done
done = false;
leg[i].write(pos);
}
}
delay(15); // adjust to correct speed
if (done) break; // if all legs are done, no need to continue for() loop
}
while(1);
}