So I picked up an RGB LED and it is common anode (+) and I am wondering how to change this code to work with this LED. The code is:
//pins for the LEDs:
const int redPin = 3;
const int greenPin = 5;
const int bluePin = 6;
const int redPin2 = 9;
const int greenPin2 = 10;
const int bluePin2 = 11;
#define REDPIN 3
#define GREENPIN 5
#define BLUEPIN 6
#define FADESPEED 5
void setup() {
// initialize serial:
Serial.begin(9600);
// make the pins outputs:
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(redPin2, OUTPUT);
pinMode(greenPin2, OUTPUT);
pinMode(bluePin2, OUTPUT);
Serial.print("Arduino control RGB LEDs Connected OK ( Sent From Arduinno Board )");
Serial.print('\n');
}
void loop() {
// if there's any serial available, read it:
while (Serial.available() > 0) {
// look for the next valid integer in the incoming serial stream:
int red = Serial.parseInt();
// do it again:
int green = Serial.parseInt();
// do it again:
int blue = Serial.parseInt();
int red2 = Serial.parseInt();
// do it again:
int green2 = Serial.parseInt();
// do it again:
int blue2 = Serial.parseInt();
// look for the newline. That's the end of your
// sentence:
if (Serial.read() == '\n') {
// constrain the values to 0 - 255 and invert
// if you're using a common-cathode LED, just use "constrain(color, 0, 255);"
//red = 255 - constrain(red, 0, 255);
//green = 255 - constrain(green, 0, 255);
//blue = 255 - constrain(blue, 0, 255);
red = constrain(red, 0, 255);
green = constrain(green, 0, 255);
blue = constrain(blue, 0, 255);
red2 = constrain(red2, 0, 255);
green2 = constrain(green2, 0, 255);
blue2 = constrain(blue2, 0, 255);
// fade the red, green, and blue legs of the LED:
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
analogWrite(redPin2, red2);
analogWrite(greenPin2, green2);
analogWrite(bluePin2, blue2);
// print the three numbers in one string as hexadecimal:
Serial.print("Data Response : ");
Serial.print(red, HEX);
Serial.print(green, HEX);
Serial.println(blue, HEX);
}
}
}
Well it is the code I copied and pasted directly from my arduino software. It works great when I use common ground on separate LEDs but I just bought a common anode LED and it doesn't work now. The pins are right there in the code..3, 5, 6. I'm a bit confused by your response. The code I posted is what I am uploading to the arduino board. But it has to be changed so that in will work with a common anode LED. So I need to set the pins 3, 5, 6 to output as negative and not positive. What do I need to change in the code above for that to happen?
Wire the anode to +5V and the cathodes through your resistor to the input pin. Then the code will be logically inverted. That is a low will turn the LED on and a high off.
This is the normal way to drive LEDs and it is called current sinking.
So instead of the code saying Output I should change it to input? I have already wired it all up but the RGB LED just lights up all three colors. What do I change in the code for it to work? Thanks for your help.
No they should not be inputs.
A digital write to a pin will set the output. Set it low to turn the led on and high for it to be off.
If you have already wired them up then you will have to rewire them.
Yea I just rewired them the way you said with positive to common anode and the rgb pins to the arduino. However there is no High or Low in the code I posted. It is Analog write and constrain. This is for an app on my android phone which works awesome with a common cathode. But like I said I am now using a common anode LED and it doesn't work because I do not know what to change in the code. I understand the High and Low thing but this code doesn't have that in it.
Yes it is HIGH and LOW. However like I said the code I am using doesn't use that. All I need is the pins 3, 5, 6 to be ground and not positive. What do I need to change in the code I posted to achieve this? Thanks.
Yes it is HIGH and LOW. However like I said the code I am using doesn't use that. All I need is the pins 3, 5, 6 to be ground and not positive. What do I need to change in the code I posted to achieve this? Thanks.
I don''t understand that. ?
First thing, I can't read your code. Try reformat it.
For pin 3 to be ground,
const int redPin = 3;
const int greenPin = 5;
const int bluePin = 6;
const int redPin2 = 9;
const int greenPin2 = 10;
const int bluePin2 = 11;
#define REDPIN 3
#define GREENPIN 5
#define BLUEPIN 6
#define FADESPEED 5
void setup() {
Serial.begin(9600);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(redPin2, OUTPUT);
pinMode(greenPin2, OUTPUT);
pinMode(bluePin2, OUTPUT);
Serial.print("Arduino control RGB LEDs Connected OK ( Sent From Arduinno Board )");
Serial.print('\n');
}
void loop() {
while (Serial.available() > 0) {
int red = Serial.parseInt();
int green = Serial.parseInt();
int blue = Serial.parseInt();
int red2 = Serial.parseInt();
int green2 = Serial.parseInt();
// do it again:
int blue2 = Serial.parseInt();
if (Serial.read() == '\n') {
red = constrain(red, 0, 255);
green = constrain(green, 0, 255);
blue = constrain(blue, 0, 255);
red2 = constrain(red2, 0, 255);
green2 = constrain(green2, 0, 255);
blue2 = constrain(blue2, 0, 255);
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
analogWrite(redPin2, red2);
analogWrite(greenPin2, green2);
analogWrite(bluePin2, blue2);
Serial.print("Data Response : ");
Serial.print(red, HEX);
Serial.print(green, HEX);
Serial.println(blue, HEX);
}
}
}
No where in that code is digitalWrite, HIGH, LOW, etc.... They are analog write on pins 3, 5, and 6. As the code sits, pins 3,5, and 6 all have positive voltage coming from them. I need the code to be changed so that they are ground as I am using a common anode LED. Thanks.
I changed the code to say INPUT instead of OUTPUT and uploaded code to arduino. But now when I open the app to control the RGB LED all 3 colors are always on and when I move the sliders to change the color the LED gets dimmer as I move up. It's like backwards. But they never shut off. I am really close I think I just need some more help. Any other thoughts please??
Same issue here with the code being written for common cathode. As you are aware I am using a common anode. I already changed the OUTPUT to INPUT and it works it's just the colors don't match on the LED when I select the corresponding color from my android app. Thanks again.
I already changed the OUTPUT to INPUT and it works
Then change them back, the pins must be an output.
Using PWM. The values are reversed, so if you have a value val then you need to subtract it from 255.
analogWrite(pin,val); // for common cathode
analogWrite(pin,255 - val); // for common anode
So in your code you an change the map values or simply do
ehrja:
You should change pinMode() as an INPUT when you are using common anode(+)
if you are using common cathode(-) use pinMode() as an OUTPUT...
the color pins goes to they'er respective pin in the IC, already configured in software as an INPUT or OUTPUT.
This is rubbish, please do not post rubbish.
What happens when you do this is that instead of setting spin high or low you switch the pull up resistor on or off. This resistor is about 30K so if you just use a scope it will look to have inverted things but the current you get out of it is very limited. So under a restricted set of conditions you get the illusion it is working.