im trying to make an rgb led light up in different colors to based on the analog input, for instance a "potentiometer" or equivelent like the lm324 controlling the input causes the input to go higher the red turns on then green and then blue progressively, i have some code worked up but am willing to work from scratch again, im working with the lm324 op-amp and an arduino w/328

sort by: active | newest | oldest
5 years ago
My method of choice is to build an array of rgb values, say 255 to go the full rainbow spectrum. This is the hardest part...

then you analogread the potentiometer, and map it from 0-1024 down to 0-255, and set the rgb output pins to what the array says those values should be.

The reason I pre-build the rgb array is because there is a lot of floating point math to do the first calculations but after it's built you can just look up those values. The downside is it takes up 255*3 bytes of ram, a precious commodity in uC production.

I can share my example code when I get my laptop from home later.
redd.the.cat (author)  frollard5 years ago
^w^ that might help me out tanx
5 years ago
Oops, found a bug, reposting (hence multiple replies)...

//*******Hue to RGB functions by Frollard
//*******May 2012
//*******CC NC SA
//*******Converts a Hue 0-1023 to R,G,B values 0-1023 to form a looping 'rainbow' spectrum
//*******Ideal to convert a pot analog input to a rainbow RGB output.
//*******Does no error checking for bad input values (outside 0-1023), which should
//*******never happen because numbers are sourced from the analogRead function.

int redPin = 3; //output red pwm pin
int greenPin = 6; //output green pwm pin
int bluePin = 9; //output blue pwm pin
int potPin = 0; //analog pin zero
const int hueArraySize = 16;
byte arrayPosition = 0; //array read/write marker
unsigned int averageHue = 0; //average of the array
void setup(){
//Serial.begin(9600);
pinMode(redPin,OUTPUT);
pinMode(greenPin,OUTPUT);
pinMode(bluePin,OUTPUT);
}

void loop(){
inputHue = analogRead(potPin); //check the value of the pot
arrayPosition++;
if (arrayPosition >= hueArraySize) { //looping array write marker
arrayPosition = 0;
}
hueArray[arrayPosition] = inputHue;
averageHue = 0;
for(int x = 0; x < hueArraySize; x++){
averageHue += hueArray[x];
}
averageHue = averageHue / hueArraySize;
//Serial.print("averageHue:");  //debug
//Serial.print(averageHue, DEC);
//Serial.print("; ");

analogWrite(redPin,map(redFromHue(averageHue),0,1023,0,255)); //convert the hue to red, and output
analogWrite(greenPin,map(greenFromHue(averageHue),0,1023,0,255)); //convert the hue to green, and output
analogWrite(bluePin,map(blueFromHue(averageHue),0,1023,0,255)); //convert the hue to blue, and output

delay(10);

}//end main loop

int redFromHue(unsigned int inputHue) //converts a hue from 0-1023 to its corresponding red value
{
int R = 1023; //default value
if (inputHue > 341 && inputHue <= 512){
R = 1023-map((inputHue-342),0,170,0,1023);
}//end if
if (inputHue >512 && inputHue < 768){
R = 0;
}//end if
if (inputHue >768){
R = (inputHue - 768) *4;
}//end if
//Serial.print(R,DEC);   //debug
//Serial.print(",");
return R;
} //end redFromHue

int greenFromHue(unsigned int inputHue) //converts a hue from 0-1023 to its corresponding green value
{
int G = 0; //default value
if (inputHue <=341){
G = inputHue * 3;
} //end if
if (inputHue > 341 && inputHue <= 896)
{
G = 1023-map((inputHue-342),0,554,0,1023);
}//end if
//Serial.print(G,DEC);  //debug
//Serial.print(",");
return G;
}//end greenFromHue

int blueFromHue(unsigned int inputHue) //converts a hue from 0-1023 to its corresponding blue value
{
signed int correctedHue = inputHue - 768;
signed int B = 768-(abs(correctedHue)*3);
if (B < 0)
{
B = 0;
} //end if
//Serial.print(B,DEC);  //debug
//Serial.println(" ");

return B;
} //

5 years ago

I uploaded the pde file here in case the formatting breaks it.
redd.the.cat (author)  frollard5 years ago
^w^ omg omg omg its perfect thank you soooo much ive been racking my brain for months and u swoop in and save me in the knick of time <3 ur the bast
steveastrouk5 years ago
What's wrong with your code ?
redd.the.cat (author)  steveastrouk5 years ago
its incomplete, heres what i got so far

int lm324 = A0; //input where lm324 is connected
int red = 3; //red led
int grn = 6; //green led
int blu = 9; //blue led
int sig; //variable

void setup() {

}

void loop()
{
sig = analogRead(lm324); //apply input to variable
sig = map(sig, 0, 1023, 0, 100); //map variable to smaller number

}
redd.the.cat (author)  redd.the.cat5 years ago
i know that i need some bunch of "if" statements i just dont know how to use them