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