Hi
I would like to use three RGB LEDs in a project. The idea is to have the RGB LEDs cycle though different colors and each of the LED to cycle through the colors at a different rate so that each one is on with a different color at any given moment.
The RGB LEDs are the common cathode type. I have connected the first two LEDs to PWM pins 3, 5, 6 and PWN pins 9, 10, 11. They work fine and give me all the in-between colors as I am expecting.
I connected the other LED to analog pins A0, A1, A2. However when I run the same sequence of colors on this LED it does not produce the in-between colors. It only seems to work correctly for the RGB value combinations of 0 or 255. For example it produces the correct color for (255, 255, 0), but not for (255, 127, 0).
Why does it not work correctly for the analog pins? It looks like it acts as digital pins by if the value is below 127 it becomes 0 (LOW) and if the value is above 127 it becomes 255 (HIGH). For this forum post I have used the "Adafruit Arduino - Lesson 3. RGB LED" code:
/*
Adafruit Arduino - Lesson 3. RGB LED
*/
//int redPin = 11;
//int greenPin = 10;
//int bluePin = 9;
int redPin = A0;
int greenPin = A1;
int bluePin = A2;
//uncomment this line if using a Common Anode LED
//#define COMMON_ANODE
int delayTime = 2000;
void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop()
{
setColor(255, 0, 0); // red //Works for Analog pins
delay(delayTime);
setColor(0, 255, 0); // green //Works for Analog pins
delay(delayTime);
setColor(0, 0, 255); // blue //Works for Analog pins
delay(delayTime);
setColor(255, 255, 0); // yellow //Works for Analog pins
delay(delayTime);
setColor(80, 0, 80); // purple //Does not work for analog pins. LED off
delay(delayTime);
setColor(0, 255, 255); // aqua //Works for Analog pins
delay(delayTime);
setColor(255, 127, 0); // orange //Does not work for analog pins. Displays red
delay(delayTime);
setColor(255, 255, 255); // white
delay(delayTime);
setColor(255, 0, 255); // //Works for Analog pins
delay(delayTime);
setColor(255, 0, 100); // //Does not work for analog pins. Displays red
delay(delayTime);
}
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);
}