rgb led code

this code will control the color of a rgb led using pwm and one potentiometer the only problem is it is written for common anode leds and i need it to do this with a common cathode led… will the code work as is or does it need to be changed some?.. if it does need changed then what changes need to be made?..
thanx

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

For common anode, you need to invert the "analogWrite"s, so "analogWrite (255 - val)"

I may be an idiot i am pretty new at this but I cannot find the part of the code you are telling me to change.. and also this code is going to adjust the color of the led with pwm right?, because it is not going to be driving the led directly it will be done with transistor switching.

You need to change all the "analogWrite"s in your sketch. You can find them by type "ctrl+F", then entering "analogWrite" into the dialogue box that appears.

I assume also that you have changed the wiring to accommodate the change to common cathode. The code as written will work with your type of diode it is just that the controls will be backwards. AWOL's modifications will turn the controls round the right way.

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

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

is this correct?

No, the pin is the name of the connection to the LED, not the PWM value you want to write

You don't want to change the pin number. You want to change the value written to the pin. Try this:

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

Regards,

-Mike

ok i got that workin thanks for all the help but why does it start at red and end at red? when i turn the pot it goes red orange yellow green blue indigo violet red... why? [u]this is with the regular unchanged code and a common anode rgb led[/u]... ( i went to radio shack and bought a common anode rgb led for testing because my high power common cathode isnt here yet)

when i turn the pot it goes red orange yellow green blue indigo violet red... why?

Because of this: h_int = (int) 360*h;?

ahh ok thnx! And one more thing whilst i have your attention… how could i combine this code

int potpin = 0;              // 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) * 500;                  //RGB results = 0 ÷ 255
    G = (1-var_g) * 500;
    B = (1-var_b) * 2;
  }
}

with this code

// Output
int redPin   = 9;   // Red LED,   connected to digital pin 9
int greenPin = 10;  // Green LED, connected to digital pin 10
int bluePin  = 11;  // Blue LED,  connected to digital pin 11

// Program variables set as integer (number) type
int redLEDValue   = 255; // Variables to store the values to send to the pins
int greenLEDValue = 1;   // Initial values are Red full, Green and Blue off
int blueLEDValue = 1;  // These values get passed to the analogWrite() function.
int i = 0;     // Loop counter

//setup the pins/ inputs & outputs
void setup()
{
pinMode(redPin, OUTPUT);   // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}

// Main program, count to 763, a third of the way switch the incrementing of the LEDs
void loop()
{
i += 2;      // Increment counter
if (i < 255) // First phase of fades
{
redLEDValue -= 1; // Red down
greenLEDValue += 1; // Green up
blueLEDValue = 1; // Blue low
}
else if (i < 509) // Second phase of fades
{
redLEDValue = 1; // Red low
greenLEDValue -= 1; // Green down
blueLEDValue += 1; // Blue up
}
else if (i < 763) // Third phase of fades
{
redLEDValue += 1; // Red up
greenLEDValue = 1; // Green low
blueLEDValue -= 1; // Blue down
}
else // Re-set the counter, and start the fades again
{
i = 1;
}// analogWrite() expects 2 parameters, the object/pin and a value between 0 (off) and 255 (full on)
analogWrite(redPin,   redLEDValue);   // Write current values to LED pins
analogWrite(greenPin, greenLEDValue);
analogWrite(bluePin,  blueLEDValue);

delay(50); // Pause for xx milliseconds before resuming the loop
}

so that when i take away the analog input or apply a high to another input it will switch from the pot controlling the color to it just cycleing through the colors?
EDIT: ohhh or even better make so that when the pot is turned all the way up it starts the fader code… and also how would i need to change the fader code(second code) so that the counter is only counting when there is a high on one of the digital pins?.. like how would i make one of those pins an input and make the code only count when there is a high on it?