Controlling 8 servos


looking for advice as new to this

I need to control 8 servos using an UNO in the following way.

Each servo must move 45 Deg for 3 seconds and return to rest position

There must be a random time gap of between 7 and 15 seconds between servo operations

The board must operate the servos 1 to 8 selecting one randomly repeating this for 10 cycles then stop

The cycle must be triggered by a button press.

The origional design called for solenoids to be used but this has now become servos below is a code for solenoid use. Can this be modified to operate with servos instead

int ranNum;
int ranDel;
int count = 1;
int buttonState;
const int buttonpin = 11;

void setup() {
// Seed RNG from analog port.
// Setup 8 output ports for LED’s
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(buttonpin, INPUT);

void loop()

buttonState = digitalRead(buttonpin);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
count = 1;
else {
count = 0;

for (int count = 1; count <= 10; count++) {

// Generate random delay time

//Generate random number between 2 and 10

//Turn on the LED
digitalWrite(ranNum, HIGH);

//Turn off the LED
digitalWrite(ranNum, LOW);

Any help would be greatly appreciated

Have you looked at the Servo library in the reference section.

You create a servo instance with (for example) Servo myServo; and you specify the pin that controls the servo with myServo.attach(servoPin);

You can create an array of servo instances Servo myServoArray[8]; and then attach them each to a pin - for example

for (byte n = 0; n < 8; n++) {
  myServoArray[n].attach(n + 3); // attaches the servos to pins 3 to 10

Then your random number between 0 and 7 can be used to operate a servo like this

myServoArray[myRandomIndex].write(45); // move to 45 deg