Fading 3 LEDs to different values simultaneously

Hi,

I am trying to make a little lightbox which will include 3 LEDs. When i press a button i want each LED to fade to a new value.

I have it working fine using one LED but i am not sure how to make 3 LEDs fade to different values simultaneously.

Here is the code to fade just the green LED to a different value with every press of the button. Ignore the Serial.println stuff it was just there when debugging.

Any help would be greatly appreciated.

Thanks, Patrick

int greenpin = 11;
int bluepin = 10;
int redpin = 9;

int buttonpin = 2;
int val;

int rgreen; //Latest random value for each colour
int rblue;
int rred;

int buttonstate = 0;

int brightnessgreen = 0; //
int brightnessblue = 0;
int brightnessred = 0;
int destinationgreen = 0;
int destinationblue = 0;
int destinationred = 0;

int fadeamount = 1;

void setup(){
Serial.begin(9600);
pinMode(greenpin, OUTPUT);
pinMode(bluepin, OUTPUT);
pinMode(redpin, OUTPUT);
pinMode(buttonpin, INPUT);
randomSeed(analogRead(0));
}

void loop(){

val = digitalRead(buttonpin); //Check whether the button has been pressed
if (val != buttonstate){
if (val== LOW){

rgreen = random(30);
rblue = random(30);
rred = random(30);
Serial.println(rgreen);
Serial.println(rblue);
Serial.println(rred);
if(rgreen > brightnessgreen){ //If the new randomnumber is LARGER than the previous one, proceed with the following for statement
for (int brightnessgreen = destinationgreen; brightnessgreen <= rgreen; brightnessgreen++){
analogWrite(greenpin, brightnessgreen);
delay(100);
Serial.println(brightnessgreen);

}
brightnessgreen = rgreen;
destinationgreen = rgreen;
Serial.println(“it was a bigger number!”);
Serial.print("the new brightness variable is " );
Serial.println(brightnessgreen);

}else{
if(rgreen < brightnessgreen){ //If the new randomnumber is SMALLER than the previous one, proceed with the following for statement
for (int brightnessgreen = destinationgreen; brightnessgreen >= rgreen; brightnessgreen–){
analogWrite(greenpin, brightnessgreen);
delay(100);
Serial.println(brightnessgreen);}
brightnessgreen = rgreen;
destinationgreen = rgreen;
Serial.println(“it was a smaller number!”);
Serial.print("the new brightness variable is " );
Serial.println(brightnessgreen);}}

}
buttonstate = val;}} // reset the buttonstate for the ’ if (val != buttonstate){’ line of code

Something like this?

To be honest I haven’t tested the code, but I works in my brain!!!

Give it a shot who knows?

rgreen = random(30);
rblue = random(30);
rred = random(30);

for(int max_val = 0; max_val <= 30 ; max_val++){
	if(rgreen > brightnessgreen){
		brightnessgreen++;
		analogWrite(greenpin, brightnessgreen);
	}
	if(rgreen < brightnessgreen){
		brightnessgreen--;
		analogWrite(greenpin, brightnessgreen);
	}
	if(rblue > brightnessblue){
		brightnessblue++;
		analogWrite(bluepin, brightnessblue);
	}
	if(rblue < brightnessblue){
		brightnessblue--;
		analogWrite(bluepin, brightnessblue);
	}
	if(rred > brightnessred){
		brightnessred++;
		analogWrite(redpin, brightnessred);
	}
	if(rred < brightnessred){
		brightnessred--;
		analogWrite(redpin, brightnessred);
	}
	delay(100);
}
brightnessgreen = rgreen;
brightnessblue = rblue;
brightnessred = rred;

Hi,

This is great!

Thanks, Patrick.

Code can be optimized a bit,

for(int max_val = 0; max_val <= 30 ; max_val++)
{
	if (rgreen > brightnessgreen) brightnessgreen++;
	else if (rgreen < brightnessgreen) brightnessgreen--;
	analogWrite(greenpin, brightnessgreen);

 same for blue and red

@keenthell
Please modify that post. Select the code and hit the # icon. That puts it in a nice box.

The problem with the code posted is that it will not fade smoothly between colours as you increment each component until it reaches the required value. You need to work out the distance between each colour component and only increment the biggest. The others get an added value less than one (use floating point), that way all the colours fade up together. For a full discussing see:-

Edit:- it was this one
http://arduino.cc/forum/index.php/topic,67766.0.html

The basic function is:-

// set up these global variables before the setup()
int currentRed = 0, currentGreen = 0, currentBlue = 0;

void fadeTo(int R, int G, int B, int fadeSteps) {  // fade from the current colour to the one passed to the function
  float deltaR, deltaG, deltaB;
  float tempR, tempG, tempB;
// set up the temp values to the current LED state
  tempR = currentRed;
  tempG = currentGreen;
  tempB = currentBlue;

// calculate the distance between each colour as it is now, and the target colour
  deltaR = ( float(R - currentRed)) / fadeSteps;
  deltaG = ( float(G - currentGreen)) / fadeSteps;
  deltaB = ( float(B - currentBlue)) / fadeSteps;

  // fade to new colour
  for (int i = 0; i < fadeSteps; i++) { 
 // calculate new intermediate colour
   tempR += deltaR;
   tempG += deltaG;
   tempB += deltaB;

  // set these LEDs to the new intermediate colour
    analogWrite(REDPIN, int(tempR));
    analogWrite(GREENPIN, int(tempG));
    analogWrite(BLUEPIN, int(tempB));

    delay(FADESPEED);
  } // end of for loop

// keep the global colour up to date
  currentRed = tempR;
  currentGreen = tempG;
  currentBlue = tempB;

}

Change the way to set the colours (here using the analogWrite() to suite your hardware.