Light random LEDs

Hi there, I’m pretty new to programming and I’m looking to light a random LED between the pins 3,5,6 and A0, I’m also trying to make a breathing effect on the led that lights up and light up the next when the first one is done with one breathing effect. Please help I’ve been trying to find the answer for more than 2 weeks now, is this even possible with Arduino?

I don't understand what a "breathing effect" means for an LED.

But if you've been working on this for 2 weeks you must have written some code. Please post your best try and say what it does and exactly what you want it to do differently.

Steve

Here is what I’ve got so far (This is only part of the full code I wrote I can post the full code if you want).

sketch_oct07a.ino (660 Bytes)

You haven't completely answered reply #1.

aarg:
You haven't completely answered reply #1.

Here is what I want to happen in order:

The program chooses a random led out of the 4 leds i have, the led fades on, the led fades off, the program chooses another random led, and it loops over and over again.

I hope this makes sense i'm sorry if my english isn't very good since it's not my native language.

This is my full code:

const byte Button = 13;

byte ButtonState;
byte lastState = LOW;
byte count = 0;
int L1 = 3;
int L2 = 5;
int L3 = 6;
int L4 = 14;
int L5 = 9;
int L6 = 10;
int L7 = 11;
int L8 = 15;
int LED[] ={
  2, 3, 4, 5, 9, 10, 11, 12
};
int pinCount = 8;
int brightness = 0;
int fadeAmount = 5;
int fadeAmountRandom = 7;
long randomNumber1;
long randomNumber2;

void setup() {
  
  Serial.begin(9600);

  pinMode(L1, OUTPUT);
  pinMode(L2, OUTPUT);
  pinMode(L3, OUTPUT);
  pinMode(L4, OUTPUT);
  pinMode(L5, OUTPUT);
  pinMode(L6, OUTPUT);
  pinMode(L7, OUTPUT);
  pinMode(L8, OUTPUT);
  pinMode(Button, INPUT);
  randomSeed(analogRead (A3));
}

void loop() {
  ButtonState = digitalRead(Button);

  if(ButtonState && ButtonState != lastState)  
  {
    if(count < 2) 
      count += 1;
    else
      count = 0;
  } 
  lastState = ButtonState;

  if    (count == 0)
{ 
  
  delay(50);
  digitalWrite (L1, LOW);
  digitalWrite (L2, LOW);
  digitalWrite (L3, LOW);
  digitalWrite (L4, LOW);
  digitalWrite (L5, LOW);
  digitalWrite (L6, LOW);
  digitalWrite (L7, LOW);
  digitalWrite (L8, LOW);
  digitalWrite (L1, HIGH);
  digitalWrite (L2, HIGH);
  digitalWrite (L3, HIGH);
  digitalWrite (L4, HIGH);
  digitalWrite (L5, HIGH);
  digitalWrite (L6, HIGH);
  digitalWrite (L7, HIGH);
  digitalWrite (L8, HIGH);

}
else if(count == 1)
{ 
  delay(50);
  digitalWrite (L1, LOW);
  digitalWrite (L2, LOW);
  digitalWrite (L3, LOW);
  digitalWrite (L4, LOW);
  digitalWrite (L5, LOW);
  digitalWrite (L6, LOW);
  digitalWrite (L7, LOW);
  digitalWrite (L8, LOW);
  analogWrite(L1, brightness);
  analogWrite(L2, brightness);
  analogWrite(L3, brightness);
  analogWrite(L4, brightness);
  analogWrite(L5, brightness);
  analogWrite(L6, brightness);
  analogWrite(L7, brightness);
  analogWrite(L8, brightness);
  brightness = brightness + fadeAmount;
  if (brightness <= 0 || brightness >= 250) {
    fadeAmount = -fadeAmount;
  }
  delay(30);
}
else if(count == 2)
{
  delay(50);
  digitalWrite(L1, LOW);
  digitalWrite(L2, LOW);
  digitalWrite(L3, LOW);
  digitalWrite(L4, LOW);
  digitalWrite(L5, LOW);
  digitalWrite(L6, LOW);
  digitalWrite(L7, LOW);
  digitalWrite(L8, LOW);
  randomNumber1 = random(1,4);
  randomNumber2 = random(1,4);
  analogWrite(randomNumber1, brightness);
  analogWrite(randomNumber2, brightness);
  for(int i = 0; i < 10; i++)
  {
     brightness = brightness + fadeAmountRandom;
      if (brightness <= 0 || brightness >= 250) {
        fadeAmountRandom = -fadeAmountRandom;
      }
     delay (30);
   }
 }
}

I’m trying to use a button to switch between different modes. I’m only looking to fix the 3rd mode, where count == 2.

  randomNumber1 = random(1,4);
  randomNumber2 = random(1,4);
  analogWrite(randomNumber1, brightness);
  analogWrite(randomNumber2, brightness);

