how to repeat a program

Hi! :slight_smile:

I'm making a program that lights leds randomly...

for (i = 0; i< 6; i ++)
{
light = pinArray[analogRead(0) % 6];
digitalWrite(light, HIGH);
delay(100);
}

...and when all lights are lit, it turns them off ...

for (j = 0; j <6; j ++) {
digitalWrite (pinArray[j], LOW);
delay(100);
}

...and starts over again.

But i can't connect these 2 parts together and i don't know how to make the hole program repeat itself.

hi,

first, I hope you are aware that it is not guaranteed that the first loops light up ALL the leds because your random number expression may return the index of a pin that has already been lit! To fix this, you could, for example, use another character array of the same length as pinArray where you store a 1 if the corresponding pin has already been lit and a 0 otherwise. In the second loop, you set all flags back to zero again (you could also use a bitfield for that to save space if you are comfortable with bit logic).

Anyways, have you simply tried putting these two loops into the loop() function of a sketch? I'm just starting out myself, but as far as I know, loop() is called over and over again whereas setup() is called exactly once, before the first call to loop().

Maybe I'm overlooking something, but I'm quite sure that this should do the trick. If it didn't work before, maybe the LEDs aren't connected correctly? The only drawback is that delay() totally blocks the board, but this is not a problem unless you want to communicate with the board via serial or have it do something else besides playing with the LEDs...

good luck!

Thank you gck, i tried what you told me, but not all leds light (i can't figure out what i've done wrong). Anyway, here is my code if someone has time to help me :

int pinArray [] = {
4,5,6,7,8,9 };
int lit [] = {
0,0,0,0,0,0 };
int lineSize = 6;

void setup()
{
int i;
for (i=0; i< lineSize; i++)
{
pinMode(pinArray*, OUTPUT);*

  • }*
    }
    void loop() {

  • int light; *

  • int i;*

  • int j;*

  • for (i = 0; i< 6; i ++) *

  • { *

  • light = pinArray[analogRead(0) % 6]; *

  • if (lit [light] == 0) {*

  • digitalWrite(light, HIGH); *

  • lit[light] = 1;*

  • delay(100); *

  • }*

  • }*

  • for (j = 0; j <6; j ++) {*

  • digitalWrite (pinArray[j], LOW);*

  • }*

  • delay (1000);*
    }
    }

You forgot to actually check if the light selected by the random number generator has already been lit. If it's already shining, you have to select another one until you find one that has not been lit yet. Also you have to be careful not to confuse things: lit and pinArray use the same index, but you will get a program crash with your old code because you set light to the value IN pinArray rather than the index, so if light was set to 9, for example, you would then try to read lit[9] which doesn't exist since lit has (like pinArray) only 6 entries, indexed from 0 to 5. C isn't very forgiving in respect to out of bounds indexing...

Try to replace this loop

for (i = 0; i< 6; i ++)
{
light = pinArray[analogRead(0) % 6];
if (lit [light] == 0) {
digitalWrite(light, HIGH);
lit[light] = 1;
delay(100);

}
}

with this

for (i = 0; i< 6; i ++)
{
do {
light = analogRead(0) % 6;
} while (lit[light]);

digitalWrite(pinArray[light], HIGH);
lit[light] = 1;
delay(100);
}

Note the use of do-while which translates to "get a new pin until you find one that hasn't been lit yet". Also notice how "light" doesn't hold the pin any more but rather its index.

Then in the second loop (where you digitalWrite all LED pins to LOW), you also have to set all entries in "lit" to 0 again so that it is prepared for the next iteration of loop(). Just write "lit[j] = 0;" directly after the digitalWrite(pinArray[j], LOW)...

One more thing: The above code will infinitely loop in do-while if the random number selection doesn't produce enough different numbers. I'm not sure if reading an unconnected pin (I assume, analog pin 0 is unconnected in your example) is reliable in this respect.

Is there any reason not to use the standard C pseudo-random numbers (I'm not sure, I'm new to this too). You could try appending "srand(analogRead(0));" to your setup() function and then use "random() % 6" instead of "analogRead(0) % 6" in loop(), if it works, I'd rather leave it this way... Just run it a few times to make sure the random numbers are really different every time you reset your board (if srand() does nothing on Arduino, you would always get the same numbers).

hope this helps!

Thanks a lot!!! :slight_smile:
I'm taking a look at it

Yep it works! :wink: