Prevent All LEDs from Turning On

Hello!

I'd like to set multiple LEDs to blink randomly and concurrently, but prevent the situation where all LEDs are on at the same time.
The code I'm using to control the random blinking is as follows:

const int ledRed = 13;
const int ledGreen = 9;
const int ledYellow = 6;

void setup() {
  pinMode(ledRed, OUTPUT);
  pinMode(ledGreen, OUTPUT);
  pinMode(ledYellow, OUTPUT);  
}

void loop() {
  digitalWrite(ledRed, random(2));
  digitalWrite(ledGreen, random(2));
  digitalWrite(ledYellow, random(2));
  
  delay(500);
}

I think it has something to do with Arrays and if-statements, but I'm getting turned around. I'm still quite new and coding and Arduino in general, so any help would be appreciated.

Thank you!

rosspapa:
Hello!

I'd like to set multiple LEDs to blink randomly and concurrently, but prevent the situation where all LEDs are on at the same time.

Concurrently MEANS all at the same time. If you can't precisely describe what you want, how are we to know?

const int ledRed = 13;
const int ledGreen = 9;
const int ledYellow = 6;

void setup() {
  pinMode(ledRed, OUTPUT);
  pinMode(ledGreen, OUTPUT);
  pinMode(ledYellow, OUTPUT);  
}

void loop() {

byte bits=random(7);
//bits will hold 0 to 6 inclusive,  will never hold 7
  
  digitalWrite(ledRed, bits & 1 );
  digitalWrite(ledGreen, bits & 2 );
  digitalWrite(ledYellow, bits & 4 );
  
  delay(500);
}

Henry_Best:
Concurrently MEANS all at the same time. If you can't precisely describe what you want, how are we to know?

Thanks for pointing that out, Henry_Best! Are you asking for clarification or is your intention to point out my lack of clarity? In case it's the former, let me try it again:

I hope this clarifies things to those who'd like to help! :slight_smile:

KenF:

const int ledRed = 13;

const int ledGreen = 9;
const int ledYellow = 6;

void setup() {
  pinMode(ledRed, OUTPUT);
  pinMode(ledGreen, OUTPUT);
  pinMode(ledYellow, OUTPUT); 
}

void loop() {

byte bits=random(7);
//bits will hold 0 to 6 inclusive,  will never hold 7
 
  digitalWrite(ledRed, bits & 1 );
  digitalWrite(ledGreen, bits & 2 );
  digitalWrite(ledYellow, bits & 4 );
 
  delay(500);
}

Thanks KenF, I'll give this a try!