This should get you close. It basically picks one of the pins from the array and uses it. It then swaps the last element of the array into the used slot and reduces the count of pins left to choose from.
FYI.. if you don't put in the randomSeed() function, every time you run the code, you will get the exact same sequence of "random" numbers so it is good to seed it.
byte pins[] = { 3,4,5,6,7,8,9,10,11,12,13 };
int pinCount = sizeof(pins) / sizeof(pins[0]);
const byte pinButton = 2;
void setup () {
for (int i = 0; i < pinCount; i++) {
pinMode(pins[i], OUTPUT) ; // setup all of the neccesary LED pins
}
pinMode(pinButton, INPUT) ; // setup the button pin
Serial.begin(9600) ; // setup the serial for debugging
randomSeed(analogRead(A0)); // so you don't get the same sequence every time you run
}
void LightSequence() {
int randomNum = random(pinCount) ; // pick random light to turn on.
int pin = pins[randomNum];
pins[randomNum] = pins[pinCount-1];
pinCount--;
Serial.println(randomNum); // print the chosen light in the serial
digitalWrite(pin, HIGH) ; // turn on the randomly chosen light
int randomTime = random(500, 3000) ; // pick a random amount of time to wait, the lowest being the minimum and the highest being the maxium
Serial.println(randomTime); // print the chosen time in the serial
delay(randomTime) ; //wait the random time
digitalWrite(pin, LOW) ; // turn off the light
}
void LightDisplay() {
LightSequence() ; // trigger a random light, and wait for it to turn off
LightSequence() ; // trigger a random light, and wait for it to turn off
LightSequence() ; // trigger a random light, and wait for it to turn off
LightSequence() ; // trigger a random light, and wait for it to turn off
Serial.println("End"); //print end in the serial to show when the sequence has ended
}
void loop () {
int button = digitalRead(pinButton) ; //setup the button's value to be used
if (button == HIGH) { //when the button is pressed
LightDisplay() ; //trigger the light sequence
}
}
NOTE: if you press the button multiple times, pinCount will go below zero! Fixing that is up to you ![]()