RGB Mixed colour to fade Issue

Hello,

The following code works but in a strange way. Button1 Pushed ---> RBG LED Lights up with set value (R=155,G=100,B=50) ----> Delay ----> RGB strip truns to white ??? -------> then fades out.

Why does the white light show up? Why doesnt theRGB colour fade out to zero and not take the route which it currently does.

Ideally i would like it to light up to a certain colour and then fade from that colour to off.

Please help :)

Its very strange.

const int ledPinR = 9;
const int ledPinG= 11;
const int ledPinB = 10;

const int buttonPin1 = 3;
const int buttonPin2 = 4;

int buttonState1 = 0;
int buttonState2 = 0;

int redPwr = 0;
int greenPwr = 0;
int bluePwr = 0;
int    faderR;
int faderG;
int faderB;

void setup()
{
  pinMode(ledPinR, OUTPUT);
  pinMode(ledPinG, OUTPUT);
  pinMode(ledPinB, OUTPUT);
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);

}

void loop()
{
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2 );
  if (buttonState1 == LOW){
    //redPwr = 246;
    //greenPwr = 235;
    //bluePwr = 0;
    //analogWrite(ledPinR, redPwr);
    //analogWrite(ledPinG, greenPwr);
    //analogWrite(ledPinB, bluePwr);
    //delay(1000);
    fade(redPwr,greenPwr,bluePwr,50);
  } 

  if (buttonState2 == LOW) {
    //redPwr = 246;
    //greenPwr = 235;
    //bluePwr = 0;
    //analogWrite(ledPinR, redPwr);
    //analogWrite(ledPinG, greenPwr);
    //analogWrite(ledPinB, bluePwr);
    //delay(1000);
    fade2(redPwr,greenPwr,bluePwr,50);
  }  



  else {
    analogWrite(ledPinR, LOW);
    analogWrite(ledPinG, LOW);
    analogWrite(ledPinB, LOW);
  }
}

void fade(int redFrom, int grnFrom, int bluFrom, int delayTime)
{
  /*for (int r=redFrom, g=grnFrom, b=bluFrom; r>=0; g>=0; b>=0; r-=5, g-=5, b-=5)
   {
   analogWrite(ledPinR, r);
   analogWrite(ledPinG, g);
   analogWrite(ledPinB, b);
   delay(delayTime);
   } */
  faderR = 246;
  faderG = 235;
  faderB = 0;
  analogWrite(ledPinR, faderR);
  analogWrite(ledPinG, faderG);
  analogWrite(ledPinB, faderB);

  delay(1000);
  for (int fader=255; fader>=0; fader-=5)
  {
    faderR -= 4;

    faderG -= 5;
    faderB -= 2;

    if (faderR > 0)
    {
      faderR = 0;
    }
    if (faderG > 0)
    {
      faderG = 0;
    }
    if (faderB > 0)
    {
      faderB = 0;
    }

    analogWrite(ledPinR, faderR);
    analogWrite(ledPinG, faderG);
    analogWrite(ledPinB, faderB);
    delay(delayTime);
  }
}
void fade2(int redFrom, int grnFrom, int bluFrom, int delayTime)
{
  /*for (int r=redFrom, g=grnFrom, b=bluFrom; r>=0; g>=0; b>=0; r-=5, g-=5, b-=5)
   {
   analogWrite(ledPinR, r);
   analogWrite(ledPinG, g);
   analogWrite(ledPinB, b);
   delay(delayTime);
   } */
  faderR = 100;
  faderG = 0;
  faderB = 100;
  analogWrite(ledPinR, faderR);
  analogWrite(ledPinG, faderG);
  analogWrite(ledPinB, faderB);

  delay(1000);
  for (int fader=255; fader>=0; fader-=5)
  {
    faderR -= 4;

    faderG -= 5;
    faderB -= 2;

    if (faderR > 0)
    {
      faderR = 0;
    }
    if (faderG > 0)
    {
      faderG = 0;
    }
    if (faderB > 0)
    {
      faderB = 0;
    }

    analogWrite(ledPinR, faderR);
    analogWrite(ledPinG, faderG);
    analogWrite(ledPinB, faderB);
    delay(delayTime);
  }
}
void fade3(int redFrom, int grnFrom, int bluFrom, int delayTime)
{
  /*for (int r=redFrom, g=grnFrom, b=bluFrom; r>=0; g>=0; b>=0; r-=5, g-=5, b-=5)
   {
   analogWrite(ledPinR, r);
   analogWrite(ledPinG, g);
   analogWrite(ledPinB, b);
   delay(delayTime);
   } */
  faderR = 0;
  faderG = 150;
  faderB = 200;
  analogWrite(ledPinR, faderR);
  analogWrite(ledPinG, faderG);
  analogWrite(ledPinB, faderB);

  delay(3000);
  for (int fader=255; fader>=0; fader-=5)
  {
    faderR -= 4;

    faderG -= 5;
    faderB -= 2;

    if (faderR > 0)
    {
      faderR = 0;
    }
    if (faderG > 0)
    {
      faderG = 0;
    }
    if (faderB > 0)
    {
      faderB = 0;
    }

    analogWrite(ledPinR, faderR);
    analogWrite(ledPinG, faderG);
    analogWrite(ledPinB, faderB);
    delay(delayTime);
  }

}

