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 <Servo.h>
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 <= xPrevious; x += 1) {
myservo.attach(x);
Serial.println(x);
for (pos = 0; pos <= 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 >= 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
}
}
}
}