generate a series of random numbers

How can i generate a series of random number to select multiple positions of an array?

I need to set on which loop, a random number of servos moving and also on a random order.

So far I managed to do it like this but I can't seem to find a way to associate a random number with which of servos to move.

code

#include <Servo.h>

/////////////
Servo s2; // #######################
Servo s3; // # #
Servo s4; // # (2) (3) (4) #
Servo s5; // # #
Servo s6; // # (5) (6) (7) #
Servo s7; // # _ #
// #######################
/////////////

Servo exec[] = {s2, s3, s4, s5, s6, s7};

int whichMotors;
int quantatyMotor;

void setup(){

Serial.begin(9600);

s2.attach(2);
s3.attach(3);
s4.attach(4);
s5.attach(5);
s6.attach(6);
s7.attach(7);

randomSeed(analogRead(0));
}

void loop(){

quantatyMotor = random(0, 6);

for (int i=0; i <= quantatyMotor; i++){

Serial.println("I");
Serial.println(i+2);
Serial.println("QUANTATY OF MOTORS");
Serial.println(quantatyMotor+1);

whichMotors = random(0, 6);

exec*.write(10);*

  • delay (100);*
    _ exec*.write(40);*_

* Serial.println("MOTOR");*
* Serial.println(whichMotors);*

* }*

delay(4000);
} [/font]
---------------------------------------------------------------------------
What i need is to move for instance the 3rd and the 5th motor, afterward the 1st, the 2nd, and the 6th, and next the 1st, the 4th the 5th and the 6th.
Sometimes only one, and sometime all of them.
Any help greatly appreciated!
Cheers!

Untested but try this:

#include <Servo.h> 

const int NumberMotors = 6;  // the number of servos

Servo exec[NumberMotors ] ; //create the servo array

// here is how servo numbers (S0-S5) map to pins
///////////// 
 //     ########################
 //     #                      #
 //     #  S0=P2  S1=P3 S2=P4  #
 //     #                      # 
 //     #  S3=P5  S4=P6  S5=P7 #
 //     #                      #
 //     ########################
/////////////

int whichMotors;
int quantatyMotor;

void setup(){ 
 Serial.begin(9600);
 
 exec[0].attach(2); 
 exec[1].attach(3);
 exec[2].attach(4);
 exec[3].attach(5);
 exec[4].attach(6);
 exec[5].attach(7);
   
 randomSeed(analogRead(0));
} 

void loop(){
 
 quantatyMotor = random(0, 6);
   for (int i=0; i <= quantatyMotor; i++){     
     Serial.println("I");
     Serial.println(i+2);
     Serial.println("QUANTATY OF MOTORS");
     Serial.println(quantatyMotor+1);
     
     whichMotors = random(0, 6); 
      exec[i].write(10);
      delay (100);
      exec[i].write(40);
      
     Serial.println("MOTOR");
     Serial.println(whichMotors);     
  }  
delay(4000);

}

this seams to me as a different way of creating an array with for the servos. It doesn't help me with the writing on a random set of servos.

thanks any way

It looks like you are writing to a random set of servos. Perhaps you can explain what functionality is missing from the code (I have not actually run it). What serial output do you get and what do you expect?

/me is just guessing
I see you have whichMotors = random(0, 6); but you never use it (for other than printing).

You are indexing your Servo array with i, and i is never going to be random unless you tell it to.

I suspect you want something that looks like this:

whichMotors = random(0, 6);
exec[whichMotors].write(10);
delay (100);
exec[whichMotors].write(40);

You might even want to assure that the whichMotor is within the range of [0, quantatyMotor]

i tried that... unfortunately whichMotors = random(0, 6); only returns 1 random number. Therefor it will only write on one servo at a time.

The array contains the 6 servos. By creating a variable - quantatyMotor = random(0, 6) - that defines the quantity of servos to be activated in the for loop each time it runs I was able to make it write to a random quantity of servos.

However it always starts on the first servo since the variable that tells the servos to move is always a single int.

What i really need is for the loop to say something like this:

each time it runs, select a random group of servos between 0 and 6 and write on them.
This group may be composed of just one random servo or all of them, or just the number 4 and 2.

I don't know if I'm explaining myself right...

thanks

I need for the script to return something like this:

3, 4, 6

5, 6

2, 4

1, 2, 3, 5

3

1, 2, 4, 5, 6

...and so on,

a random selection of the array in a random quantaty

hope this makes it clear

So, call "random" more than once, for instance, a random number of times.

Then you need to do something like this:

//setup and other code

void loop(){

  int quantity = random(6);
  for (int i=0; i<quantity; i++){
    int servo = random(6);
    exec[servo].write(10);
    delay(100);
    exec[servo].write(40);
  }

}

How about this:

void loop()
{
  int quantity = random(64);
  /* use the lower 6 bits of the random number to determine which */
  /* servos to activate. */
  for (int i = 0; i < 6; i++)
    {
    if (bitSet(quantity,i))
       exec[i].write(10);
    }
  delay(100);
  for (int i = 0; i < 6; i++)
    {
    if (bitSet(quantity,i))
       exec[i].write(40);
    }
}

Note that this solution sometimes turns on no servos.

-Mike