Also someone emailed me this code but it doesnt seem to work!

const int ledPinR = 9;
const int ledPinG= 11;
const int ledPinB = 10;

const int buttonPin1 = 3;
const int buttonPin2 = 4;

int buttonState1 = 0;
int buttonState2 = 0;

int redPwr = 0;
int greenPwr = 0;
int bluePwr = 0;
int    faderR;
int faderG;
int faderB;

void setup()
{
  pinMode(ledPinR, OUTPUT);
  pinMode(ledPinG, OUTPUT);
  pinMode(ledPinB, OUTPUT);
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);

}

void loop()
{
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2 );
  if (buttonState1 == LOW){
    //redPwr = 246;
    //greenPwr = 235;
    //bluePwr = 0;
    //analogWrite(ledPinR, redPwr);
    //analogWrite(ledPinG, greenPwr);
    //analogWrite(ledPinB, bluePwr);
    //delay(1000);
    fade(redPwr,greenPwr,bluePwr,50);
  } 

  if (buttonState2 == LOW) {
    //redPwr = 246;
    //greenPwr = 235;
    //bluePwr = 0;
    //analogWrite(ledPinR, redPwr);
    //analogWrite(ledPinG, greenPwr);
    //analogWrite(ledPinB, bluePwr);
    //delay(1000);
    fade2(redPwr,greenPwr,bluePwr,50);
  }  



  else {
    analogWrite(ledPinR, LOW);
    analogWrite(ledPinG, LOW);
    analogWrite(ledPinB, LOW);
  }
}

