Controlling 3 different micro servos at the same time

I have been able to make one servo work through this tutorial. https://www.arduino.cc/en/Tutorial/Sweep

However, for my Arduino project I need to make each of them work randomly.

How can I achieve this goal?..

Someone please help!

I am new to the Arduino world.

Thank you

You need to look into using millis() for timing as in the BlinkWithoutDelay example and Several things at the same time
Save the time a servo move happens then each time through loop() check whether the required period has elapsed since the movement. If not then go round loop() again reading inputs etc. If the period has elapsed then take the required action. You can do this independently for 3 servos with the amount of sweep for each servo being random and different from the other two.

Try extending that code to use a second servo. If it does not work then post your code and tell us how it behaves.

And please use the code button </>

so your code looks like this

and is easy to copy to a text editor

...R

Robin2:
Try extending that code to use a second servo. If it does not work then post your code and tell us how it behaves.

And please use the code button </>

so your code looks like this

and is easy to copy to a text editor

...R

#include <Servo.h>

Servo myservo1; // create servo object to control a servo
// twelve servo objects can be created on most boards

Servo myservo2; // create servo object to control a servo

Servo myservo3; // create servo object to control a servo

int pos = 0; // variable to store the servo position

void setup() {

Can you guys help me improve this
myservo.attach(9); // attaches the servo on pin 9 to the servo object
myservo.attach(8); // attaches the servo on pin 8 to the servo object
myservo.attach(7); // attaches the servo on pin 7 to the servo object
}

void loop() {
int randNumber = random(1, 3);
Serial.println(randNumber);

int del = random(1000, 16,000);
Serial.println(randNumber);

delay (del);

if randNumber = myservo1

// in steps of 1 degree
myservo1.write(90);
// in steps of 1 degree
myservo2.write(90);

// in steps of 1 degree
myservo3.write(90);

delay(2000);
myservo.write(0); // tell servo to go to position in variable 'pos'
// waits 15ms for the servo to reach the position
}
}

THIS IS WHAT I HAVE WRITTEN SO FAR.. please help..

UKHeliBob:
You need to look into using millis() for timing as in the BlinkWithoutDelay example and Several things at the same time
Save the time a servo move happens then each time through loop() check whether the required period has elapsed since the movement. If not then go round loop() again reading inputs etc. If the period has elapsed then take the required action. You can do this independently for 3 servos with the amount of sweep for each servo being random and different from the other two.

#include <Servo.h>

Servo myservo1; // create servo object to control a servo
// twelve servo objects can be created on most boards

Servo myservo2; // create servo object to control a servo

Servo myservo3; // create servo object to control a servo

int pos = 0; // variable to store the servo position

void setup() {

Can you guys help me improve this
myservo.attach(9); // attaches the servo on pin 9 to the servo object
myservo.attach(8); // attaches the servo on pin 8 to the servo object
myservo.attach(7); // attaches the servo on pin 7 to the servo object
}

void loop() {
int randNumber = random(1, 3);
Serial.println(randNumber);

int del = random(1000, 16,000);
Serial.println(randNumber);

delay (del);

if randNumber = myservo1

// in steps of 1 degree
myservo1.write(90);
// in steps of 1 degree
myservo2.write(90);

// in steps of 1 degree
myservo3.write(90);

delay(2000);
myservo.write(0); // tell servo to go to position in variable 'pos'
// waits 15ms for the servo to reach the position
}
}

THIS IS WHAT I HAVE WRITTEN SO FAR.. please help..

delay(2000);
      myservo1.write(pos); 
      myservo2.write(pos); 
      myservo3.write(pos);

Test code for controlling several servos.

//zoomkat 11-22-12 simple delimited ',' string parse 
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added 
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h> 
Servo myservoa, myservob, myservoc, myservod;  // create servo object to control a servo 

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

  //myservoa.writeMicroseconds(1500); //set initial servo position if desired

  myservoa.attach(6);  //the pin for the servoa control
  myservob.attach(7);  //the pin for the servob control
  myservoc.attach(8);  //the pin for the servoc control
  myservod.attach(9);  //the pin for the servod control 
  Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded
}

void loop() {

  //expect single strings like 700a, or 1500c, or 2000d,
  //or like 30c, or 90a, or 180d,
  //or combined like 30c,180b,70a,120d,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.println(readString); //prints string to serial port out

        int n = readString.toInt();  //convert readString into a number

        // auto select appropriate value, copied from someone elses code.
        if(n >= 500)
        {
          Serial.print("writing Microseconds: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
          if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
          if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
          if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
        }
        else
        {   
          Serial.print("writing Angle: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.write(n);
          if(readString.indexOf('b') >0) myservob.write(n);
          if(readString.indexOf('c') >0) myservoc.write(n);
          if(readString.indexOf('d') >0) myservod.write(n);
        }
         readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

Cross post, Anyone have idea on how I can connect all three micro servos to my Arduino? - Project Guidance - Arduino Forum

myservo.attach(8); // attaches the servo on pin 8 to the servo object

If only you had used code tags a suggested, then the code would have been readable

 myservo.attach(8);  // attaches the servo on pin 8 to the servo object