Three Servos on different loops

Hi

I'm trying to do the same thing with 3 servos. I can get a random timer but not to get them to run simultaneously but asynchronously.
My code is:

// Include the Servo library
#include <Servo.h>
// Declare the Servo pin
int servoPin1 = 1;
int servoPin2 = 2;
int servoPin4 = 4;
// Create a servo object
Servo Servo1, Servo2, Servo3;
void setup() {
// We need to attach the servo to the used pin number
Servo1.attach(servoPin1);
Servo2.attach(servoPin2);
Servo3.attach(servoPin4);
}

void loop(){
Servo1.write(90);
Servo2.write(0);
Servo3.write(90);
delay(random(1000, 5000));
Servo1.write(0);
delay (500);
Servo1.write(90);
delay(random(1000, 5000));
Servo2.write(90);
delay(500);
Servo2.write(0);
delay(random(1000, 5000));
Servo3.write(0);
delay (500);
Servo3.write(90);
}

The problem is they take turns to run.

Any advice for a newbie please?

Thanks,
Roxanne

Robin2's "many things" tutorial sweeps one servo delay()-lessly using millis().

The servos's moves are all handled in a function servoSweep() which is called from loop():

void loop()
{
...
updateLed_B_State();
switchLeds();
servoSweep(); //<<<<
}

... along with other stuff to show how things can all be mixed together elegantly.

So it should be straightforward to make other functions eg servo2Sweep()with other servos using different timing intervals and start and end points and call them in loop:

void loop()
{
servoSweep(); 
servo2Sweep(); 
//etc
}

... all merrily sweeping around independently of one another.

For bonus points and long-term simplicity, you could even manage the servos in an array.

I found this very good explanation in English as opposed to code, of how to manage servo timing. It's basically what Robin2's code does I think (haven't checked very closely):

UKHeliBob:
....don't move the servos from their current position to their target position all in one command. Move them a little every now and again using millis() for timing. Use a boolean variable to stop them moving when the target position is reached

For one servo, something like this:

start of loop()
  get current time from millis()
  if current time - previous move time >= wait period and the moving variable is true
     move a little
     save the time of the move
     change the target position a little
      if at the target position
        set the boolean to false
      end if
    end if
end of loop()

UKHeliBob:
You can extend this to multiple servos by using arrays for the values and an array of servos

Roxy_Roxy:
...
...

Please do not hijack threads; especially active ones. It can become very tricky to figure out who is replying to who.
Please read the 'how to use this forum' sticky, specifically the point about posting code.

@Roxy_Roxy

TOPIC SPLIT
DO NOT HIJACK !

Could you take a few moments to READ THIS.
It will help you get the best out of the forum.

Bob.

Thanks to those who tried to help me. I certainly didn't try to hijack anything! I'm brand new to this forum and was hoping to find help, not abuse.
Please remember we all started somewhere.
I don't think I'll be posting again after this experience. I'm gutted by the immediate attacks.

Roxy_Roxy:
Please remember we all started somewhere.
I don't think I'll be posting again after this experience. I'm gutted by the immediate attacks.

How many other Topics on this Forum had you read to learn how we work before you decided to post your question in another person's Topic?

If you were on your first visit to a new group of people you would not interrupt existing conversations to get your question answered. The same sort of politeness applies here.

...R

Roxy_Roxy:
I certainly didn't try to hijack anything!

Nobody said you did it on purpose, yet the result was there were 2 similar but different questions in one thread. While it may be possible to give generic help to 2 questions, once it got to the specifics it would have been impossible to follow.

Roxy_Roxy:
I don't think I'll be posting again after this experience.

That's a pity, since it's unlikely that you will get Arduino help of the calibre of this forum anywhere else.

Take a breath, get un-gutted, and see if you can use any of the advice given so far. Then try to incorporate that thinking in your code, and come back with specific questions if you need specific help.