Getting two RGB LED to be independent of each other using two sets of pwm pins

Hi,

First off, thank you for taking the time to offer your advice and helping me out.

I am totally new to Arduino and am loving learning how to do things. I have read through a few basic examples such as 'blink' and one on RGB LED.

While I can successfully control / program one RGB led with colours, fades delays etc I now would like to move to controlling two RGB leds so each is independent of each other.

Using the basic programming for one RGB led and also some coding from the 'several things at once' example I have compiled the code below.
This has been successful in getting BOTH leds to cycle through the colours using the six pwm pins, goal one achieved ... go me!

Here is the code I am using to achieve this so far:

const int red_A_Pin = 3; // the pin numbers for the 1st LED
const int green_A_Pin = 5;
const int blue_A_Pin = 6;
const int red_B_Pin = 11; // the pin numbers for the 2nd LED
const int green_B_Pin = 10;
const int blue_B_Pin = 9;

//uncomment this line if using a Common Anode LED
#define COMMON_ANODE

void setup()
{
pinMode(red_A_Pin, OUTPUT);
pinMode(green_A_Pin, OUTPUT);
pinMode(blue_A_Pin, OUTPUT);
pinMode(red_B_Pin, OUTPUT);
pinMode(green_B_Pin, OUTPUT);
pinMode(blue_B_Pin, OUTPUT);
}
void loop()
{
setColor(255, 0, 0); // red
delay(1000);
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(red_A_Pin, red);
analogWrite(green_A_Pin, green);
analogWrite(blue_A_Pin, blue);
analogWrite(red_B_Pin, red);
analogWrite(green_B_Pin, green);
analogWrite(blue_B_Pin, blue);
}

Goal Two:

What I would like to do now is to separate the two RGB leds so they both do something different. At present they are both running the same cycle of colours albeit not in sync but that is irrelevant.

What I would like to achieve is RGB_A to be BLUE while RGB_B would be RED.

I think the part of the coding I need to change is this part:

void loop()
{
setColor(255, 0, 0); // red
delay(1000);
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);
}

But I do not know what I need to be using. I'm guessing it will be something which defines A and B leds somehow. I have tried adding A and B like this: setColor_A or setColor_B bu I get error messages

Could someone point me in the right direction please :slight_smile:

I am using an Arduino Uno, resistors and LED - RGB clear common anode

Thank you for your time and any help / advice you are willing to give.

Note to mods - I'm fairly confident I have the right section of the forum, if not please feel free to move my post. But do let me know where it's moved too :slight_smile:

One way to achieve what you want is to change the setColor() function to accept pin numbers as parameters then pass in the pin numbers of the LED you would like to change.

const int red_A_Pin = 3;      // the pin numbers for the 1st LED
const int green_A_Pin = 5;
const int blue_A_Pin = 6;
const int red_B_Pin = 11;      // the pin numbers for the 2nd LED
const int green_B_Pin = 10;
const int blue_B_Pin = 9;

//uncomment this line if using a Common Anode LED
#define COMMON_ANODE

void setup()
{
  pinMode(red_A_Pin, OUTPUT);
  pinMode(green_A_Pin, OUTPUT);
  pinMode(blue_A_Pin, OUTPUT);
  pinMode(red_B_Pin, OUTPUT);
  pinMode(green_B_Pin, OUTPUT);
  pinMode(blue_B_Pin, OUTPUT);
}
void loop()
{
  setColor(red_A_Pin, green_A_Pin, blue_A_Pin, 0, 0, 255); // blue
  setColor(red_B_Pin, green_B_Pin, blue_B_Pin, 255, 0, 0); // red
  delay(1000);

}


void setColor(int redPin, int greenPin, int bluePin, 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);

}

Welcome to the forum.
Good to see you have had some success.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Tom... :slight_smile:

Hi Blue Eyes..... Thank you so much. :slight_smile: Goal 2 achieved.

I'm assuming that if I wanted to add in a third and fourth RGB led than I would just amend the code similar to how it's modified for led 'B', only with 'C' and 'D' plus the pin numbers used for their power.

TomGeorge,
thanks for the link on how to put code into a post. I did try that initially with the result I got a lot of what looked like page mark up code. I couldn't see any of my Arduino code at all. Having re-read the post I think I was going wrong on the Arduino sketch box. I had used the option of 'Copy for Forum'. I think this copied the page source code rather than my coding for the board.

I was going to try again by putting in an example of the code for led 'C' and 'D' using that method but can't find the insert code tab on this quick reply box.

Just a quick question on terminology, is the code in the sketch box referred to as coding, programing, a sketch or something else?

Oh, and one last question (for now) In order to run 4 RGB leds independently, would a need a different Arduino board with more PWM pins? (something like an Arduino Mega 2560 Rev3 which has 14 pwm pins).
or is there a way to do that with the Arduino Uno board?

Again thank you for your help and advice. I am LOVING playing and learning with Arduino. :slight_smile:

Yes, to add more LEDs you could follow the same pattern but, as you are aware, you are out of PWM pins on an Uno.

I suggest you aim for making your code more readable / maintainable / compact by learning how to store data in arrays. See the IDE example under Examples => Control => Arrays.

If you run the code below and look at the serial monitor, it should give you an idea of how this applies to your own program.

byte pinsA[] = {
  3, 5, 6};

byte pinsB[] = {
  11, 10, 9};

void setup()
{

  Serial.begin(9600);

}

void loop()
{

  showValues(pinsA);
  showValues(pinsB);
  delay(2000);

}

void showValues(byte pins[])
{

  Serial.print("pin 1:  ");
  Serial.println(pins[0]);

  Serial.print("pin 2:  ");
  Serial.println(pins[1]);

  Serial.print("pin 3:  ");
  Serial.println(pins[2]);

  Serial.println("");

}
[quote author=Blue Eyes link=msg=2995071 date=1478786848]

I suggest you aim for making your code more readable / maintainable / compact by learning how to store data in arrays.  See the IDE example under Examples => Control => Arrays.

Hi Blue Eyes,

That may be getting a bit complicated for my needs as I don't think there will be a lot of coding / programming involved. I could be very wrong tho. I will certainly check it out tho, thank you:-)

The project I am working on just requires two sets of four RGB leds. Two Arduino boards each controlling 4 groups of separate RGB leds.

The first board will have groups 1 - 4 (or A - D) and the second board will have groups 5 - 8 (or E- H)
Programming wise the light show is very simple. Each group will start 'Off' First push of a remote will turn all groups on however each group will have a different colour (I should have eight different colours on view) With the second push of a remote the colours will do a simple chase patten. For example the leftmost group (Group 1 or A) will start off Red and progress in sequence through groups 2 - 8 (or groups B- H)

I did put a post up a couple of weeks ago describing my project, here's the link to that post: http://forum.arduino.cc/index.php?topic=430273.0

I'm not sure how to turn that into a clickable link but a copy and paste will work.

The above describes the project I am attempting to build for my show, hopefully it's not too confusing.

I'm looking at getting two of the Arduino Mega 2560 boards to run the LED groups. Then have to work out the wireless remote part.