const int timeUp = 380;
const int timeOn = 100;
const int timeDown = 700;
const int profileLength = timeUp+timeOn+timeDown;
const int fastFly = 5000;
const int interval = 1000;
const int fuzziness = 700;

const float upSlope = 255.0/float(timeUp);
const float downSlope = (-255.0)/float(timeDown);

const int pwm[] = {0,1};
const int col[] = {3,4};

int timeOff[2][2] = {{5000, 6500}, {7000, 8000}};
int totalLength[2][2];
int index;

void setup(){
  //Serial.begin(9600);
  randomSeed(analogRead(1));
  for (int r=0; r<(sizeof(pwm)/sizeof(int)); r++){
    pinMode(pwm[r], OUTPUT);
  }
  for (int r=0; r<(sizeof(pwm)/sizeof(int)); r++){
    for (int c=0; c<(sizeof(col)/sizeof(int)); c++){
      pinMode(col[c], OUTPUT);
      digitalWrite(col[c], HIGH);
      int center = fastFly + (interval*((2*r)+c));
      timeOff[r][c] = random(center-fuzziness, center+fuzziness);
      totalLength[r][c] = profileLength + timeOff[r][c];
    } 
  }
}

void loop(){
  for (int c=0; c<(sizeof(col)/sizeof(int)); c++){
    for (int r=0; r<(sizeof(pwm)/sizeof(int)); r++){
      index = millis() % totalLength[r][c];     
      ///fade in the led/// 
      if (index > 0 && index <= timeUp){
        analogWrite(pwm[r], upSlope*index);
      }
      ///keep led on///
      else if (index > timeUp && index <= (timeUp + timeOn)){
        analogWrite(pwm[r], 255);
      }
      ///fade out the led///
      else if (index > (timeUp + timeOn) && index <= (profileLength)){
        analogWrite(pwm[r], downSlope*(index-profileLength));
      }
      ///keep led off///
      else { //if (index > (timeUp + timeOn + timeDown) && index <= int(totalLength[r][c]))
        analogWrite(pwm[r], 0); 
      }
    }
    digitalWrite(col[c], LOW);
    delay(10);
    digitalWrite(col[c], HIGH);
  }  
}

/*
int getUp(int index){
  return upSlope*index;
  //return 100;
}

int getDown(int index){
  return downSlope*(index-profileLength);
  //return 100;
}
*/