RGB LED w/ Potentiometer Script w/ Comments

Hi All,

I have a commented script which will allow you to alter an RGB LED w/ a Potentiometer.
I’m a noob at this and had a tough time figuring out how to use an RGB LED, so i figured i’d comment this script for others. Here is the script and comments to help out anyone else that is looking to do similar.

/*
 RGB LED w/ Potentiometer

 This script will allow a potentiometer to adjust the RGB values of an RGB LED to adjust it's color.
 It will also output values to the serial monitor to view the values. (Potentiometer value, Color Hue degree, RGB values)
 
 Components used:
   - Arduino Duemilanove
   - Solderless Breadboard
   - Easy wire connectors
   - 5mm High Brightness Full-Color LED (4.0V max green/blue, 2.6V max red)
   - 10kom Potentiometer
 
 Quick Fact:  
   - An RGB LED is actually made up of 3 tiny LEDS inside of one!
   - Pot = Potentiometer
 
 The circuit:
 * Potentiometer connected to analog pin 2 w/ pos 5v and grounded neg
 * RGB LED connections are in this order when looking at LED:  Red, Common Annode(Longest - also where 5v power is connected), Green, Blue
 * RGB LED attached from digital pin 9(red), 10(green), 11(blue) w/ common annode to 5v power.
 
 Created 8 May 2010
 Commented by Dave a.k.a Ka0ticstyle
 
 */

int potpin = 2;              // POT connected to digital pin 2 - pos/neg are left and right connections on POT
int rpin = 9;                // Red
int gpin = 10;               // Green
int bpin = 11;               // Blue
float h;                     // Hue range
int h_int;                   // Hue color
int r = 0, g = 0, b = 0;           // Default RGB values

int val = 0;                   // Set POT value to default 0

void h2rgb(float h, int& R, int& G, int& B); // Instantiate h2rgb and it's variables  a.k.a  Hue to RGB

void setup()                    // Run once, when the sketch starts
{
  Serial.begin(9600);          // Begin the output of data to serial
}


void loop()                     // Run over and over again
{
  val = analogRead(potpin);    // Read the pin and display the value
  h = ((float)val)/1024;       // Get the range. pot value / 1024
  h_int = (int) 360*h;         // Get the color hue by multiplying by 360

  h2rgb(h,r,g,b);              // Call the h2rgb function passing it the hue value

  Serial.print("POT value: ");
  Serial.print(val);           // Pot value
  Serial.print(" = Hue of ");
  Serial.print(h_int);         // Color Hue value
  Serial.print(" degrees. RGB values: ");
  Serial.print(r);             // Red value
  Serial.print(" ");
  Serial.print(g);             // Green value
  Serial.print(" ");
  Serial.println(b);           // Blue value

  analogWrite(rpin, r);        // Changes red led
  analogWrite(gpin, g);        // Changes green led
  analogWrite(bpin, b);        // Changes blue led
  
}

void h2rgb(float h, int& R, int& G, int& B) {

  // Used HSV --> RGB function
  // HSV - Hue, Saturation, Value
  // RGB - Red, Green, Blue - example (255,255,255)
  // Function below does a bunch of math to convert HSV values to RGB
  int var_i;
  float S=1, V=1, var_1, var_2, var_3, var_h, var_r, var_g, var_b;

  if ( S == 0 )                       //HSV values = 0 ÷ 1
  {
    R = V * 255;
    G = V * 255;
    B = V * 255;
  }
  else
  {
    var_h = h * 6;
    if ( var_h == 6 ) var_h = 0;      //H must be < 1
    var_i = int( var_h ) ;            //Or ... var_i = floor( var_h )
    var_1 = V * ( 1 - S );
    var_2 = V * ( 1 - S * ( var_h - var_i ) );
    var_3 = V * ( 1 - S * ( 1 - ( var_h - var_i ) ) );

    if      ( var_i == 0 ) {
      var_r = V     ;
      var_g = var_3 ;
      var_b = var_1 ;
    }
    else if ( var_i == 1 ) {
      var_r = var_2 ;
      var_g = V     ;
      var_b = var_1 ;
    }
    else if ( var_i == 2 ) {
      var_r = var_1 ;
      var_g = V     ;
      var_b = var_3 ;
    }
    else if ( var_i == 3 ) {
      var_r = var_1 ;
      var_g = var_2 ;
      var_b = V     ;
    }
    else if ( var_i == 4 ) {
      var_r = var_3 ;
      var_g = var_1 ;
      var_b = V     ;
    }
    else                   {
      var_r = V     ;
      var_g = var_1 ;
      var_b = var_2 ;
    }

    R = (1-var_r) * 255;                  //RGB results = 0 ÷ 255
    G = (1-var_g) * 255;
    B = (1-var_b) * 255;
  }
}

Hope this helps!

Hello, I want to build RGB LEDs controller. It will be controlled by two voltages, one will be changing hue (0-5V), another brightness (0-4V). Control Voltages come from analog music controller - pitch(1V per octave) and volume. Is it possible to use this code to make such device with PIC? I really need help with it.

to make such device with PIC?

Yes, quite possible, but your question would be better asked at a Microchip/PIC forum.

would there be a way to modify this to use three pots and 10 rgb leds? im thinking i could use this http://www.arduino.cc/playground/Main/RGBLEDPWM set up, and add a transistor of some sort. what do you guys think?

@kaoticstyle: Goood commenting, but comments in the HSV2RBG routine are missing. If you did that you would have noticed that the use of float here is not really needed (though it makes more transparent). Task: Modify it, so H,S, and V are transformed to 255 rather than 1 :-)

@ Arti G: The script kindly posted above does this already when you add two more lines (and make "V" a parameter)

@The Seaker: There is already a thread about this?

neat- I was trying to do exactly this :) I made a whole other code before i found yours, which works better. Note: depending on the pot, the fade can be pretty choppy.