independent control of BRIGHTNES & COLOR of a RGB LEDstrip with 2 potentiometers

Hello guys. This is a personal project for which I’ve managed to solve 50% of the problem already. I’m sure that the remaining part is going to take most of you only a few minutes to solve, but due to my basic coding skills, it’s been quite a challenge for me. I’ve been stuck with this coding puzzle for the past 10-12 days, so I’ve finally decided to reach out for help. I will be using an ArduinoMega for now.

Tasks needed & method:
** The task should be accomplished with the use of ONLY TWO 10k potentiometers.**

-Task #1: By manipulating potentiometer #1, the user is able to select a HUE of color by manually cycling through all the hues of light in the visible color wheel. (This part of the code is provided below and it works perfectly for this task.)

-Task #2: By manipulating the 2nd potentiometer, the user is able to adjust the selected color’s overall BRIGHTNESS level, from 0% light-output to full 100% light-output. (This is what I can’t get to figure out :sob: )

Both variables (the hue of color) & (the total brightness of emitted light) need to behave independently of each other, but they need to be on the same Arduino sketch …so that I can put them in an ATtiny85 chip later in the future.

The use of two PWM modification variables at the same time for the same LED has got me really confused and frustrated. I know this is something easy to do with some sort of matrix or mathematical analysis function, but my coding knowledge is not yet at this level of expertise.

Below is the code that I’m using for the “Task #1”. It was designed by Julian White and uploaded to his blog, here. Now, I need to use the “potentiometer #2” sensor to control the LED’s brightness (my Task #2). Does anyone here has any simple idea on how to accomplish this final step?

Note: The code written by Julian White that I’m using here, was designed for a single RGB LED powered by the Arduino itself. I will end up using the code with short sections of a 12v RGB LED stip. I haven’t experimented with it yet because it is still in the mail; but since the control signals will be coming to them via the ArduinoMEGA’s PWM pins, I guess that the best way to power the strip is via 3 “L series MOSFET transistors”. One for each color, all 3 hooked up to a 9v battery ( instead of a 12v one because of portability), like described here. Does this external power method sounds right for my intended use? If not, are there any other simpler/better ways to power it with a 9v battery?

Thank you all very much for any help.

/* 
 * Arduino with Single Potentiometer and Common Anode RGB LED
 * Program: rgbled.ino
 * Author: Julian White
 * Created: 10/13/2012
 * Description: Use single 10K potentiometer to cycle through all
 * hues in the color wheel. The RGB LED is wired as common anode
 * (+5V). 330 ohm resistor wired in series with LED anode.
 */
 
int redPin = 9;
int greenPin = 11;
int bluePin = 10;
int potPin = A0;
int val = 0;

void setup()
{
    pinMode(redPin, OUTPUT);
    pinMode(greenPin, OUTPUT);
    pinMode(bluePin, OUTPUT);

    Serial.begin(9600);
}

void loop()
{
    //Read the potentiometer (color wheel).
    val = analogRead(potPin);
    Serial.println(val);
    
    //Red-orange-yellow-green spectrum
    if ( val <= 341 )
    {
        //We now know the val is between 0 and 341.
        //Map the val to 0-255 for the LED.
        val = map(val, 0, 341, 0, 255);
        
        //Higher val turns red down.
        //Lower val turns red up (common anode).
        analogWrite(redPin, val);
        
        //Write the complimentary amount to the green pin.
        //The mixed color values add up to 255.
        //This keeps the LED at constant brightness.
        analogWrite(greenPin, 255-val);
        
        //Turn blue pin off.
        analogWrite(bluePin, 255);
    }
    
    //Green-teal-blue spectrum
    else if ( val <= 683 )
    {
        //We now know the val is between 341 and 683.
        //Map the val to 0-255 for the LED.
        val = map(val, 341, 683, 0, 255);
        
        //Higher val turns green down.
        //Lower val turns green up (common anode).
        analogWrite(greenPin, val);
        
        //Write the complimentary amount to the blue pin.
        //The mixed color values add up to 255.
        //This keeps the LED at constant brightness.
        analogWrite(bluePin, 255-val);
        
        //Turn red pin off.
        analogWrite(redPin, 255);
    }
    
    //Blue-indigo-purple-red spectrum.
    else
    {
        //We now know the val is between 683 and 1023.
        //Map the val to 0-255 for the LED.
        val = map(val, 683, 1023, 0, 255);
        
        //Higher val turns blue down.
        //Lower val turns blue up (common anode).
        analogWrite(bluePin, val);

        //Write the complimentary amount to the red pin.
        //The mixed color values add up to 255.
        //This keeps the LED at constant brightness.
        analogWrite(redPin, 255-val);

        //Turn green pin off.
        analogWrite(greenPin, 255);
    }
    
    //Sampling rate.
    delay(20);
}

Google "HLS to RGB"

all 3 hooked up to a 9v battery

If you have a PP3 9V battery (small block with terminals on the top) in mind then it will probably not power the LEDs for long as its capacity is very low. For longer term power you should maybe think of using 6 AA batteries to provide the power.

1- wire 2 pots on A0 and A1 inputs, and have your code read the values.

2- if you go with (HSL), then use a saturation Sthat's fixed, possibly 1023, and then have A0 and A1 represent H and L, both likely have values less than 1024 like AWOL says.

3- delete the color cycling code you're using which is RGB and use the math that converts HSL to RGB, and write those values for each R,G,B LEDs.

Alternatively:

do the same thing to calculate R,G,B values, and use A1 as a multiplier.

float bright = A1/1024;

R = bright * R; G = bright *G; B = bright *B;

this will allow you to dim the LEDs though the hue will likely get screwed up.

UKHeliBob: If you have a PP3 9V battery (small block with terminals on the top) in mind then it will probably not power the LEDs for long as its capacity is very low. For longer term power you should maybe think of using 6 AA batteries to provide the power.

True! Even thou the application will be used just for a few minutes each time, in the long run for bigger applications with a longer power need, your suggestion is very valid. Thanks UKHeliBob! :)

AWOL: Google "HLS to RGB"

I thought that this was going to be a fairly easy solution, but it complicates this project even more to a level that i'm not good/comfortable at yet. I will keep it in mind for future simpler projects thou. Thanks a lot for the suggestion AWOL.

sonyhome: Alternatively:

do the same thing to calculate R,G,B values, and use A1 as a multiplier.

float bright = A1/1024;

R = bright * R; G = bright *G; B = bright *B;

this will allow you to dim the LEDs though the hue will likely get screwed up.

I'm currently reading about floats & multipliers to be able to figure something out using your last suggestion. I guess its the simpler and easiest approach to understand. Thank you! :D

THANKS EVERYONE FOR THE QUICK RESPONSES, and sorry for the delay. Now that my thesis is almost over, I have time to play with Arduino stuff once again...and to be more active in this community once again. I'm grateful for all of this advice and for this great group of creative individuals.

If anyone else can think of something else, please do let us all know. THANKS A LOT! :)