Code Problem- Two motors need to work at once in simple sketch

I’m working on a project where I want two servo motors to go to a different random angle Random(30, 180) . The 2 motors should be travelling at the same time, going to their respective random angle and then returning.

I believe the delay() is causing the two motors not to be working together. Is there anyway I could work around this? I need the motors to be running at the same time. I have added the code I’m working with below.

C

void loop() {

for(pos1 = v2; pos1 < 179; pos1 += 1) {

v2 = random(30,180);
servo1.write(pos1);
delay(50);

}

for(pos1 = 179; pos1 > v2; pos1 -= 1) {

servo1.write(pos1);
delay(50);

}

for(pos2 = v1; pos2 < 179; pos2 += 1) {

v1 = random(30,180);
servo2.write(pos2);
delay(50);

}

for(pos2 = 179; pos2 > v1; pos2 -= 1) {

servo2.write(pos2);
delay(50);
}
}

`````` for(pos1 = v2; pos1 < 179; pos1 += 1)  {

v2 = random(30,180);
servo1.write(pos1);
delay(50);
}
``````

Probably work better if you assigned a value to v2 BEFORE using it in the for loop.

You have one loop to move each servo. The 2nd servo doesn’t start until the 1st one completes its motion.

Use just one loop, moving both servos in that loop.

Please select the code and hit the # icon before posting it.

You can simply command the two servos to assume the position without doing any loops.

``````servo1.write(random(30,180));
servo2.write(random(30,180));
``````

All the loop does is slow down the time it takes to get the servos to the position.

If you want them slowed down together then have one loop and a separate increment for each servo taking it from the current position to the new position in as many steps as the loop takes.

Thanks Paul, but I’m still having some issues.

Your suggestion makes the servos jump rapidly (after the for loop is complete) to the freshly generated random angle. I need the movement of the servo arm to be smooth and slow and then have each arm return to the original random position.

Also, the motors still won’t work at the same time.

Mike, thanks for your reply. I need the motors to move slowly and not become jumpy. That’s why I had them in the for loop, so I could control the delay in between in increment.

Do you know if there’s any other way?

Do you know if there's any other way?

Yes I said:-

If you want them slowed down together then have one loop and a separate increment for each servo taking it from the current position to the new position in as many steps as the loop takes.

Like this? This is still moving way too fast.

I think the delay is causing each motor not to work at the same time. Maybe I need to figure out how to use the millis().

``````myservo10.write(10);
myservo10.write(11);
myservo10.write(12);
myservo10.write(13);
myservo10.write(14);
myservo10.write(15);
myservo10.write(16);
myservo10.write(17);
myservo10.write(18);
myservo10.write(19);
myservo10.write(20);
myservo10.write(21);
myservo10.write(22);
myservo10.write(23);
``````

Like this?

No,
use a loop I said, here is a code fragment to give you the idea:-

``````float pos1dest, pos2dest, inc1, inc2;
float pos1Now=0, pos2Now=0;
int steps = 100; // how smooth you want the change

// generate the destanation angles
pos1dest = random(30,180);
pos2dest = random(30,180);
// calculate the increment rate
inc1 = (pos1dest-pos1Now) / float(steps);
inc2 = (pos2dest-pos2Now) / float(steps);

for(int i=0; i< steps; i++){
pos1Now += inc1;
pos2Now += inc2;
servo1.write(pos1Now);
servo2.write(pos2Now);
delay(50); // or what ever you want
}
``````

Mike, this is great! Thanks very much.