void fade(int redFrom, int grnFrom, int bluFrom, int delayTime)
{
  /*for (int r=redFrom, g=grnFrom, b=bluFrom; r>=0; g>=0; b>=0; r-=5, g-=5, b-=5)
   {
   analogWrite(ledPinR, r);
   analogWrite(ledPinG, g);
   analogWrite(ledPinB, b);
   delay(delayTime);
   } */
  faderR = 246;
  faderG = 235;
  faderB = 0;
  analogWrite(ledPinR, faderR);
  analogWrite(ledPinG, faderG);
  analogWrite(ledPinB, faderB);

  delay(1000);
  for (int fader=255; fader>=0; fader-=5)
  {
    faderR -= 4;

    faderG -= 5;
    faderB -= 2;

    if (faderR > 0)
    {
      faderR = 0;
    }
    if (faderG > 0)
    {
      faderG = 0;
    }
    if (faderB > 0)
    {
      faderB = 0;
    }

    analogWrite(ledPinR, faderR);
    analogWrite(ledPinG, faderG);
    analogWrite(ledPinB, faderB);
    delay(delayTime);
  }
}
void fade2(int redFrom, int grnFrom, int bluFrom, int delayTime)
{
  /*for (int r=redFrom, g=grnFrom, b=bluFrom; r>=0; g>=0; b>=0; r-=5, g-=5, b-=5)
   {
   analogWrite(ledPinR, r);
   analogWrite(ledPinG, g);
   analogWrite(ledPinB, b);
   delay(delayTime);
   } */
  faderR = 100;
  faderG = 0;
  faderB = 100;
  analogWrite(ledPinR, faderR);
  analogWrite(ledPinG, faderG);
  analogWrite(ledPinB, faderB);

  delay(1000);
  for (int fader=255; fader>=0; fader-=5)
  {
    faderR -= 4;

    faderG -= 5;
    faderB -= 2;

    if (faderR > 0)
    {
      faderR = 0;
    }
    if (faderG > 0)
    {
      faderG = 0;
    }
    if (faderB > 0)
    {
      faderB = 0;
    }

    analogWrite(ledPinR, faderR);
    analogWrite(ledPinG, faderG);
    analogWrite(ledPinB, faderB);
    delay(delayTime);
  }
}
void fade3(int redFrom, int grnFrom, int bluFrom, int delayTime)
{
  /*for (int r=redFrom, g=grnFrom, b=bluFrom; r>=0; g>=0; b>=0; r-=5, g-=5, b-=5)
   {
   analogWrite(ledPinR, r);
   analogWrite(ledPinG, g);
   analogWrite(ledPinB, b);
   delay(delayTime);
   } */
  faderR = 0;
  faderG = 150;
  faderB = 200;
  analogWrite(ledPinR, faderR);
  analogWrite(ledPinG, faderG);
  analogWrite(ledPinB, faderB);

  delay(3000);
  for (int fader=255; fader>=0; fader-=5)
  {
    faderR -= 4;

    faderG -= 5;
    faderB -= 2;

    if (faderR > 0)
    {
      faderR = 0;
    }
    if (faderG > 0)
    {
      faderG = 0;
    }
    if (faderB > 0)
    {
      faderB = 0;
    }

    analogWrite(ledPinR, faderR);
    analogWrite(ledPinG, faderG);
    analogWrite(ledPinB, faderB);
    delay(delayTime);
  }

}

First problem is the common anode. Because it uses a common anode 0 if full on and 255 if off. So you need to reverse way it fades.

Next if you want the buttons to read as low and not use a pull up resistor you need to designate them as digitalWrite in the void setup.

void setup()
{
  pinMode(ledPinR, OUTPUT);
  pinMode(ledPinG, OUTPUT);
  pinMode(ledPinB, OUTPUT);
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  digitalWrite(buttonPin1, HIGH);
  digitalWrite(buttonPin2, HIGH);
}

Once again you need to keep this in a single thread. Stop creating a new thread for each individual problem your facing with the same code.

Add this statement to setup:

Serial.begin(9600);

Add this to fade, in the loop that is supposed to fade the led:

Serial.print("Setting color values: ");
Serial.print(faderR);
Serial.print(" ");
Serial.print(faderG);
Serial.print(" ");
Serial.println(faderB);

Upload the sketch. Open the Serial Monitor window (rightmost button on IDE). Set the baud rate to 9600. Then press the button. Copy and paste the output back here, please.

Thanks for the responses.

I will be able to post this info at 19:30 (GMT)

I have somethings to do first. (dinner!!!!) I will try out the code and run the serial and then post the info at that time.

Cheers,

Victor

Ok i sat down and worked out the logic for you. all you have to do is change the different fader variables to control how fast the color fades out. Or adjust it up so that the color can fully fade out.

const int ledPinR = 9;
const int ledPinG= 11;
const int ledPinB = 10;

const int buttonPin1 = 3;
const int buttonPin2 = 4;

int buttonState1 = 0;
int buttonState2 = 0;

int redPwr = 255;
int greenPwr = 255;
int bluePwr = 255;
int faderR;
int faderG;
int faderB;

void setup()
{
  pinMode(ledPinR, OUTPUT);
  pinMode(ledPinG, OUTPUT);
  pinMode(ledPinB, OUTPUT);
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  digitalWrite(buttonPin1, HIGH);
  digitalWrite(buttonPin2, HIGH);
  analogWrite(ledPinR, redPwr);
  analogWrite(ledPinG, greenPwr);
  analogWrite(ledPinB, bluePwr);
}