What do you think these statements will do? random(1,4) generates numbers 1,2 or 3. analogWrite needs a pin number. How do you think the two randomNumber variables are related to LED pin numbers?

Steve

slipstick:

  randomNumber1 = random(1,4);

randomNumber2 = random(1,4);
  analogWrite(randomNumber1, brightness);
  analogWrite(randomNumber2, brightness);



What do you think these statements will do? random(1,4) generates numbers 1,2 or 3. analogWrite needs a pin number. How do you think the two randomNumber variables are related to LED pin numbers?

Steve
const byte Button = 13;

byte ButtonState;
byte lastState = LOW;
byte count = 0;
int L1 = 2;
int L2 = 3;
int L3 = 4;
int L4 = 5;
int L5 = 9;
int L6 = 10;
int L7 = 11;
int L8 = 12;
int LED[] ={
  2, 3, 4, 5, 9, 10, 11, 12
};
int pinCount = 8;
int brightness = 0;
int fadeAmount = 5;
int fadeAmountRandom = 7;
long randomNumber1;
long randomNumber2;

void setup() {
  
  Serial.begin(9600);

  pinMode(L1, OUTPUT);
  pinMode(L2, OUTPUT);
  pinMode(L3, OUTPUT);
  pinMode(L4, OUTPUT);
  pinMode(L5, OUTPUT);
  pinMode(L6, OUTPUT);
  pinMode(L7, OUTPUT);
  pinMode(L8, OUTPUT);
  pinMode(Button, INPUT);
  randomSeed(analogRead (A3));
}

void loop() {
  ButtonState = digitalRead(Button);

  if(ButtonState && ButtonState != lastState)  
  {
    if(count < 2) 
      count += 1;
    else
      count = 0;
  } 
  lastState = ButtonState;

  if    (count == 0)
{ 
  
  delay(50);
  digitalWrite (L1, LOW);
  digitalWrite (L2, LOW);
  digitalWrite (L3, LOW);
  digitalWrite (L4, LOW);
  digitalWrite (L5, LOW);
  digitalWrite (L6, LOW);
  digitalWrite (L7, LOW);
  digitalWrite (L8, LOW);
  digitalWrite (L1, HIGH);
  digitalWrite (L2, HIGH);
  digitalWrite (L3, HIGH);
  digitalWrite (L4, HIGH);
  digitalWrite (L5, HIGH);
  digitalWrite (L6, HIGH);
  digitalWrite (L7, HIGH);
  digitalWrite (L8, HIGH);

}
else if(count == 1)
{ 
  delay(50);
  digitalWrite (L1, LOW);
  digitalWrite (L2, LOW);
  digitalWrite (L3, LOW);
  digitalWrite (L4, LOW);
  digitalWrite (L5, LOW);
  digitalWrite (L6, LOW);
  digitalWrite (L7, LOW);
  digitalWrite (L8, LOW);
  analogWrite(L1, brightness);
  analogWrite(L2, brightness);
  analogWrite(L3, brightness);
  analogWrite(L4, brightness);
  analogWrite(L5, brightness);
  analogWrite(L6, brightness);
  analogWrite(L7, brightness);
  analogWrite(L8, brightness);
  brightness = brightness + fadeAmount;
  if (brightness <= 0 || brightness >= 250) {
    fadeAmount = -fadeAmount;
  }
  delay(30);
}
else if(count == 2)
{
  delay(50);
  digitalWrite(L1, LOW);
  digitalWrite(L2, LOW);
  digitalWrite(L3, LOW);
  digitalWrite(L4, LOW);
  digitalWrite(L5, LOW);
  digitalWrite(L6, LOW);
  digitalWrite(L7, LOW);
  digitalWrite(L8, LOW);
  randomNumber1 = random(1,4);
  randomNumber2 = random(9,12);
  analogWrite(randomNumber1, brightness);
  analogWrite(randomNumber2, brightness);
  for(int i = 0; i < 10; i++)
  {
     brightness = brightness + fadeAmountRandom;
      if (brightness <= 0 || brightness >= 250) {
        fadeAmountRandom = -fadeAmountRandom;
      }
     delay (30);
   }
 }
}

This was my original code, with leds plugged in to pins 1-4 and 9-12. I realized that to make the fade on/fade off effect i needed my leds plugged to a pwm or analog pin. So I moved my leds to pwm pins 3,5,6,9,10,11 and analog pins 0 and 1, so my issue now is that I wanna generate a random number that will turn on these leds.

Easiest is to make use of an array for the pin numbers. Then you can randomly pick one. The below demonstrates the basics.

// array with PWM pins
const byte pins[] = {10, 11, 5, 6};

void setup()
{
  Serial.begin(115200);
}

void loop()
{
  int rnd = random(0, sizeof(pins) / sizeof(pins[0]));
  Serial.print("Random pin "); Serial.println(pins[rnd]);
  delay(500);
}

I figured something out but thanks for all of your help.