Hexapod spider Robot

Hello, everyone!

Please, I'd like a help to make three servo drivers works together, in the same time, and in different positions. In program bellow is possible, but only after two of them had finished the sequency of program. Not in same time.

void loop() {   // put your main code here, to run repeatedly:

for (int pulse=365;pulse<420;pulse++){
Servos.setPWM(SV1,0,pulse);
Servos.setPWM(SV3,0,pulse); // Move two servo drivers at the same time (good)
delay(5);
}
for (int pulse=410;pulse>335;pulse--){
Servos.setPWM(SV5,0,pulse); // Move one servo drivers only after the previous two arrived in position (Bad)
delay(5);
}
}

I'd like move the three servo motors at the same time in diffrent positions.

I appreciate any help!

Welcome to the forum

Please post a complete sketch that illustrates the problem, using code tags (the </> icon) when you do

Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
Adafruit_PWMServoDriver Servos = Adafruit_PWMServoDriver();

float duty = 1500;
bool update_pwm = 0;
void beginServos();

byte SV1=(0);
byte SV3=(2);
byte SV5=(4);

void setup() {
  Serial.begin(9600);
  Servos.begin();
  Servos.setPWMFreq(60);
  delay(200);

//Aciona os Servos para posição inicial

Servos.setPWM(SV1,0,365);
Servos.setPWM(SV3,0,365);
Servos.setPWM(SV5,0,365);

delay(500);
}


void loop() {   

for (int pulse=365;pulse<420;pulse++){
Servos.setPWM(SV1,0,pulse);
Servos.setPWM(SV3,0,pulse);delay(5);
}
for (int pulse=410;pulse>335;pulse--){
Servos.setPWM(SV5,0,pulse);
delay(5);
}


for (int pulse=420;pulse>350;pulse--){ 
Servos.setPWM(SV1,0,pulse);
Servos.setPWM(SV3,0,pulse);
delay(5);
}
for (int pulse=335;pulse<410;pulse++){ 
Servos.setPWM(SV5,0,pulse);
delay(5);
}

}

To do that, you must put all three servo commands in the same for loop.

You will need to use some math to convert the loop index (pulse value) for the first two servos into the proper pulse value range for the third.

Since the two loops currently have different numbers of steps, it will take some thought.

To make this process easier, make the loop index independent (ranging from 1 to 75, for example), and derive the individual pulse values for each servo from the loop variable value.

Something like this should work:

  for (int i=0; i<= 100; i++)
  {
    Servos.setPWM(SV1,0,map(i, 0, 100, 365, 420));
    Servos.setPWM(SV3,0,map(i, 0, 100, 365, 420));
    Servos.setPWM(SV5,0,map(i, 0, 100, 410, 335));
    delay(5);
  }

This way the move will take 101 steps, roughly 505 milliseconds.

Defining a macro might make things easier to read:

#define pulseRange(start, end) map(i, 0, 100, start, end)

  for (int i=0; i<= 100; i++)
  {
    Servos.setPWM(SV1,0,pulseRange(365, 420));
    Servos.setPWM(SV3,0,pulseRange(365, 420));
    Servos.setPWM(SV5,0,pulseRange(410, 335));
    delay(5);
  }

Hello, Jreminton.

Thanks for your adivises!

Hello, Mr. Johnwasser.

Thanks a lot for your instructions. It worked perfectly!!! Was exactly what i needed!

Hello, guys.

I'd like one more help. I am using 18 servo motors in this project and was necessary add the PCA9685 plate to control 16 of them. With this a had to use 02 entries of the arduino Nano plate, to control the others two servo motors.
Now i need simultaneously move the two servos connected to the PCA9685 and one servo connected to the arduino Nano, but i'm having trouble getting it.
Bellow are the lines of this part of program:

void loop() {  


for (int i=0; i<= 100; i++)
  {
    Servos.setPWM(SV13,0,pulseRange(365,300));  // Servo motor connected to the PCA9685 output
    Servos.setPWM(SV15,0,pulseRange(365,430)); // Servo motor connected to the PCA9685 output
    delay(5);
     }

for (pos=70; pos<=100; pos+=50){
SV17.write (pos);  // Servo motor connected to the Arduino Nano output
}
}

The goal is to move these three servo motors at the same time in different positions.

I appreciate any help!

This looks like a mistake. You're going from 70 to 100 in steps of 50?!? That's "Move to 70 and stay there since 70+50 would go beyond 100."

If you want S17 to move from 70° to 100° along with the other two servos:

  for (int i=0; i<= 100; i++)
  {
    Servos.setPWM(SV13,0,pulseRange(365,300));  // Servo motor connected to the PCA9685 output
    Servos.setPWM(SV15,0,pulseRange(365,430)); // Servo motor connected to the PCA9685 output
    SV17.write(pulseRange(70, 100));
    delay(5);
  }

Also works in microseconds:
SV17.writeMicroseconds(pulseRange(1400, 1700));

Perfect Mr. Johnwasser.

I understood your advice and it was correct. I tested this line in the program and worked perfectly.

Thanks again for your help!!