blink a RGB LED


I searched for hours but didn't find any solution, sorry for asking another noob question. For my first arduino project I am trying to build a little RGB strobe, that combines two functions: a RGB mixer based on 3 pots, and a blinking velocity control with another pot. I am using a common anode RGB LED. the RGB mixer is working great, based on this sketch: But the blinking part is not. I'd like the LED to simply turns on and off, and control the delay between the on and the off with a pot value. But instead, it blinks between two colors (depending on the value I give with the RGB mixer).

Any suggestions? Here is my code below:

/*note that we use common anode RGB LED, so anone is connected to 5V
and a write LOW turns the LED on while a write HIGH turns it off.
invert these for common cathode RGB LED*/

// Init the Pins used for PWM
const int redPin = 9;
const int greenPin = 10;
const int bluePin = 11;
// Init the Pins used for color pots
const int redPotPin = 0;
const int greenPotPin = 1;
const int bluePotPin = 2;
//Init the Pin used for velocity pot
const int velocityPotPin = 3;
// Init Variables
int currentColorValueRed;
int currentColorValueGreen;
int currentColorValueBlue;
int velocityValue; 

void setup() {
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
void loop() {
  // Read the voltage on each analog pin then scale down to 0-255 and inverting the value for common anode
  currentColorValueRed = (255 - map( analogRead(redPotPin), 0, 1024, 0, 255 ) );
  currentColorValueBlue = (255 - map( analogRead(bluePotPin), 0, 1024, 0, 255 ) );
  currentColorValueGreen = (255 - map( analogRead(greenPotPin), 0, 1024, 0, 255 ) );
  // Write the color to each pin using PWM and the value gathered above
  analogWrite(redPin, currentColorValueRed);
  analogWrite(bluePin, currentColorValueBlue);
  analogWrite(greenPin, currentColorValueGreen);
  //Read velocity pot value
  velocityValue = analogRead(velocityPotPin);
  //"LOW" PORTB pins
  PORTB = B00000000;
  //"HIGH" PORTB pins
  PORTB = B11111111;

Another thing you can probably try is to set the LED pin that normally would go to ground, to a regular I/O pin and just bring that HIGH and LOW. And being that it is an LED which functions like a regular diode, there shouldn't be any flowback, so not harm should come to the Arduino.

I tried it myself and I didn't have any repercussions afterwards, everything still works fine. Maybe someone else can confirm this.

Also if you still want to do port manipulation, you don't need to turn everything on and off, just do the ones you need.

Once you use a digital direct port output then anything you have done with the analogue output is out of the window.

Drop the direct port output and switch between two values of analogue write that will turn it between the colour you want and off.

Your only need to turn 3 pins on and off but your manipulating all 8 pins on port B

 //"LOW" PORTB pins
  PORTB = B00000000;

Better to either turn the required pins on/off individually using digitalWrite() or maybe using read/modify/write (I'm not sure if the arduino core does this or not)

@HazardsMind : once my sketch works, I'd like to connect 2 or 3 LEDs simultaneously, so I would be afraid of a short curcuit with your technique...

concerning ports, I set all 8 pins the same because I tried different pins to see if there was any difference, and forgot to change that before copying and paste, sorry for that.

I already tried digitalWrite, without success. Maybe with analogWrite it would work, I'll try this out!

I tried to use analogWrite, adding this at the beginning of the sketch:

const int allPin = (9, 10, 11);

setting that as output,

and adding this in the loop:

  analogWrite(allPin, 0);
  analogWrite(allPin, 255);

but now it doesn't even blink at all... What have I done wrong?

What have I done wrong?

const int allPin = (9, 10, 11);

Is rubbish, do you want an array? If so you define it as:-

const int allPin[] = {9, 10, 11};

What kind of arduino do you have UNO or Mega? If you have an UNO then you can only hook up one other rgb led, because each color requires 3 pwm pins, the UNO only has 6. The Mega on the other hand has 14 PWM pins.

I have an arduino UNO. But I was wondering if it was possible to connect two LEDS in serial to the arduino, in order to have two RGB LEDs connected to only 3 PWM pins of the arduino?

I tried your method which is working fine, thanks a lot HazardsMind (and everyone who helped me!)

aldrff: I was wondering if it was possible to connect two LEDS in serial to the arduino, in order to have two RGB LEDs connected to only 3 PWM pins of the arduino?

If you had independent LEDs, you could connect them in series as you describe. In this case your LED packages would have six terminals. However, your RGB LEDs probably have either a common cathode or a common anode configuration with four terminals, in which case you don't have access to both ends of each LED and can't connect them up independently.

PeterH: ...and can't connect them up independently.

But what if I don't need to connect them up independently? Could I just serial them on my breadboard and connect both on only 4 pins (as if I had only one common anode RGB LED)?

Sorry for asking stupid questions, but I'm a bit confused...

Could I just serial them on my breadboard and connect both on only 4 pins (as if I had only one common anode RGB LED)?

No. Draw the circuit out, you will see that it will not work.

I agree that connecting RGB LED's in series will not work. If you want to do something like that, I suggest that you get a LED Matrix. There are single color, dual color, and RGB LED matrix's on the market. There is also a form of multiplexing for LED's known as 'charlieplexing' that can be utilized for single color LED's. I'm not sure how or if it can be used with RGB's.

OK, thank you everyone for your answers!