0

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
  }

 }
 }
 }
4
  • Can you explain more? I still don't get the "random" pattern
    – duck
    Nov 22, 2016 at 1:39
  • Yeah so it is basically going to be a game where you roll a random number and when you get that number servos will open. For example I roll a 2 on my first time so servo 1,2, and 3 will open. 1 will open because that is where a ball starts. Then on the next turn I roll a 5 so servos 4,5,6,7,8, and 9 open. Does that make more sense?
    – mads046
    Nov 22, 2016 at 3:51
  • if "n" is a random number, the opened servo are servo(0+prevpos+1) through servo(n+prevpos+1)? correct?
    – duck
    Nov 22, 2016 at 3:58
  • I think that is it
    – mads046
    Nov 22, 2016 at 4:01

1 Answer 1

1

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]--> pin 0
                  // servo[1]--> pin 1
                  // servo[2]--> pin 2
                  // servo[3]--> pin 3
                  // servo[4]--> 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 < 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 < randomNumber; x++) { 
  myservo[x+pos].write(180);
 }
 delay(2000);

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.