multiple servos operate together

im trying to write code to have one servo go from 0-180
at the same time servo2 moves 0-90-0
then servo1 180-0. servo2 doesnt move. then repeat.
so this has been taking a week and alot of headache.
i wrote some code but cant quite get it correct. any ideas?

#include <Servo.h>
int servoPin1 = 2;
int servoPin2 = 3;
int servoPin3 = 4;
int servoPin4 = 5;
Servo Servo1;
Servo Servo2;
Servo Servo3;
Servo Servo4;
int pos1 = 0;
int pos2 = 0;
void setup() {
Servo1.attach(servoPin1);
Servo2.attach(servoPin2);
Servo3.attach(servoPin3);
Servo4.attach(servoPin4);
}
void loop() {
for (pos1 = 0; pos1 <= 180; pos1 += 1)
for (pos2 = 0; pos2 <= 90; pos2 += 1)
for (pos2 = 90; pos2 >= 0; pos2 -= 1)
{ Servo2.write(pos1); Servo4.write(pos2);
delay(5);
}
for (pos1 = 180; pos1 >= 0; pos1 -= 1)
{ Servo2.write(pos1);
delay(5);
}
}

Hi mtmtown, welcome to the forum.
There is a sticky post at the top of this forum.
General Guidance and How to use the Forum
Read through it.
It has a lot of very useful info for new forum members like yourself.
Of particular interest is the link to the How to use this forum - please read. that covers things like using code tags to post your code.

Your task is interesting.
If I were writing this, I would write a single loop that moved both servos from 0 to 90.
This would be a loop taking your index variable pos from 0 to 90, writing pos to both servos.
Then I would write a second loop that moved both servos, one from 90 to 180 and one from 90 to 0.
This would be a loop taking your index variable pos from 90 to 180.
Write pos to the first servo.
Write 180 - pos to the second servo.
Then a third loop to move the first servo from 180 back to 0.
This would be a loop taking your index variable pos from 180 to 0.
Write pos to your first servo.

Of interest, since you are trying to do several things at the same time, is the Demonstration code for several things at the same time

But first I would do it with the 3 loops I discussed above.

Nice! that is genius thanks alot!! its simple i like it. i owe you one. THANKS!!!

If you are using an ESP32 just assign each servo routine sequence to a task with the same priority, then trigger the task to run. The OS freeRTOS would then run each task multi tasked.

Or you can assign each task to a ESP32 core, multi-processing, and trigger both task (under a task group) to run at the same time. With each task assigned to a different core and grouped triggered, the task or servo sequences, will run at the same time.

I'm using an rf nano. So I changed my code to go 90° at a time. Still having issues I guess I'm not good at code.
I'm going to try to figure out my code I think it's the "for" code. Not sure. Kinda wonder if there's like arduino club or a place I can go to get help.

#include <Servo.h>
int servoPin1 = 2;
int servoPin2 = 3;
int servoPin3 = 4;
int servoPin4 = 5;
Servo Servo1;
Servo Servo2;
Servo Servo3;
Servo Servo4;
int pos1 = 0;
int pos2 = 180;
int pos3 = 90;
int pos4 = 90;
void setup() {
Servo1.attach(servoPin1);
Servo2.attach(servoPin2);
Servo3.attach(servoPin3);
Servo4.attach(servoPin4);
}
void loop() {

{ for (pos1 = 0; pos1 <= 90; pos1 += 1)
{Servo1.write(pos1); Servo2.write(pos1);}
for (pos2 = 180; pos2 >= 90; pos2 -= 1)
{Servo4.write(pos2);
delay(50);}}

for (pos3 = 90; pos3 <= 180; pos1 += 1)
{Servo1.write(pos3); Servo2.write(pos3); Servo4.write(pos3);
delay(50);}

{for (pos2 = 180; pos2 >= 90; pos2 -= 1)
{Servo1.write(pos2); Servo2.write(pos2);}
for (pos1 = 0; pos1 <= 90; pos1 += 1)
{Servo3.write(pos1);
delay(50);}}

for (pos4 = 90; pos4 >= 0; pos4 -= 1)
{Servo1.write(pos4); Servo2.write(pos4); Servo3.write(pos4);
delay(50);}
}

Again, go read through the posts I linked to. It covers how to use code tags.
You can edit your post to put your code in code tags.

#include <Servo.h>
int servoPin1 = 2;
int servoPin2 = 3;
int servoPin3 = 4;
int servoPin4 = 5;
Servo Servo1;
Servo Servo2;
Servo Servo3;
Servo Servo4;
int pos1 = 0;
int pos2 = 180;
int pos3 = 90;
int pos4 = 90;
void setup() {
  Servo1.attach(servoPin1);
  Servo2.attach(servoPin2);
  Servo3.attach(servoPin3);
  Servo4.attach(servoPin4);
}
void loop() {
 
  for (pos1 = 0; pos1 <= 90; pos1 += 1) 
  {Servo1.write(pos1); Servo2.write(pos1);}
  for (pos2 = 180; pos2 >= 90; pos2 -= 1) 
  {Servo4.write(pos2); 
  delay(50);}

  
  for (pos3 = 90; pos3 <= 180; pos1 += 1)
  {Servo1.write(pos3); Servo2.write(pos3); Servo4.write(pos3);
  delay(50);}


  for (pos2 = 180; pos2 >= 90; pos2 -= 1)
  {Servo1.write(pos2); Servo2.write(pos2);}
  for (pos1 = 0; pos1 <= 90; pos1 += 1)
  {Servo3.write(pos1); 
  delay(50);}


  for (pos4 = 90; pos4 >= 0; pos4 -= 1)
  {Servo1.write(pos4); Servo2.write(pos4); Servo3.write(pos4);
  delay(50);}
}

