// DoorSign_RGB_CrossFade 
// Copyright (C)2015 Rinky-Dink Electronics, Henning Karlsen. All right reserved
// web: http://www.RinkyDinkElectronics.com/
//
// This is a simple sketch to crossfade some RGB-LEDs in my doorsign.
//
// The circuit:
//  * RGB-LED attached from digital pin 9,10 and 11 to ground.
// 

static const int FlashDelay = 100;
static const int LoopTime = 5; // minutes

int RledPin = 9;    // Red LED connected to digital pin 9
int GledPin = 10;    // Green LED connected to digital pin 10
int BledPin = 11;    // Blue LED connected to digital pin 11
double Rvalue = 0;
double Gvalue = 0;
double Bvalue = 0;
double Rold = 0;
double Gold = 0;
double Bold = 0;
int LoopSize = 100;

void setup()
{ 
  randomSeed(analogRead(0));

  analogWrite(RledPin, 255);
  analogWrite(GledPin, 0);
  analogWrite(BledPin, 0);
  delay(FlashDelay);
  analogWrite(RledPin, 0);
  analogWrite(GledPin, 255);
  analogWrite(BledPin, 0);
  delay(FlashDelay);
  analogWrite(RledPin, 0);
  analogWrite(GledPin, 0);
  analogWrite(BledPin, 255);
  delay(FlashDelay);
  analogWrite(RledPin, 255);
  analogWrite(GledPin, 255);
  analogWrite(BledPin, 0);
  delay(FlashDelay);
  analogWrite(RledPin, 255);
  analogWrite(GledPin, 0);
  analogWrite(BledPin, 255);
  delay(FlashDelay);
  analogWrite(RledPin, 0);
  analogWrite(GledPin, 255);
  analogWrite(BledPin, 255);
  delay(FlashDelay);
  analogWrite(RledPin, 255);
  analogWrite(GledPin, 255);
  analogWrite(BledPin, 255);
  delay(FlashDelay);

  while (Rvalue+Gvalue+Bvalue<255)
  {
    Rvalue = random(255);
    Gvalue = random(255);
    Bvalue = random(255);
  }
} 

void loop()
{ 
  Rold = Rvalue;
  Gold = Gvalue;
  Bold = Bvalue;
  Rvalue = 0;
  Gvalue = 0;
  Bvalue = 0;
  while (Rvalue+Gvalue+Bvalue<255)
  {
    Rvalue = random(255);
    Gvalue = random(255);
    Bvalue = random(255);
  }

  for(double fadeCounter = 0 ; fadeCounter <= LoopSize; fadeCounter +=1) { 
    analogWrite(RledPin, Rold + ((Rvalue-Rold)*(fadeCounter/LoopSize)));         
    analogWrite(GledPin, Gold + ((Gvalue-Gold)*(fadeCounter/LoopSize)));         
    analogWrite(BledPin, Bold + ((Bvalue-Bold)*(fadeCounter/LoopSize)));         
    
    delay(((LoopTime*60)/LoopSize)*1000);                            
  } 
}