Hi
I want to use 2 pots to control an array of RGB LEDs. One to control the colour, and another to control the brightness. All the LEDs will be the same colour at any one time.
First I am looking at the Brightness pot. I have used this code with a single colour LED OK, but I need it to output the pot value to 3 different PWN pins together. Can you help me how to do this?
Cheers
Rob
/*
Use a knob to fade an LED.
This super simple script allows you to use a potentiometer (knob) to control the brightness of an LED.
We use analogRead() to get the value from the control knob.
We use analogWrite() to set the brightness of the LED.
Important note: the analog inputs on the Arduino (Duemillanove +) have 10 bit resolution, while the digital pins (PWM) are only 8 bit resolution. So analogRead() will be 0 to 1023, while analogWrite() will be 0 to 254.
The circuit:
* LED attached from digital pin 9 to ground.
* Control knob (potentiometer) with the center leg connected to analog pin 0, and the outer two legs connected to ground and source voltage respectivly.
created 2010
by Andrew Frueh
I built this code starting with "Fading" from Arduino examples - 1 Nov 2008, By David A. Mellis
http://arduino.cc/en/Tutorial/Fading
*/
// these constant variables store the pin numbers
const int ledPin = 3;
const int knobPin = 0;
// these variables store the values for the knob and LED level
int knobValue, fadeValue;
void setup() {
// initialize the serial port
Serial.begin(9600);
}
void loop() {
// read the value from the input
knobValue = analogRead(knobPin);
// remap the values from 10 bit input to 8 bit output
fadeValue = map(knobValue, 0, 1023, 0 , 254);
// use the input value to fade the led
analogWrite(ledPin, fadeValue);
// print the input value to the serial port for debugging
Serial.println(fadeValue);
}
Just for clarity, I need one pwm pin for each colour so that I can later change them with a second pot but the brightness will be synced across them.
Sorry, I figured it out...
/*
Use a knob to fade an LED.
This super simple script allows you to use a potentiometer (knob) to control the brightness of an LED.
We use analogRead() to get the value from the control knob.
We use analogWrite() to set the brightness of the LED.
Important note: the analog inputs on the Arduino (Duemillanove +) have 10 bit resolution, while the digital pins (PWM) are only 8 bit resolution. So analogRead() will be 0 to 1023, while analogWrite() will be 0 to 254.
The circuit:
* LED attached from digital pin 9 to ground.
* Control knob (potentiometer) with the center leg connected to analog pin 0, and the outer two legs connected to ground and source voltage respectivly.
created 2010
by Andrew Frueh
I built this code starting with "Fading" from Arduino examples - 1 Nov 2008, By David A. Mellis
http://arduino.cc/en/Tutorial/Fading
*/
// these constant variables store the pin numbers
const int rledPin = 3;
const int gledPin = 5;
const int bledPin = 11;
// 3=red 5=green 11=blue
const int knobPin = 0;
// these variables store the values for the knob and LED level
int knobValue, fadeValue;
void setup() {
// initialize the serial port
Serial.begin(9600);
}
void loop() {
// read the value from the input
knobValue = analogRead(knobPin);
// remap the values from 10 bit input to 8 bit output
fadeValue = map(knobValue, 0, 1023, 0 , 254);
// use the input value to fade the led
analogWrite(rledPin, fadeValue);
analogWrite(gledPin, fadeValue);
analogWrite(bledPin, fadeValue);
// print the input value to the serial port for debugging
Serial.println(fadeValue);
}
If one analogWrite outputs to one LED then three analogWrites ....
Hint enough?
Edit - ah, you just worked it out yourself, too.
Want a hint on the "colour"/Hue ?

well I got this working on its own to control the colour with a pot
int potpin = 1; // Switch connected to digital pin 1
int rpin = 3;
int gpin = 5;
int bpin = 11;
float h;
int h_int;
int r=0, g=0, b=0;
int val=0;
void h2rgb(float h, int &R, int &G, int &B);
void setup() // run once, when the sketch starts
{
Serial.begin(9600); // set up Serial library at 9600 bps
}
void loop() // run over and over again
{
val=analogRead(potpin); // Read the pin and display the value
//Serial.println(val);
h = ((float)val)/1024;
h_int = (int) 360*h;
h2rgb(h,r,g,b);
Serial.print("Potentiometer value: ");
Serial.print(val);
Serial.print(" = Hue of ");
Serial.print(h_int);
Serial.print("degrees. In RGB this is: ");
Serial.print(r);
Serial.print(" ");
Serial.print(g);
Serial.print(" ");
Serial.println(b);
analogWrite(rpin, r);
analogWrite(gpin, g);
analogWrite(bpin, b);
}
void h2rgb(float H, int& R, int& G, int& B) {
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;
}
}
So I think I just need to work out how to integrate the 2? or is there an easier way?
You're doing fine.
If it works, it is correct. There are lots of way of skinning that cat.
I'd pass both pot values to the h2rgb routine and "scale" the final r, g, b by that, which makes it "hv2rgb"
.
Thanks very much your help
Got it sorted.
In the end I realised that the HSV colour model facilitates for a brightness level so the solution was quite neat.
const int hpotpin = 1; // Switch connected to digital pin 1
const int fpotpin = 0;
int rpin = 3;
int gpin = 6;
int bpin = 11;
float h;
int h_int;
int r=0, g=0, b=0;
int hval=0;
// these variables store the values for the fade knob and LED level
int fpotpinValue, fval;
void h2rgb(float h, int &r, int &g, int &b);
void setup() // run once, when the sketch starts
{
Serial.begin(9600); // set up Serial library at 9600 bps
}
void loop() // run over and over again
{
hval=analogRead(hpotpin);
fpotpinValue=analogRead(fpotpin);
fval = map(fpotpinValue, 0, 1023, 0 , 255);
h = ((float)hval)/1024;
h2rgb(h,r,g,b);
analogWrite(rpin, r);
analogWrite(gpin, g);
analogWrite(bpin, b);
}
void h2rgb(float H, int &R, int&G, int&B) {
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) * fval; //RGB results = 0 ÷ fade value from pot
G = (1-var_g) * fval;
B = (1-var_b) * fval;
}
}
credit to this thread for the original script at http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1207331496
I'm going to keep my project updates in this thread - http://arduino.cc/forum/index.php/topic,58696.0.html - now if you want to follow it 