4 motors working at the same time

Hi all,

I’m working on a chandelier that is connected from 4 points to 4 motors.
So each of the motors has their own angle of rotation and also their own speed of movement and it works perfectly clean. But the only issue now is that when I upload the code, the first motor starts to work and after rotating once, it stops and then the second one starts working and after one round it stops then the third one starts working and the same thing for the forth one.
But I want all of them to start rotating at the same time.

I couldn’t figure out that yet. Does anyone know how I can fix this??

Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;

int pos1 = 65;
int pos2 = 152;
int pos3 = 30;
int pos4 = 125;
int pos5 = 55;
int pos6 = 135;
int pos7 = 20;
int pos8 = 100;

int S1 = pos1;
int S2 = pos3;
int S3 = pos5;
int S4 = pos7;

int add = 1;

int speedconst1 = 20;
int speedconst2 = 40;
int speedconst3 = 60;
int speedconst4 = 80;

void setup()
{
Serial.begin(9600);
Serial.flush();

myservo1.attach(8);
myservo2.attach(9);
myservo3.attach(10);
myservo4.attach(11);
}

void loop()
{
for(S1 = pos1; S1 < pos2; S1 += add)
{
myservo1.write(S1);

delay(speedconst1);
Serial.flush();
Serial.print(S1);
Serial.print(’\n’);
}
for(S2 = pos3; S2 < pos4; S2 += add)
{
myservo2.write(S2);
delay(speedconst2);
Serial.flush();
Serial.print(S2);
Serial.print(’\n’);
}
for(S3 = pos5; S3 < pos6; S3 += add)
{
myservo3.write(S3);
delay(speedconst3);
Serial.flush();
Serial.print(S3);
Serial.print(’\n’);
}
for(S4 = pos7; S4 < pos8; S4 += add)
{
myservo4.write(S4);
delay(speedconst4);
Serial.flush();
Serial.print(S4);
Serial.print(’\n’);
}
for(S1 = pos2; S1>=pos1+1; S1 -= add)
{
myservo1.write(S1);
delay(speedconst1);
Serial.flush();
Serial.print(S1);
Serial.print(’\n’);
}
for(S2 = pos4; S2>=pos3+1; S2 -= add)
{
myservo2.write(S2);
delay(speedconst2);
Serial.flush();
Serial.print(S2);
Serial.print(’\n’);
}
for(S3 = pos6; S3>=pos5+1; S3 -= add)
{
myservo3.write(S3);
delay(speedconst3);
Serial.flush();
Serial.print(S3);
Serial.print(’\n’);
}
for(S4 = pos8; S4>=pos7+1; S4 -= add)
{
myservo4.write(S4);
delay(speedconst4);
Serial.flush();
Serial.print(S4);
Serial.print(’\n’);
}

The servos are moving one by one because you wrote your sketch to do all the servo1 movements and then all the servo2 movements and so on.

If you want them to move simultaneously, you will need to substantially redesign your sketch.

The simplest redesign would be to have a single loop that moves all four servos, with delay() to slow it down to the right speed. This is only sensible if the movements of all your servos will be basically similar - if they are changing direction/speed independently then the single loop will not work well. In that case you need to design your sketch to control the servo movements using the techniques shown in the ‘blink without delay’ example sketch. With that approach, the main loop() function runs repeatedly without any delays, and just checks the current time to see whether it is time to do anything. This is the preferred approach when your sketch needs to time several activities independently.

golnar:
Hi all,

I’m working on a chandelier that is connected from 4 points to 4 motors.
So each of the motors has their own angle of rotation and also their own speed of movement and it works perfectly clean. But the only issue now is that when I upload the code, the first motor starts to work and after rotating once, it stops and then the second one starts working and after one round it stops then the third one starts working and the same thing for the forth one.
But I want all of them to start rotating at the same time.

I couldn’t figure out that yet. Does anyone know how I can fix this??

Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;

int pos1 = 65;
int pos2 = 152;
int pos3 = 30;
int pos4 = 125;
int pos5 = 55;
int pos6 = 135;
int pos7 = 20;
int pos8 = 100;

int S1 = pos1;
int S2 = pos3;
int S3 = pos5;
int S4 = pos7;

int add = 1;

int speedconst1 = 20;
int speedconst2 = 40;
int speedconst3 = 60;
int speedconst4 = 80;

void setup()
{
Serial.begin(9600);
Serial.flush();

myservo1.attach(8);
myservo2.attach(9);
myservo3.attach(10);
myservo4.attach(11);
}

void loop()
{
for(S1 = pos1; S1 < pos2; S1 += add)
{
myservo1.write(S1);

delay(speedconst1);
Serial.flush();
Serial.print(S1);
Serial.print(’\n’);
}
for(S2 = pos3; S2 < pos4; S2 += add)
{
myservo2.write(S2);
delay(speedconst2);
Serial.flush();
Serial.print(S2);
Serial.print(’\n’);
}
for(S3 = pos5; S3 < pos6; S3 += add)
{
myservo3.write(S3);
delay(speedconst3);
Serial.flush();
Serial.print(S3);
Serial.print(’\n’);
}
for(S4 = pos7; S4 < pos8; S4 += add)
{
myservo4.write(S4);
delay(speedconst4);
Serial.flush();
Serial.print(S4);
Serial.print(’\n’);
}
for(S1 = pos2; S1>=pos1+1; S1 -= add)
{
myservo1.write(S1);
delay(speedconst1);
Serial.flush();
Serial.print(S1);
Serial.print(’\n’);
}
for(S2 = pos4; S2>=pos3+1; S2 -= add)
{
myservo2.write(S2);
delay(speedconst2);
Serial.flush();
Serial.print(S2);
Serial.print(’\n’);
}
for(S3 = pos6; S3>=pos5+1; S3 -= add)
{
myservo3.write(S3);
delay(speedconst3);
Serial.flush();
Serial.print(S3);
Serial.print(’\n’);
}
for(S4 = pos8; S4>=pos7+1; S4 -= add)
{
myservo4.write(S4);
delay(speedconst4);
Serial.flush();
Serial.print(S4);
Serial.print(’\n’);
}

Sweep_GBP6.ino (2.8 KB)

Thanks for the reply. But how can I put all of them in a same loop? I know there is && command but it keeps giving me errors. I am an architect and this is the first time I'm writing a code and it took me a week to write this!

Re your sketch being mostly in italics:

Please edit your post, select the code, and put it between [code][/code] tags.

You can do that by hitting the # button above the posting area.

golnar: But how can I put all of them in a same loop?

http://www.gammon.com.au/blink

golnar: But how can I put all of them in a same loop?

Rather than have your sketch stop and wait until it's time to do something, you have a loop which implements this logic:

Is it time to move servo 1 yet? If so, move servo 1 to its new position.
Is it time to move servo 2 yet? If so, move servo 2 to its new position.
Is it time to move servo 3 yet? If so, move servo 3 to its new position.
... and so on for anything else you need to do on a timed basis