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() {
myservo1.attach(10); // attaches the servo on pin 9 to the servo object
myservo2.attach(9); // attaches the servo on pin 8 to the servo object
myservo3.attach(8); // attaches the servo on pin 7 to the servo object
}
void loop() {
int randNumber = random(1, 3);
Serial.println(randNumber);
int del = random(1000,16000);
Serial.println(randNumber);
delay (del);
if (randNumber == 1){
// in steps of 1 degree
myservo1.write(90);
}
if (randNumber == 2){
// in steps of 1 degree
myservo2.write(90);
}
if (randNumber == 3){
// in steps of 1 degree
myservo3.write(90);
}
delay(2000);
myservo1.write(pos); // tell servo to go to position in variable 'pos'
myservo2.write(pos);
myservo3.write(pos);
// waits 15ms for the servo to reach the position
}
Have you read the tutorial? The only thing you need to do is power the servos with a different supply. Do not power servos off the arduino.
Please use code tags when posting code. Do you see the smiley in your post? You also need to explain what it is you're trying to do versus what its doing now.
It's not a bad idea to make the comments match the code:-
myservo1.attach(10); // attaches the servo on pin 9 to the servo object
myservo2.attach(9); // attaches the servo on pin 8 to the servo object
myservo3.attach(8); // attaches the servo on pin 7 to the servo object
The 'max' value for 'random()' is exclusive, so this:-
int randNumber = random(1, 3);
.
.
.
if (randNumber == 3)
{
// in steps of 1 degree
myservo3.write(90);
}
will never move myservo3.
'random()' will only return 1 or 2.
And as pointed out by codlink, your code should be between code tags and not inline. Code tags are produced using the </> button.
It's not too late to edit your post and do this, to make it more readable. (Then the '8' in 'myservo3.attach()' won't look like this:- 8) )
Cross-posting, ignoring multiple requests to use code tags, taking absolutely no notice of suggestions made by those trying to help ...... another wonderful example of "How to Lose Friends and Alienate People."