I am using this to randomly pick a pin from my array to turn them on/off...but it is not very random at all. In fact it is quite predictable. Is there a better approach?
const byte servoPins [] = {2, 4, 7, 9};
int restart = 0;
int rndmServo = 0;
void setup() {
// set pins as output
pinMode(2, OUTPUT);
pinMode(4, OUTPUT);
pinMode(7, OUTPUT);
pinMode(9, OUTPUT);
}
void loop() {
// if restart var == 0
if(restart == 0){
// start 10 run loop
for (int i = 0; i < 10; i++) {
// pick random
rndmServo = servoPins [random (0, 4)];
// move to 90
digitalWrite(rndmServo, HIGH); // turn the LED on (HIGH is the voltage level)
// pause
delay(1000); // wait for a second
// move to 0
digitalWrite(rndmServo, LOW); // turn the LED off by making the voltage LOW
// pause
delay(1000); // wait for a second
// end 10 run loop
}
// reset restart var = 1
restart = 1;
// end if restart var == 0
}
}
... this is eventually going to be for a bank of servos(hence the reference to servos), but I am just trying with led's to keep it simple for now
Another possible technique is to wait for a button press and use the value of millis() or micros() when the button is pressed as the parameter for randomSeed()
gruant2000: @vaj4088 ..
oops, I must have forgotten to paste that line here.... it was
int rndmServo = 0;
I will edit the op, and I will look into that random seed
and I am always getting the same sequence of numbers on reset
random() does not generate true random numbers. It generates pseudo-random sequence with a random (high entropy) distribution of output values. if it always gets the same seed, it will always generate the same sequence. If you want a different sequence each time your program runs, then you need to give it a different seed each time you run. This is usually done by basing the seed on some random natural event, like the response time of the user, or the time between your program starting, and some external event that is not timed off your program starting. Computers do not have true random number generators.