Pages: [1]   Go Down
Author Topic: Potentiometer controlled RGB fade without using serial  (Read 913 times)
0 Members and 1 Guest are viewing this topic.
Central Wisconsin
Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

is there a way to use a potentiometer to control an RBG fade thought Arduino without using and type of serial command. the code below only semi works. how can i chage the code to fade smoothly through the whole color spectrum?


Code:

// BlinkM / BlinkM MinM pins
const int redPin = 3; 
const int grnPin = 4;
const int bluPin = 1; 
const int sclPin = 2; 

const int knobPin  = sclPin;

int pos;


void setup() {
  pinMode(redPin, OUTPUT);
  pinMode(grnPin, OUTPUT);
  pinMode(bluPin, OUTPUT);


  pinMode(knobPin, INPUT);

}

void loop() {

  pos = analogRead( knobPin );

 
  int redValue = constrain(map(pos, 0, 512, 255, 0),0,255);

  int greenValue = constrain(map(pos, 0, 512, 0, 255),0,255)-constrain(map(pos, 512, 1023, 0, 255),0,255); 

  int blueValue = constrain(map(pos, 512, 1023, 0, 255),0,255);


 

  analogWrite(redPin, redValue);

  analogWrite(grnPin, greenValue);

  analogWrite(bluPin, blueValue);
}
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 216
Posts: 13673
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
is there a way to use a potentiometer to control an RBG fade thought Arduino without using and type of serial command. the code below only semi works. how can i chage the code to fade smoothly through the whole color spectrum?

Yes, possible but not trivial and not 100% . There is a problem as the RGB color space is not "the whole colorspectrum".  In fact the colorspectrum is 3 dimensional and with your potmeter you can only control one dimension. To control the color along the rainbow (spectrum) you need the LAB color space - http://en.wikipedia.org/wiki/Lab_color_space - or the HSL colorspace  and convert it to RGB values. BUt this RGB is device dependant.

That said you can approximate the needed conversion quite well, for some formulas see  - http://www.easyrgb.com/index.php?X=MATH -

Try the HSL to RGB first




Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Central Wisconsin
Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I dont even know where to start with writing that into my program...
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26248
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm pretty sure there is example code for HSL to RGB somewhere; have you tried the Playground?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 216
Posts: 13673
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


found this one - http://www.dipzo.com/wordpress/?p=50 - need a rewrite a bit to read from potmeter iso rotary encoder

Seems to work quite well
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Central Wisconsin
Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
found this one - http://www.dipzo.com/wordpress/?p=50 - need a rewrite a bit to read from potmeter iso rotary encoder

Seems to work quite well

The problem i am having is that anything that has "serial" in it, i will always get an error.
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 144
Posts: 5987
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What error are you getting when you try the serial interface? Like in this thread?
http://arduino.cc/forum/index.php/topic,67519.0.html
If so, reply#10 in that thread had the solution for me.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've done something similar when designing a character creator for a game - a slider that selects hue.  I split the bar up into red->red+green, red+green->green, green->green+blue, green+blue->blue, blue->red+blue, red+blue->red and made each section linear interpolate.  Worked quite nicely.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26248
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Have you tried spelling it 'Serial'?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1]   Go Up
Jump to: