#define probability 5 // Sets the density of the lit up LEDs

int pwmStates[] = {0,0,0,0,0,0,0,0,0,0,0,0};  // Stores the current state of the pins
int pwmTarget[] = {0,0,0,0,0,0,0,0,0,0,0,0};  // Stores the target state of the pins
int rnd = 0;                                  // Stores the random numbers for choosing the pins
int probable = 0;                             // Stores the random number for the pwm values

void setup()
{
  for(int i = 0; i < 12; i++){  // Set all nececary pins to output
    pinMode(i, OUTPUT);
  }
}

void loop()
{
  rnd = random(0, 12);
  if(rnd != 4 && rnd != 7 && rnd != 8){   // Chose a valid PWM pin
    
    probable = random(1, 10);
    if(probable <= probability){    // Decide if the pin needs to fade on or off
      pwmTarget[rnd] = 255;
    }else{
      pwmTarget[rnd] = 0;
    }
    
    for(int i = 0; i < 50; i++){    // Fade the LEDs towards the target value over time
      for(int n = 0; n < 12; n++){
        if(pwmTarget[n] == 255){
          pwmStates[n]++;
        }else{
          pwmStates[n]--;
        }
        
        if(pwmStates[n] >= 255){  // If the pwm value is out of range, reset value
          pwmStates[n] = 255;
        }
        if(pwmStates[n] <= 0){
          pwmStates[n] = 0;
        }
        analogWrite(n, pwmStates[n]); // Write values to the output
      }
      delay(1);
    }
    
  }
}