Randomly LEDs fading problem

Hello everybody!
I'm new here, about this forum and Arduino environment.

I was trying to write a sketch that randomly fades one of the 4 LED connected to the board.
Obviously, something doesn't work properly: in fact, I cannot even see one LED actually fading.

Here's the code:

// Defining MACROs for pins.
#define PIN1 9
#define PIN2 10
#define PIN3 11
#define PIN4 13

// Procedures for fading/blinking leds. //
void blinking_light1();
void blinking_light2();
void blinking_light3();
void blinking_light4();
// ------------------------------------ //

// Global variables. --------------------------------------------------- //
int rand_generator;  // Random number for selecting random leds. 
int done1 = 0;       // Control variables for procedures blinded to PIN1
int done2 = 0;       // Same for PIN2...
int done3 = 0;       // ...and PIN3...
int done4 = 0;       // ...and even PIN4!
// --------------------------------------------------------------------- //

void setup()
{
  // Setting pins in OUTPUT.
  pinMode(PIN1, OUTPUT);
  pinMode(PIN2, OUTPUT);
  pinMode(PIN3, OUTPUT);
  pinMode(PIN4, OUTPUT);
  
  // Seed for random number.
  randomSeed(analogRead(0));
  
  // Starting Serial functionalities.
  Serial.begin(9600);
}

void loop()
{ 
   // Generating the random number selector. 
   rand_generator = (int) random(1, 5); // Casting to "int".
   Serial.print(rand_generator);
   Serial.print("\n");
   
   // Invocating the right procedures linked to "rand_generator".
   switch (rand_generator)
   {
     case 1:
       if (!done1) blinking_light1();
       
     case 2:
       if (!done2) blinking_light2();

     case 3:
       if (!done3) blinking_light3();

     case 4:
       if (!done4) blinking_light4();      
   }
   
   delay(30);
}

void blinking_light1()
{
  int brightness = 0;  // Setting the starting brightness for pin.
  int fadeAmount = 5;  // Amount of incrementing brightness.
  
  // ------------------------------------------------------------ //
  
  done1 = 1;  // Setting the pin on "busy" mode.
  
  for(; brightness <= 255; brightness += fadeAmount)
  {
    analogWrite(PIN1, brightness);
    delay(30);
  }
  
  for(; brightness >= 0; brightness -= fadeAmount)
  {
    analogWrite(PIN1, brightness);
    delay(30);
  }
    
  done1 = 0;
}

void blinking_light2()
{
  int brightness = 0;
  int fadeAmount = 5;
  
  // ------------------------------------------------------------ //
  
  done2 = 1;
  
  for(; brightness <= 255; brightness += fadeAmount)
  {
    analogWrite(PIN2, brightness);
    delay(30);
  }
  
  for(; brightness >= 0; brightness -= fadeAmount)
  {
    analogWrite(PIN2, brightness);
    delay(30);
  }
    
  done2 = 0;
}

void blinking_light3()
{
  int brightness = 0;
  int fadeAmount = 5;
  
  // ------------------------------------------------------------ //
  
  done3 = 1;
  
  for(; brightness <= 255; brightness += fadeAmount)
  {
    analogWrite(PIN3, brightness);
    delay(30);
  }
  
  for(; brightness >= 0; brightness -= fadeAmount)
  {
    analogWrite(PIN3, brightness);
    delay(30);
  }
    
  done3 = 0;
}

void blinking_light4()
{
  int brightness = 0;
  int fadeAmount = 5;
  
  // ------------------------------------------------------------ //
  
  done4 = 1;
  
  for(; brightness <= 255; brightness += fadeAmount)
  {
    analogWrite(PIN4, brightness);
    delay(30);
  }
  
  for(; brightness >= 0; brightness -= fadeAmount)
  {
    analogWrite(PIN4, brightness);
    delay(30);
  }
    
  done4 = 0;
}

Thanks to everyone would point me my n00bie errors :smiley:

I would suggest you simplify your code massively by:

  1. Learn about arrays
  2. Learn about passing parameters to functions.

You only need 1 blinking_light() function which can act on a member of an array, the index of which is passed to it at runtime.

If your using an UNO for this then pin 13 is not a PWM pin and cannot be dimmed. It will just turn on/off only.

// Defining MACROs for pins.
#define PIN1 9
#define PIN2 10
#define PIN3 11
#define PIN4 13

