Finding Orange on a Common Anode RGB LED

I had it. I looked online and got:
if (LEDcolor==2) {analogWrite(redPin, 5);analogWrite(greenPin, 215);analogWrite(bluePin, 255);Serial.println(“Orange”);}

or (5,215,255)

But recently I noticed that the orange is just red now. I put those numbers in, and poof, RED.

//Thanks to Grumpy_Mike and tuxduino on the Arduino.cc boards

unsigned long keyPrevMillis = 0;
const unsigned long keySampleIntervalMs = 25;

int redPin = 11;
int greenPin = 10;
int bluePin = 9;
int LEDcolor=0; // 0=White, 1=Red, 2=Orange, 3=Yellow, 4=Green, 5-Blue,  6=Purple, 7=Pink 
int LEDstatus =0; //0=Off, 1=On
int changed =0; //has the LEDstatus just changed?
byte longKeyPressCountMax = 80;    // 80 * 25 = 2000 ms
byte longKeyPressCount = 0;

byte prevKeyState = HIGH;         // button is active low
const byte keyPin = 2;            // button is connected to pin 2 and GND

// called when button is kept pressed for less than 2 seconds
void shortKeyPress() 
  {
    Serial.println("short");
    if (LEDstatus==1)
    {
    LEDcolor = LEDcolor +1;
    if (LEDcolor==8) {LEDcolor=0;}
    if (LEDcolor==0) {analogWrite(redPin, 0);analogWrite(greenPin, 0);analogWrite(bluePin, 0);Serial.println("White");}
    if (LEDcolor==1) {analogWrite(redPin, 0);analogWrite(greenPin, 255);analogWrite(bluePin, 255);Serial.println("Red");}
    if (LEDcolor==2) {analogWrite(redPin, 5);analogWrite(greenPin, 215);analogWrite(bluePin, 255);Serial.println("Orange");}
    if (LEDcolor==3) {analogWrite(redPin, 0);analogWrite(greenPin, 0);analogWrite(bluePin, 255);Serial.println("Yellow");}
    if (LEDcolor==4) {analogWrite(redPin, 255);analogWrite(greenPin, 0);analogWrite(bluePin, 255);Serial.println("Green");}
    if (LEDcolor==5) {analogWrite(redPin, 255);analogWrite(greenPin, 255);analogWrite(bluePin, 0);Serial.println("Blue");}
    if (LEDcolor==6) {analogWrite(redPin, 175);analogWrite(greenPin, 255);analogWrite(bluePin, 175);Serial.println("Purple");}
    if (LEDcolor==7) {analogWrite(redPin, 0);analogWrite(greenPin, 255);analogWrite(bluePin, 155);Serial.println("Pink");}
    }
    else
    {
      Serial.println("NOTHING");
    }
  }

// called when button is kept pressed for more than 2 seconds
void longKeyPress() 
  {
    Serial.println("long");
    if (LEDstatus==0 && changed==0)
    {
    if (LEDcolor==0) {analogWrite(redPin, 0);analogWrite(greenPin, 0);analogWrite(bluePin, 0);}
    if (LEDcolor==1) {analogWrite(redPin, 0);analogWrite(greenPin, 255);analogWrite(bluePin, 255);}
    if (LEDcolor==2) {analogWrite(redPin, 5);analogWrite(greenPin, 215);analogWrite(bluePin, 255);}
    if (LEDcolor==3) {analogWrite(redPin, 0);analogWrite(greenPin, 0);analogWrite(bluePin, 255);}
    if (LEDcolor==4) {analogWrite(redPin, 255);analogWrite(greenPin, 0);analogWrite(bluePin, 255);}
    if (LEDcolor==5) {analogWrite(redPin, 255);analogWrite(greenPin, 255);analogWrite(bluePin, 0);}
    if (LEDcolor==6) {analogWrite(redPin, 175);analogWrite(greenPin, 255);analogWrite(bluePin, 175);}
    if (LEDcolor==7) {analogWrite(redPin, 0);analogWrite(greenPin, 255);analogWrite(bluePin, 155);}
      changed=1;
      LEDstatus=1;Serial.println("On");
    }
    if (LEDstatus==1 && changed==0)
    {
      analogWrite(redPin, 255);
      analogWrite(greenPin, 255); 
      analogWrite(bluePin, 255);
      changed=1;
      LEDstatus=0;Serial.println("Off");
    }
    longKeyPressCount = 0;
  }

// called when key goes from not pressed to pressed
void keyPress() {
    Serial.print("key press / ");
    longKeyPressCount = 0;
}


// called when key goes from pressed to not pressed
void keyRelease() 
{
    Serial.println("key release");
    
    if (longKeyPressCount < longKeyPressCountMax && changed==0) {
          shortKeyPress();
}
changed=0;
}

