Go Down

### Topic: Thoughts on how to program this array (Read 1 time)previous topic - next topic

#### stoopkid

##### Feb 25, 2011, 01:34 pm
I'm not looking for someone to code it for me, just some ideas on how to approach it... I'm fairly amateur.

Anyway, I want to have X number of LEDs and a potentiometer. As you fade the pot up, random LEDs turn on. So at half turned, a random 50% of the LEDs are on, at full turn, all the LEDs.

Simple enough so far. But here's the thing, I don't want it to rerandomize all the lights as it fades up. I just want it to add to the ones already on. So no static TV look. So what I need advice on is how to efficiently figure out which LEDs are not on yet, then randomly choose an off one to turn on.

Then there's the fade down, I need to find an efficient way to choose random on LEDs to turn back off as it fades back down.

So basically I need help on how to create an array, then how to interact with objects in that array based on their values, in groups.

The fact that they will be LEDs is not important, I'm just interested on how to do these things with an array.

#### stoopkid

#1
##### Feb 25, 2011, 01:45 pm
Now that I think of it, I'm thinking I could have two arrays. The OFF array and the ON array. Then move the LEDs from one to the other. Is this a good approach?

#### PaulS

#2
##### Feb 25, 2011, 02:14 pm
Quote
Now that I think of it, I'm thinking I could have two arrays. The OFF array and the ON array. Then move the LEDs from one to the other. Is this a good approach?

You could do that, but, think about this for a minute. You have one array. Each position in the array corresponds to an LED. The LED is either ON or OFF, so the value in the array is either HIGH or LOW.

If you have two array, OFF, and ON, what do the positions in the array correspond to?

Upon further reflection, you really do need two arrays - an array of pin numbers and an array of states.

When the number of LEDs on is too high or too low, you must choose an LED to change the state of.

So, you set up an infinite loop. In that loop, you generate a random number between 0 and the number of LEDs you have. If the state of the LED in that position in the array is the same as the new state (already on or already off, do nothing. The loop will repeat, and pick a new random number.

On the other hand, if the state of the LED in that position in the array is not the same as the new state, change the state of that position in the array, and of the corresponding pin number, and break out of the loop.

#### Fletcher_Chr

#3
##### Feb 25, 2011, 02:18 pmLast Edit: Feb 25, 2011, 02:21 pm by Fletcher Chr Reason: 1
Hi Stoopkid

I would create an array with the LED numbers.
When potentiometer value is so low that all LED's are off I would shuffle the array.
When potentiometer value gets to the point where no. 1 LED is lit: Stop shuffleing and turn on/off the LEDs according the the order in a array.
Reshuffle when potentiometer again is low.

-Fletcher

Update:
Quote
Then there's the fade down, I need to find an efficient way to choose random on LEDs to turn back off as it fades back down.

I'm not sure if you want same order down as up. My approch would give the same order.

#### stoopkid

#4
##### Feb 25, 2011, 02:25 pm
Paul,
My only concern with that is, if I've got 100 LEDs and 99 are on and I need to find that last one by randomly checking, it may take 100 tries or more to find it. Let alone, all the other higher numbers, or lower as I'm going down.

Fletcher, that's a very good idea but ideally I would like it to be totally random whenever changing. So if you're at 50% then take it to 90% then back to 50% you'd have many different ones lit. I know I'm asking for a lot more complication, but I'd like to have it work like that if I can.

Thanks guys

#### Fletcher_Chr

#5
##### Feb 25, 2011, 02:32 pmLast Edit: Feb 25, 2011, 02:42 pm by Fletcher Chr Reason: 1
Hi

Keep track of current location = numbers of LED's lite.
Going up: shuffle from current location to end of array. Lite up the next one in the array (current +1).
Going down: shuffle from 0 to current location. Turn off the pervious one in the array (curren - 1).

-Fletcher

Update:
Going down will not work correct. you have to:
shuffle from 0 to current location (including current location), turn off current location and then redice current location by one.

#### PaulS

#6
##### Feb 25, 2011, 02:43 pm
Quote
My only concern with that is, if I've got 100 LEDs and 99 are on and I need to find that last one by randomly checking, it may take 100 tries or more to find it. Let alone, all the other higher numbers, or lower as I'm going down.

True, but the Arduino can generate random numbers pretty darn quick.

Just out of curiosity, how will you connect (and power and individually control) that number of LEDs?