Servos and Random Number I am new to coding and this website so I apologize if I make any mistakes. I am trying to get servos to move when a random number is generated. Like if I roll a 2 I want servos 1, 2 and 3 to open and then if I roll a 3 after I want servos 4, 5 and 6 to open. I am able to hold the previous values in the code now but can't get the servos to go with it. Any help is great! Thanks!

#include &lt;Servo.h&gt;

Servo myservo;

int pos = 0;    // variable to store the servo     position
int x = 0;
int button = 8;
int BUTTON;
int randomNumber;
int xPrevious = 0;
int oldNumber;



void setup() {
pinMode(button, INPUT);
Serial.begin(9600);
}

void loop() {


BUTTON = digitalRead(button);
Serial.println(BUTTON);
 if(BUTTON == LOW){
  randomNumber = random(1,4);
  delay(275);
  Serial.print("Random Number ");
 Serial.println(randomNumber);
 if( xPrevious == 0) {
   randomNumber = randomNumber + 1;
 }


  // Only want it to do this on the first roll. 
     xPrevious = randomNumber + xPrevious;
     Serial.print("xPrevious ");
     Serial.println(xPrevious);
    oldNumber = xPrevious;


 for (x = randomNumber; x &lt;= xPrevious; x += 1) { 
  myservo.attach(x);
  Serial.println(x);

  for (pos = 0; pos &lt;= 180; pos += 1) { // goes     from 0 degrees to 180     degrees
 //in steps of 1 degree
    myservo.write(pos);              // tell servo to go   to position in variable 'pos'
    delay(15);                       // waits 15ms for the     servo to reach the position
  }
  for (pos = 180; pos &gt;= 0; pos -= 1) { // goes  from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go    to position in variable 'pos'
    delay(15);                       // waits 15ms for the  servo to reach the position
  }

 }
 }
 }
You planning to use multiple servo, but only declare one. 
I still don't get about the pattern, but maybe this will give some image:

Servo myservo[5]; // assuming you have 5-servo
                  // servo[0]--&gt; pin 0
                  // servo[1]--&gt; pin 1
                  // servo[2]--&gt; pin 2
                  // servo[3]--&gt; pin 3
                  // servo[4]--&gt; pin 4
pos = 0;

void loop() {

BUTTON = digitalRead(button);
Serial.println(BUTTON);
 if(BUTTON == LOW){
  randomNumber = random(1,4);
  delay(275);
  pos+=randomNumber;
  Serial.print("Random Number ");
  Serial.println(randomNumber);
  Serial.print("Current pos ");
  Serial.println(pos);


 for (x = 0; x &lt; randomNumber; x++) { 
  myservo[x+pos].attach(x+pos);
  Serial.print("attach servo-");
  Serial.println(x+pos); }


//open (move from 0-180 degrees)
 for (x = 0; x &lt; randomNumber; x++) { 
  myservo[x+pos].write(180);
 }
 delay(2000);

//close (move from 180-0 degrees)
 for (x = 0; x &lt; randomNumber; x++) { 
  myservo[x+pos].write(0);
 }
 delay(2000);

//detach servo
 for (x = 0; x &lt; randomNumber; x++) { 
  myservo[x+pos].detach();
 }
 delay(2000);
}
}