One thing that helps with code is formating it so that you can more easily see the coding structures.
CTRL-T auto formats the code.
This makes it easier to see each loop.

I notice that you added more loops and more servos than in your description.
You say you still have issues but do not describe what those issues are.
Please do so.

I wonder if part of your problem is that you have delays(50) in some of your loops and not in others. Is that on purpose?

As for clubs, there are such things. I have attended meetings of a local Makers Alliance.
Perhaps there is something similar near you.
I learned things in person that would been difficult for me to pick up on this forum.

So originally I was going to have 4 servos. I only had 2 before because if I could figure out how to do 2 then the other 2 would be similar code and I'd add them later. I added that code because its been tagged properly in accordance with forum rules. Each group separated by a space is my loops I tried to combine 2 "for" commands together then a delay. Then it moves to the next set of "for" commands.

  • Servo1/servo2 0-90
  • Servo4. 180-90
  • Servo1/servo2. 90-180
  • Servo4. 90-180.
  • Servo1/servo2. 180-90
  • Servo3. 0-90.
  • Servo1/servo2. 90-0.
  • Servo3. 90-0

That's the sequence I been trying to accomplish. I dont know how to combine the "for" commands.

Servo1/servo2 0-90
Servo4. 180-90

Do you want 4 to move after 1 and 2 move?
It looks to me like you move 1 and 2 very fast, then move 4 slower.

I want 1, 2, and 4 to move at the same time. But 4 has different degree settings.

Then 1, 2, and 4 move together. All the same degree settings.

Then 1, 2, and 3 move together with different degrees.

Then 1, 2, and 3 move together with similar direction.
Then the cycle repeats. All with a delay between degrees (50) so the servos dont move to fast. Using this command.
for (pos3 = 90; pos3 <= 180; pos1 += 1)

How do you get servo 1, 2, and 4 to move simultaneously in different directions? Then move to the next set of movements?

mtmtown:
I want 1, 2, and 4 to move at the same time.

Have you done the above?
Break the thing down into pieces.
Have you made servo 1n2 and 4 move at the same time?
servo1n2 starts at 0 and servo 4 starts at 180 you want servo 1n2 to count up and servo 4 to count down

int servo4Posit = 180
int servo124Position = 0

servo1 = servo124Position, servo2 = servo124Position, servo4 = servo4Posit -servo124Position
++servo124Position
servo1 = servo124Position, servo2 = servo124Position, servo4 = servo4Posit -servo124Position

++servo124Position

servo1 = servo124Position, servo2 = servo124Position, servo4 = servo4Posit -servo124Position
++servo124Position
servo1 = servo124Position, servo2 = servo124Position, servo4 = servo4Posit -servo124Position

Servo1n2 go up in angle servo 4 comes down in angle.

Just code for the above. Does it work? No post the code and let us know what you expected and what actually happened.

Don't fret with the other thingies, just get one working before moving along.

So I can't combine these two lines to operate at the same time.
What happens is servo4 goes 0-90-0 while servo2 goes 1 degree each time servo4 does 1 cycle.
I would like servo2 to move 90-180 at the same time as servo4 moves 90-0.
for (pos1 = 90; pos1 <= 180; pos1 += 1) { Servo2.write(pos1); }
for (pos2 = 90; pos2 >= 0; pos2 -= 1) { Servo4.write(pos2); }

#include <Servo.h>

int servoPin2 = 3;

int servoPin4 = 5;

Servo Servo2;

Servo Servo4;
int pos1 = 0;
int pos2 = 0;
void setup() {

  Servo2.attach(servoPin2);

  Servo4.attach(servoPin4);
}
void loop() {
  for (pos1 = 0; pos1 <= 90; pos1 += 1)
  { Servo2.write(pos1); Servo4.write(pos1);
    delay(25);
  }
  

  for (pos1 = 90;  pos1 <= 180; pos1 += 1) {
    Servo2.write(pos1);
  } for (pos2 = 90; pos2 >= 0; pos2 -= 1) {
    Servo4.write(pos2);
  }


  for (pos1 = 180; pos1 >= 0; pos1 -= 1)
  { Servo2.write(pos1);
    delay(25);
  }
}

You know how code works, right?

One line at a time when you have one CPU.

What you can do is make it look like both servos are turning at the same time (task switching) by moving the servos a short distance or in a burst. Move servo 1 a few and then move servo 2 for a few and if you do it quickly enough it looks like the same time.

Explore the exercise Demonstration code for several things at the same time Demonstration code for several things at the same time - Project Guidance - Arduino Forum

Or you can get a MCU with multiple cores and independently operating servo control units.

delay(25);

You understand that this is 100% blocking on an Arduino Uno? What the delay is doing is telling the Uno to do nothing for 25 milliSeconds.

Ok thanks I think I figured it out. I have an RF Nano. I figured out how to combine two "for" commands.
:o

  for (pos1 = 90 , pos2 = 90;  pos1 <= 180 , pos2 >= 0; pos1 += 1 , pos2 -= 1)
  {Servo2.write(pos1);Servo4.write(pos2);
  delay(25);}

It's no Minecraft command block but it'll do.

Excellent, I was hoping you'd figure it out.