Try printing the value of brightness in your for loops. Are the values what you would expect ?

  for(; brightness <= 255; brightness += fadeAmount)This form of for loop always looks ugly to me. Why not use the form where the loop variable is declared in the for loop ?

Your case statement is no good, if your random number is 1, it will execute all cases, you need to put a break; after each case.

majenko:
I would suggest you simplify your code massively by:

  1. Learn about arrays
  2. Learn about passing parameters to functions.

You only need 1 blinking_light() function which can act on a member of an array, the index of which is passed to it at runtime.

Trust me, as graduating in CS, i know quite a bit about them... I just don't even think about this possibility.
It's my first sketch for Arduino, sorry. :slight_smile:

Riva:
If your using an UNO for this then pin 13 is not a PWM pin and cannot be dimmed. It will just turn on/off only.

// Defining MACROs for pins.

#define PIN1 9
#define PIN2 10
#define PIN3 11
#define PIN4 13

I have a Leonardo board. On pin 13 works fine...

michinyon:
Your case statement is no good, if your random number is 1, it will execute all cases, you need to put a break; after each case.

I tried the classic "switch ... case" syntax on my first attempts, but the compiler returns some kind of errors related to the break (I don't really know, I'll try to correct now and re-upload the sketch).

Anyway, thanks to everybody for the help! :slight_smile:

Ok, now works :wink:
Thanks again folks!

// Defining MACROs for pins.
#define pinLed1 9
#define pinLed2 10
#define pinLed3 11
#define pinLed 413

// Procedures for fading/blinking leds. ------------------------------ //
void blinking_light(int pos, int LED);  // Needs position of the array.
// ------------------------------------------------------------------- //

// Global variables. --------------------------------------------------- //
int rand_generator;  // Random number for selecting random leds. 
int control_Stack[4] = {0, 0, 0, 0}; // Stack to control the procedures.
// --------------------------------------------------------------------- //

void setup()
{
  // Setting pins in OUTPUT.
  pinMode(pinLed1, OUTPUT);
  pinMode(pinLed2, OUTPUT);
  pinMode(pinLed3, OUTPUT);
  pinMode(pinLed4, OUTPUT);
  
  // Seed for random number.
  randomSeed(analogRead(0));
  
  // Starting Serial functionalities.
  Serial.begin(9600);
  Serial.println("\tReady.\n");
}

void loop()
{ 
   // Generating the random number selector. 
   rand_generator = (int) random(1, 5); // Casting to "int".
   Serial.print(rand_generator, DEC);
   Serial.print("\n");
   
   // Invocating the right procedures linked to "rand_generator".
   switch (rand_generator)
   {
     case 1:
       if (!control_Stack[0]) blinking_light(0, pinLed1);
       break;
       
     case 2:
       if (!control_Stack[1]) blinking_light(1, pinLed2);
       break;

     case 3:
       if (!control_Stack[2]) blinking_light(2, pinLed3);
       break;

     case 4:
       if (!control_Stack[3]) blinking_light(3, pinLed4);
       break;      
   }
   
   delay(30);
}

void blinking_light(int pos, int LED)
{
  int brightness;  // Setting the starting brightness for pin.
  int fadeAmount;  // Amount of incrementing brightness.
  
  // ------------------------------------------------------------ //
  
  control_Stack[pos] = 1;  // Setting the pin on "busy" mode.
  
  for(brightness = 0, fadeAmount = 5; brightness <= 255; brightness += fadeAmount)
  {
    analogWrite(LED, brightness);
    Serial.print("LED: ");
    Serial.print(LED, DEC);
    Serial.print(", brightness: ");
    Serial.print(brightness, DEC);
    Serial.print("\n");
    
    delay(30);
  }
  
  for(fadeAmount = 5; brightness >= 0; brightness -= fadeAmount)
  {
    analogWrite(LED, brightness);
    Serial.print("LED: ");
    Serial.print(LED, DEC);
    Serial.print(", brightness: ");
    Serial.print(brightness, DEC);
    Serial.print("\n");
    
    delay(30);
  }
    
  control_Stack[pos] = 0;  // Setting on "free" the pin.
}

ar3s3ru:
Trust me, as graduating in CS, i know quite a bit about them... I just don't even think about this possibility.
It's my first sketch for Arduino, sorry. :slight_smile:

Huh? Did you manage to graduate in CS without learning how to write code?