void loop()
{
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2 );
  if (buttonState1 == LOW){
    fade(redPwr,greenPwr,bluePwr,50);
  }

  if (buttonState2 == LOW) {
    fade2(redPwr,greenPwr,bluePwr,50);
  }  

}


void fade(int redFrom, int grnFrom, int bluFrom, int delayTime)
{
  faderR = 246;
  faderG = 235;
  faderB = 0;
  analogWrite(ledPinR, faderR);
  analogWrite(ledPinG, faderG);
  analogWrite(ledPinB, faderB);

  delay(1000);
  for (int fader=0; fader<=255; fader+=5)
  {
    faderR += 5;
    faderG += 5;
    faderB += 5;

    if (faderR > 255)
    {
      faderR = 255;
    }
    if (faderG > 255)
    {
      faderG = 255;
    }
    if (faderB > 255)
    {
      faderB = 255;
    }

    analogWrite(ledPinR, faderR);
    analogWrite(ledPinG, faderG);
    analogWrite(ledPinB, faderB);
    delay(delayTime);
  }
}
void fade2(int redFrom, int grnFrom, int bluFrom, int delayTime)
{
  faderR = 100;
  faderG = 0;
  faderB = 100;
  analogWrite(ledPinR, faderR);
  analogWrite(ledPinG, faderG);
  analogWrite(ledPinB, faderB);

  delay(1000);
  for (int fader=0; fader<=255; fader+=5)
  {
    faderR += 5;

    faderG += 5;
    faderB += 6;

    if (faderR > 255)
    {
      faderR = 255;
    }
    if (faderG > 255)
    {
      faderG = 255;
    }
    if (faderB > 255)
    {
      faderB = 255;
    }

    analogWrite(ledPinR, faderR);
    analogWrite(ledPinG, faderG);
    analogWrite(ledPinB, faderB);
    delay(delayTime);
  }
}
void fade3(int redFrom, int grnFrom, int bluFrom, int delayTime)
{
  faderR = 0;
  faderG = 150;
  faderB = 200;
  analogWrite(ledPinR, faderR);
  analogWrite(ledPinG, faderG);
  analogWrite(ledPinB, faderB);

  delay(3000);
  for (int fader=0; fader<=255; fader+=5)
  {
    faderR += 5;

    faderG += 5;
    faderB += 5;

    if (faderR > 255)
    {
      faderR = 255;
    }
    if (faderG > 255)
    {
      faderG = 255;
    }
    if (faderB > 255)
    {
      faderB = 255;
    }

    analogWrite(ledPinR, faderR);
    analogWrite(ledPinG, faderG);
    analogWrite(ledPinB, faderB);
    delay(delayTime);
  }

}

yay! Thanks digimike! :slight_smile:

That solves all the problems.

The light stays on all the time but the colour comes through.
I actually prefer it better this way. It will look better in what i will
use it for. :slight_smile:

Now that this is working, I have been looking around for ways to create a random colour.

But how i would like to do it is like this:

button 1 pressed —> makes RGB LED Light up with random colour range in a certain Spectrum.

So like if i pressed button1 i would get some shade of blue (teal, turquoise, cyan, skyblue, you get the point)
But it would never be the same Blue every time button 1 is pushed until it runs out of possiblities.

I have found a code for randomly cyclying though colours but done know how to apply it to my code.

and button2 would give me some shade of Orange.

I have found a code for randomly cyclying though colours but done know how to apply it to my code.

here is the code fro random cycling.

if (lightMode == 3) {  // randomsize colorNew and step colorPwr to it
if (redPwr > redNew) {
redPwr--;
}
if (redPwr < redNew) {
redPwr++;
}
if (greenPwr > greenNew) {
greenPwr--;
}
if (greenPwr < greenNew) {
greenPwr++;
}
if (bluePwr > blueNew) {
bluePwr--;
}
if (bluePwr < blueNew) {
bluePwr++;
}

// If all Pwr match New get new colors

if (redPwr == redNew) {
if (greenPwr == greenNew) {
if (bluePwr == blueNew) {
redNew = random(254);
greenNew = random(254);
blueNew = random(254);
}
}
}

// display the colors
analogWrite(ledRed, redPwr);
analogWrite(ledGreen, greenPwr);
analogWrite(ledBlue, bluePwr);
delay(20);
}
}

