How to have an arduino randomly pick to move 1 servo, 2 servos, or all 3 servos?

Hello,

I’m rather confused as to how to get the arduino to randomly pick to move 1 servo 180 degrees, 2 servos 180 degrees, or all 3 servos 180 degrees.

This is the current code I’m using which allows all 3 servos to move simultaneously. Would it be best to somehow make an array of loops? Can that even be done?

#include <Servo.h> 
 
Servo myservo3;  // create servo objects to control servo s
Servo myservo5;  
Servo myservo6;  
 
int ServoPosition3 = 0;
int ServoPosition5 = 0;
int ServoPosition6 = 0;


void PrintPosition() {
  Serial.print("Motor position is: ");
  Serial.print(ServoPosition3);               
  Serial.println(" degrees.");
}

void setup() {        
  
  myservo3.attach(9);  // attaches the servo on pin 3 to the servo object 
  myservo5.attach(10);  // attaches the servo on pin 5 to the servo object 
  myservo6.attach(11);  // attaches the servo on pin 6 to the servo object   

  // start serial port at 9600 bps:
  Serial.begin(9600);

}

void loop() {
  ServoPosition5 = 185 - ServoPosition3;
  ServoPosition6 = ServoPosition3;
  myservo3.write(ServoPosition3); // tell servo to go to position in variable 'ServoPosition' 
  myservo5.write(ServoPosition5);
  myservo6.write(ServoPosition6);  
  PrintPosition();
  delay(4);   // wait for servo to move
  if(ServoPosition3 == 0) {
        delay(random(1000, 50000));  // wait for servo to move if moving 180 degrees
        
  }
  ServoPosition3 = ServoPosition3 + 7;  // send servo to next position
  if(ServoPosition3 > 185) {  // reset position if max. rotation reached
    ServoPosition3 = 0;
    delay(random(1000, 50000));  // pause to let sero return
    
}
}

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. If your browser doesn’t show the posting toolbar then you can just manually add the code tags:
[code]``[color=blue]// your code is here[/color]``[/code]
Using code tags and other important information is explained in the How to use this forum post. Please read it.

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read.

The cleanest way to write it would be with an array of Servo objects. The rotation is done using a for loop that steps through a random portion of the array.

Sorry about that. I'm new here. I fixed the post to show the code as code vs trying to manipulate the forums. I'll look into your suggestion for the array of servo objects.

Look into the use of random(). For what you want, first follow @pert's advice on the array of servos. Your indexes would then be Servo[0], Servo[1] and Servo[2].

Declare an array, call it sorted[] = {0,1,2}

Then generate a random number between 1 and 3 which will determine how many servos to activate, call this ran_1 (or whatever).

Randomize sorted (you'll look this up yourself) which will shuffle the contents of the array sorted[].

Use ran_1 in a loop, index from 0 to (ran1 - 1) and then use the index with sorted (which is now random 0, 1 or 2) as the index into the servo array,

Servo[sorted[index]];