void setup() 
  {
    Serial.begin(9600);
    pinMode(keyPin, INPUT);
    digitalWrite(keyPin, HIGH);
    pinMode(redPin, OUTPUT);
    pinMode(greenPin, OUTPUT);
    pinMode(bluePin, OUTPUT); 
    analogWrite(redPin, 255);
    analogWrite(greenPin, 255); 
    analogWrite(bluePin, 255);
  }

void loop() {
    // key management section
    if (millis() - keyPrevMillis >= keySampleIntervalMs) {
        keyPrevMillis = millis();
        
        byte currKeyState = digitalRead(keyPin);
        
        if ((prevKeyState == HIGH) && (currKeyState == LOW)) {
            keyPress();
        }
        else if ((prevKeyState == LOW) && (currKeyState == HIGH)) {
            keyRelease();
        }
        else if (currKeyState == LOW) {
            longKeyPressCount++;
        }

        prevKeyState = currKeyState;
    }
        if (longKeyPressCount >= longKeyPressCountMax) {
        longKeyPress();
            }
}

So my mission was to “Find Orange” again.

I wrote this to go through all LED combinations and I noticed a strange behavior:

//Thanks to Grumpy_Mike and tuxduino on the Arduino.cc boards

unsigned long keyPrevMillis = 0;
const unsigned long keySampleIntervalMs = 25;

int redPin1 = 3;
int greenPin1 = 4;
int bluePin1 = 5;
int redVal = 0;
int greenVal = 0;
int blueVal = 255;

byte prevKeyState = HIGH;         // button is active low
const byte keyPin = 2;            // button is connected to pin 2 and GND


// called when key goes from not pressed to pressed
void keyPress() {
    Serial.print("key press / ");
}


// called when key goes from pressed to not pressed
void keyRelease() 
{
    Serial.println("key release");
    Serial.print(redVal);
    Serial.print(", ");
    Serial.print(greenVal);
    Serial.print(", ");
    Serial.println(blueVal);
    delay(10000);
}

void setup() 
  {
    Serial.begin(9600);
    pinMode(keyPin, INPUT);
    digitalWrite(keyPin, HIGH);
    pinMode(redPin1, OUTPUT);
    pinMode(greenPin1, OUTPUT);
    pinMode(bluePin1, OUTPUT); 
    analogWrite(redPin1, redVal);
    analogWrite(greenPin1, greenVal); 
    analogWrite(bluePin1, blueVal);
  }

void loop() {
while (blueVal>0){
while (redVal<=255){
while (greenVal<=255){
        byte currKeyState = digitalRead(keyPin);

        if ((prevKeyState == HIGH) && (currKeyState == LOW)) {
            keyPress();
        }
        else if ((prevKeyState == LOW) && (currKeyState == HIGH)) {
            keyRelease();
        }
        prevKeyState = currKeyState;
 
  delay(100);
    analogWrite(redPin1, redVal);
    analogWrite(greenPin1, greenVal); 
    analogWrite(bluePin1, blueVal);
    Serial.print(redVal);
    Serial.print(", ");
    Serial.print(greenVal);
    Serial.print(", ");
    Serial.println(blueVal);
greenVal=greenVal+5;
}
greenVal=0;
redVal=redVal+5;
}
redVal=0;
blueVal=blueVal-5;
}
blueVal=0;
}

The LED doesn’t fade through colors like I had expected. Instead, it has a sudden transition when the green loop hits 128ish and the 0/255 change.

Why would it do this? It doesn’t matter how long or short the delay is between color changes, no fade, just a quick blink from reddish to greenish most times.

I swapped LEDs or set up multiple LEDs with the same behavior.

Thanks in advance!

How is the led wired? What value series resistors and how many?

To each of the leads of the RGB, I have 270R resistors. 5v is going to the common Anodes.

You are not using the correct pins. analogWrite() only works with pins 3, 5, 6, 9, 10, and 11 on Uno. You can't use 4, if you try, you will just get 0 or 255, nothing in-between.

Also, you should have a higher resistor on the red pin. With 270R on blue and green, you need something around 400R to 500R on red, otherwise everything will look "too red" and you won't get anything close to white.

Wow, I did not know that.... Let me rewire it up and see what happens.

Look here:

PWM: 3, 5, 6, 9, 10, and 11. Provide 8-bit PWM output with the analogWrite() function.

That fixed it! I think in my attempt to make multiple "channels" of LEDs, I decided the channels could go 3,4,5 / 6,7,8 / 9,10,11. Obviously NOT the case.

We are back to Orange being 5,215,255

Thanks a ton!