I have a project which has two options No. 1 Randomly turn on one LED from a group of 6 LED (Relay Board) No.2 randomly turn on one LED from a group of 2 LED (Relay Board) The selection of the desired option happens with a switch.
I found this code online which is just perfect for 6 LED but I don’t know how to add the selection of the options in it, how to do it?
int timeShowRandom = 3000;
int timeShowDecision = 6000;
int timeBlink = 100;
int buttonPin = 2;
int buttonPress = false;
int randomNumber;
int previousNo = 0;
int timePassed = 0;
void setup() {
// Set button pin
pinMode(buttonPin, INPUT);
// Set output pins
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
pinMode(8, OUTPUT);
pinMode(7, OUTPUT);
}
void getRandomNo() {
int rand = random(7,13);
if (rand == previousNo) {
getRandomNo();
} else {
randomNumber = rand;
previousNo = randomNumber;
}
}
void loop() {
if (digitalRead(buttonPin) == HIGH && buttonPress == false ) {
buttonPress = true;
} if (buttonPress == true && timePassed <= timeShowRandom) {
getRandomNo(); // Get random pin number
digitalWrite(randomNumber, LOW);
delay(timeBlink);
digitalWrite(randomNumber, HIGH);
delay(timeBlink);
timePassed = timePassed + (timeBlink * 2);
} else if(buttonPress == true) {
digitalWrite(random(7,13), LOW); // Set random pin on
delay(timeShowDecision); // For x seconds
buttonPress = false; // Set button to be enabled again
timePassed = 0;
}
else {
// Reset all output pins
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
digitalWrite(12, HIGH);
digitalWrite(9, HIGH);
digitalWrite(8, HIGH);
digitalWrite(7, HIGH);
}
}