Pages: [1]   Go Down
Author Topic: possible to random choose a value from an array?  (Read 777 times)
0 Members and 1 Guest are viewing this topic.
Central Indiana, USA
Offline Offline
Full Member
***
Karma: 0
Posts: 203
So many projects, so little time...and money!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 285
Posts: 25623
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

http://arduino.cc/en/Reference/Random
Logged

"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.

Central Indiana, USA
Offline Offline
Full Member
***
Karma: 0
Posts: 203
So many projects, so little time...and money!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 285
Posts: 25623
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)];"

« Last Edit: June 12, 2009, 08:25:28 am by AWOL » Logged

"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.

Central Indiana, USA
Offline Offline
Full Member
***
Karma: 0
Posts: 203
So many projects, so little time...and money!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 285
Posts: 25623
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"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.

Pages: [1]   Go Up
Jump to: