Go Down

Topic: 2 RGB LEDs that are not the same color, on one uno board  (Read 183 times) previous topic - next topic

ZoeyAK

Hello, im working on trying to get two RGB LEDs to be different colors (Ex. LED 1 is green while LED 2 is red, then LED 1 turns red while LED 2 is green). I understand how to code multiple RGB LEDs the same color, but i have not been able to find any support with coding the LEDs to be different colors while on the same board (uno R3).

The RGB LEDs are common cathodes.

Here is the code so far:

Code: [Select]

int redPin1 = 11;        //LED 1
int greenPin1 = 10;    //LED 1
int bluePin1 = 9;        //LED 1
int redPin2 = 6;         //LED 2
int greenPin2 = 5;     //LED 2
int bluePin2 = 3;       //LED 2


void setup()
{
 
  pinMode(redPin1, OUTPUT);
  pinMode(greenPin1, OUTPUT);
  pinMode(bluePin1, OUTPUT); 
  pinMode(redPin2, OUTPUT);
  pinMode(greenPin2, OUTPUT);
  pinMode(bluePin2,OUTPUT);

}
void loop()
{
  setColor(255, 0, 0);  // red
  delay(50);
  setColor(0, 255, 0);  // green
  delay(50);
}
void setColor(int red, int green, int blue)
{
 
 
  analogWrite(redPin1, red);
  analogWrite(greenPin1, green);
  analogWrite(bluePin1, blue);
  analogWrite(redPin2, red);
  analogWrite(greenPin2, green);
  analogWrite(bluePin2, blue);

}


Right now this code has both LEDs blinking at the same time and are changing from green to red at the same time. If you could help me edit this code to where the LEDs are different colors at the same time and if there are articles that can help me, i would greatly appreciate it! :D

PaulRB

First of all, well done for using code tags.

Secondly, can I check how many series resistors you are using? The correct answer is 6. If you have 2, that is wrong but probably won't damage the leds or the Arduino. If you have none, that could certainly damage the leds and the Arduino.

Try this:
Code: [Select]


int redPin[2] = {11, 6};
int greenPin[2] = {10, 5};
int bluePin[2] = {9, 3};

void setup()
{
  for (int led = 0; led < 2; led++) {
    pinMode(redPin[led], OUTPUT);
    pinMode(greenPin[led], OUTPUT);
    pinMode(bluePin[led], OUTPUT);
  }
}

void loop()
{
  setColor(0, 255, 0, 0);  // red
  setColor(1, 0, 255, 0);  // green
  delay(500);
  setColor(0, 0, 255, 0);  // green
  setColor(1, 255, 0, 0);  // red
  delay(500);
}

void setColor(int led, int red, int green, int blue)
{
  analogWrite(redPin[led], red);
  analogWrite(greenPin[led], green);
  analogWrite(bluePin[led], blue);
}



There are probably two new programming concepts in there that you have not used before. Arrays and for-loops. Read up on them in the Reference page under the Learning menu at the top of your screen. Ask if you have any questions.

ZoeyAK

PaulRB, thank you very much!

I am using 6 220 ohm's resistors.

https://postimg.org/gallery/jcya5qu6/0237491e/

The code works flawlessly. Now in the loop:

Code: [Select]

{
  for (int led = 0; led < 2; led++) {
    pinMode(redPin[led], OUTPUT);
    pinMode(greenPin[led], OUTPUT);
    pinMode(bluePin[led], OUTPUT);
  }
}

void loop()
{
  setColor(0, 255, 0, 0);  // red
  setColor(1, 0, 255, 0);  // green
  delay(1000);
  setColor(0, 255, 0, 0);  // green
  setColor(1, 0, 255, 0);  // red
  delay(1000);
}



I understand that the last three variables  in setColor() are the RGB color code, but what is  the first integer in the beginning of each setColor()? is it from the variable to integer led in the for-loop?

PaulRB

Its the number of the led you want to set the colour of. Either 0 or 1. (When you number things in C/C++ you start at zero.)

Using 220R for all 6 series resistors, you may find that when you try to make white by using 255, 255, 255 it comes out a little on the pink side. To fix that either reduce the red number by a few tens, or use a slightly higher series resistors for the red pins on the leds, like 330R.

Go Up