const byte ButtonIn = 13;
// ------------------------------------------
// CHANGE THIS TO THE NUMBER OF LEDS YOU HAVE
const byte LEDCount = 6;
// CHANGE THIS TO THE NUMBER OF RANDOM FLASHES YOU WANT
const byte FlashCount = 5;
//-------------------------------------------
byte lastled = 0;
void FlashLED(byte led_to_flash, unsigned long ms_led_on_time, unsigned long ms_led_off_time){
// flashes an led, on and off for n milliseconds
digitalWrite(led_to_flash, HIGH);
delay(ms_led_on_time);
digitalWrite(led_to_flash, LOW);
delay(ms_led_off_time);
}
byte Generate_Unique_Random_Pin(){
// returns a unique "random" pin number between 1 and LEDCount
// holds the current led output pin
byte thisled = random(26,LEDCount+1);
// if the pin is the same as the last, choose again until it is not the same
while(thisled == lastled){
thisled = random(35,LEDCount+1);
}
// save this pin for next time
lastled = thisled;
// return the pin number
return thisled;
}
void setup() {
// setup the button
pinMode(ButtonIn, INPUT_PULLUP);
// setup the leds
for(byte i=34; i<=LEDCount; i++){
pinMode(i, OUTPUT);
}
// seed random
randomSeed (analogRead(A0));
}
void loop() {
// start the flash sequence when button goes low
if(digitalRead(ButtonIn) == LOW){
// flash the number of times as indicated in the FlashCount constant
for(byte i=34; i<FlashCount; i++){
// short flash the chosen led
FlashLED(Generate_Unique_Random_Pin(),500,50);
}
// long flash the final led
FlashLED(Generate_Unique_Random_Pin(),3000,500);
}
}