Go Down

Topic: possible to random choose a value from an array? (Read 899 times) previous topic - next topic

flyboy

I am working on a firefly driver with my Arduino andI want to use the pwm outputs to drive the LEDs.  I plan to light only one LED at a time.  Since the PWM outputs are not all sequential, is it possible to use the Random() command to choose a value from an array?--each output pin would be a value in the array.  What's the best way to go about this?

AWOL

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

flyboy

I looked at the Random() command pretty closely before asking.  The problem I was having was that the PWM outputs are not all sequential, so you can't give it a limited range and expect it to always "land on" one of the preferred outputs.  I did find another way, though.  I used the random number generator with a range limitation.  I then used IF statements to tell it that numbers 0-24 = output 1, 25-48 = output 2, etc.  That seems to be about as clean as I can get it.

AWOL

#3
Jun 12, 2009, 02:55 pm Last Edit: Jun 12, 2009, 03:25 pm by AWOL Reason: 1
Quote
I looked at the Random() command pretty closely before asking

A lot of people don't - thanks!

If you put the PWM pin numbers into a constant array, thusly:
const byte pwmPins [] = {3, 5, 6, 9, 10, 11};
byte pwmPin;

And then use
"pwmPin = pwmPins [random (0, 5)];"

you'd get a random pin.

[EDIT] Oops - the docs say the max value is exclusive, so that should probably be
"pwmPin = pwmPins [random (0, 6)];"

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

flyboy

That works perfectly, see my work here: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1245067981

Thanks for your help, AWOL.  I just needed to get over the hump.

AWOL

A couple of years ago over at avrfreaks, there were a few firefly projects, using many individual processors, and algorithms to mimic the synchronisation of real fireflies.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up