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) {
  } 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);
    digitalWrite(randomNumber, HIGH);
   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);

Various suggestions:

Create 2 different arrays of pin numbers: 1 for the first group of relay pins, and the 2nd for the second group of relay pins:

byte firstRelayPins[] = [7, 8, 9, 10, 11, 12];
byte secondRelayPins[] = [13, 14]; //Replace values with your relay pins

Write a function that returns a random index from one or the other of the arrays based on a bool:

byte randomPin(bool useSecondArray) {
    int max; 
    int index;
    if useSecondArray {
       return secondRelayPins[random(sizeof(secondRelayPins))]; //Fetch random pin from array
    } else {
       return firstRelayPins[random(sizeof(firstRelayPins))]; //Fetch random pin from array

Based on the state of your switch, set useSecondArray when you call that function to determine which relay's pin you turn on.

Use the returned value to set the desired relay pin.

In your "turn everything off" code, use the arrays of index pins rather than hard-coded indexes.

You should also refactor your code to use millis() rather than using delay(). (Search on "Arduino blink without delay" for guidance on how to do that.)

Note that this is a rough outline of what to do. The code above will likely need to be adjusted to fit your needs. Don't expect to be able to copy/paste it into your project.

  • Does that help? – Duncan C Oct 9 '19 at 1:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.