Help needed for a simple RGB Sketch

Hi,

I'm trying to solve a simple problem but it's got me baffled as I'm very new to Arduino sketches. All I want to do is run two RGB LEDs independently. The first I want to change colour from red to blue at one second intervals. The second I want to change colour from green to yellow (also at one second intervals).

I can control a single RGB LED no problem. I can change the colours, brightness and timing but when it comes to two of them I'm pretty lost. I've tried googling for a tutorial but they only seem to exist for single RGB LEDs or for controlling entire LED strips which is beyond what I want at the moment.

Here's the sketch I'm currently trying...

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 = 9;      // the pin numbers for the 2nd LED
const int green_B_Pin = 10;
const int blue_B_Pin = 11;

//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,      255, 0, 0); // red
  delay(1000);
  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, 255, 0); // yellow
  delay(1000);
  setColor(red_B_Pin, green_B_Pin, blue_B_Pin,      0, 255, 0); // green
}


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);
}

When I use this code the "A" pins seem to cycle between green and aqua with a tiny bit of red. The "B" pins cycle though red and purple.

Like I mentioned above, I just want one of the RGBs to cycle between red and blue and the other to cycle between green and yellow. Whatever I'm doing seems to be cross mixing these colours between the two RGB LEDs.

Any help would be greatly appreciated. Thanks

If you want to do more then one thing you need to drop delay() :wink: See blink without delay and other topics pointed to in the stickey at the top of this board.

And if you also want some fading, have a look at FadeLed :slight_smile:

Hi Vanoyen,

As @septillion told, you can't use delay(). Delay just freezes the arduino. What you need is something like this:

unsigned long previous_led_change = 0;
void loop(){
   if(millis() - previous_led_change >= 1000){
      previous_led_change = millis();
      //do what you wan't to do with your LED's here. It repeats every 1 second (1000 milliseconds)

   }
}

Arduino's example sketches are very helpful for learning arduino!