The light stays on all the time but the colour comes through.
I actually prefer it better this way. It will look better in what i will
use it for. Smiley

Then simply changing the outlining brightness parameters will flip that around. I also trimmed the code a bit. No need to have more then 1 fade() calls in there when the defining of the color can be doen at the button push.

const int ledPinR = 9;
const int ledPinG= 11;
const int ledPinB = 10;

const int buttonPin1 = 3;
const int buttonPin2 = 4;

int buttonState1 = 0;
int buttonState2 = 0;

int redPwr = 0;
int greenPwr = 0;
int bluePwr = 0;
int faderR;
int faderG;
int faderB;

void setup()
{
  pinMode(ledPinR, OUTPUT);
  pinMode(ledPinG, OUTPUT);
  pinMode(ledPinB, OUTPUT);
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  digitalWrite(buttonPin1, HIGH);
  digitalWrite(buttonPin2, HIGH);
  analogWrite(ledPinR, redPwr);
  analogWrite(ledPinG, greenPwr);
  analogWrite(ledPinB, bluePwr);
}

void loop()
{
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2 );
  if (buttonState1 == LOW){
    faderR = 246;
    faderG = 235;
    faderB = 0;
    analogWrite(ledPinR, faderR);
    analogWrite(ledPinG, faderG);
    analogWrite(ledPinB, faderB);
    delay(1000);
    fade(redPwr,greenPwr,bluePwr,50);
  }

  if (buttonState2 == LOW) {
    faderR = 100;
    faderG = 0;
    faderB = 100;
    analogWrite(ledPinR, faderR);
    analogWrite(ledPinG, faderG);
    analogWrite(ledPinB, faderB);
    delay(1000);

    fade(redPwr,greenPwr,bluePwr,50);
  }

}


void fade(int redFrom, int grnFrom, int bluFrom, int delayTime)
{
  
  for (int fader=255; fader>=0; fader-=5)
  {
    faderR -= 5;
    faderG -= 5;
    faderB -= 5;

    if (faderR < 0)
    {
      faderR = 0;
    }
    if (faderG < 0)
    {
      faderG = 0;
    }
    if (faderB < 0)
    {
      faderB = 0;
    }

    analogWrite(ledPinR, faderR);
    analogWrite(ledPinG, faderG);
    analogWrite(ledPinB, faderB);
    delay(delayTime);
  }
}

Now if you want to randomly choose between pre set colors this is one way you can accomplish that. Set each line in the appropriate area. The void rand() can go before the void loop() or any of the other loops as ling as it s not in them. Then where you have your fade happening you replace the assignment of the faders with rand().

int randomNum;

void setup()
{
  randomSeed(analogRead(0); // uses anolog pin 0 for more random number
}

void rand()
{
  randomNum = random(1, 3); // choose a random number from 1 to 10
  if(randomNum == 1)
  {
    // store brightnes values to redPwr, greenPwr and bluePwr
  }
  if(randomNum == 2)
  {
    // store brightnes values to redPwr, greenPwr and bluePwr
  }
  if(randomNum == 3)
  {
    // store brightnes values to redPwr, greenPwr and bluePwr
  }
}

something more to the point of what you want would be this.

int randomNumR;
int randomNumG;
int randomNumB;

void setup()
{
  randomSeed(analogRead(0); // uses anolog pin 0 for more random number
}

void rand()
{
  randomNumR = faderR(256); // choose a random number from 0 to 255 and store it too LED brightness
  randomNumG = faderG(256); // choose a random number from 0 to 255 and store it too LED brightness
  randomNumB = faderB(256); // choose a random number from 0 to 255 and store it too LED brightness
}

Assign each randomNum line to a button and your all set.

Believe it or not i’m learning as much as you aare right now. With every new question you present i’m having to teach myself how to do it to then show you. So the code or methods i present may not be the best but they tend to work> besides there are enough smart people around here that are great about pointing out my many mistakes.