Question

I'm having some trouble setting up my RGB led. It's not common anode, so it only has 2 pins. Not 4. I can't seem to set the led up to change color. It will only blink red. I want the led to cycle through colors. Thanks for Helping!

int led1 = 2; //Don't worry about the other led variables, they work
int led2 = 7;
int led3 = 9;
int led4 = 12;
int led5 = 13;
int redPin = 5;int greenPin = 4;
int bluePin = 3;

//#define COMMON_ANODE

void setup(){
  pinMode(led1, OUTPUT);
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
}

void loop(){
 setColor(0, 255, 0);  // green
  delay(1000);
  setColor(0, 0, 255);  // blue
  delay(1000);
  setColor(255, 255, 0);  // yellow
  delay(1000);  
  setColor(80, 0, 80);  // purple
  delay(1000);
  setColor(0, 255, 255);  // aqua
  delay(1000);

}

 void setColor(int red, int green, int blue){
  #ifdef COMMON_ANODE
    red = 255 - red;
    green = 255 - green;
    blue = 255 - blue;
  #endif
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);  
}
Was it helpful?

Solution

First of all this is not a software question.

When it comes to your problem, 2 pinned LEDs don't work the way 4 pinned ones work. If you are using an LED with 2 pins, you give them a voltage and wait for them to change their color with time.

However, you can change their 'Color changing time' applying PWM to it's Vcc pin. But on the other hand it makes it illuminate less.

OTHER TIPS

Since you have a slow color changer you cannot control the timing through PWM. PWM is going to turn the LED on then off at a high rate. The color changing circuit in those 2 pin slow/fast color changer LEDs turns on when you apply voltage. As long as power is applied the program runs. When you turn it off and then back on (unless it is storing internal data) it will reset and start from the beginning of its programmed cycle.

Try this little experiment: Turn the LED on and wait 5 seconds. Take note of the color pattern. Turn it off and wait 2 seconds. Then turn it on again.

If it the color pattern starts over, then that LED "forgets" where it was and is reset every time power is applied.

If it continues where it left off (which I highly doubt), then it has internal NV memory and you might be able to control the changing speed but only in making it slower.

If after 5 seconds that LED never changes, I would have to say that it is a plain single color LED. To see if it is a Bi-Color type, try reversing the polarity. If you get another color, you have I Forward-Reverse Bi-Color LED. If not, you have a plain LED.

Hope that helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top