Go Down

Topic: arduino uno rgb led pot control help (Read 2562 times) previous topic - next topic

koteman

Dec 29, 2012, 02:18 pm Last Edit: Dec 29, 2012, 02:27 pm by koteman Reason: 1
i bought my first arduino uno the other day so i'm a noob. I have watched and read tutorials for probably about 12 hours worth so far and ill read another 10 hours today at work for a few of the projects i want to accomplish. first i'm trying to make a RGB led controller for my car. it will have a few RGB LEDs but they will all be the same so i know i only need 3 PWM pins to control them with transistors and i want to have a pot on A0 control the whole color spectrum plus plus off and white at the 2 extremes of the pot. i have the hard ware part down as i'm good with hardware i'm just new to software programming so i'm having difficulty learning this code. any guidance on which libraries i should use? i was thinking the map function for the pot and using say 0-50 for black 50-1000 for colors and 1000-1023 for white.

after looking for similar projects im going to base it off of this.
http://www.skylark-software.com/2011/01/arduino-notebook-rgb-led-and-color_21.html

Thankyou,
      Koteman

fungus


i only need 3 PWM pins to control them with transistors and i want to have a pot on A0 control the whole color spectrum plus plus off and white at the 2 extremes of the pot.


Sounds good.


i have the hard ware part down as i'm good with hardware i'm just new to software programming so i'm having difficulty learning this code. any guidance on which libraries i should use? i was thinking the map function for the pot and using say 0-50 for black 50-1000 for colors and 1000-1023 for white.

after looking for similar projects im going to base it off of this.
http://www.skylark-software.com/2011/01/arduino-notebook-rgb-led-and-color_21.html


You need a hue -> RGB conversion function for your color.

https://en.wikipedia.org/wiki/Hue

https://en.wikipedia.org/wiki/HSL_and_HSV#From_HSV

Hue will be calculated by mapping the values 50-1000 to the range 0-360. "Saturation" and "Value" will both be 1.0.

The code on that page basically has all you need, you just need to adapt it to your project.

Advanced Arduino

koteman

so i have to code from that site working and im trying to get

if (inputValue =< 23)
ledPins LOW;

in there to turn the pins off under 24 analog read but i keep getting errors
rgb.ino: In function 'byte* hsvToRgb(int, double, double)':
rgb:89: error: 'inputValue' was not declared in this scope
rgb:90: error: expected `;' before numeric constant

what area should i put the if statement?

koteman

well for now i simplified it to 7 colors and off and got this working
Code: [Select]
const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
const int redPin = 11;       // pin that the red LED is attached to
const int bluePin = 9;       // pin that the red LED is attached to
const int greenPin = 10;     // pin that the red LED is attached to


int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output for color

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
  // initialize the LED pins as an output:
  pinMode(redPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  pinMode(greenPin, OUTPUT);
}

void loop() {
  // read the analog in value:
  int sensorValue = analogRead(analogInPin);           
  // map it to the range of 8 colors:
  int outputValue = map(sensorValue, 0, 1023, 0, 7); 
 
  switch (outputValue) {
  //off
    case 0:
   digitalWrite(redPin, HIGH);
   digitalWrite(greenPin, HIGH);
   digitalWrite(bluePin, HIGH);
    break;
//red   
  case 1:
   digitalWrite(redPin, LOW);
   digitalWrite(greenPin, HIGH);
   digitalWrite(bluePin, HIGH);
    break;
//yellow   
  case 2:
   digitalWrite(redPin, LOW);
   digitalWrite(greenPin, LOW);
   digitalWrite(bluePin, HIGH);
    break;
//green   
  case 3:
   digitalWrite(redPin, HIGH);
   digitalWrite(greenPin, LOW);
   digitalWrite(bluePin, HIGH);
    break;
//aqua
  case 4:
   digitalWrite(redPin, HIGH);
   digitalWrite(greenPin, LOW);
   digitalWrite(bluePin, LOW);
    break;
//blue   
  case 5:
   digitalWrite(redPin, HIGH);
   digitalWrite(greenPin, HIGH);
   digitalWrite(bluePin, LOW);
    break;
//purple   
  case 6:
   digitalWrite(redPin, LOW);
   digitalWrite(greenPin, HIGH);
   digitalWrite(bluePin, LOW);
    break;
//white   
  case 7:
   digitalWrite(redPin, LOW);
   digitalWrite(greenPin, LOW);
   digitalWrite(bluePin, LOW);
    break;
  }
  // print the results to the serial monitor:
  Serial.print("sensor = " );                       
  Serial.print(sensorValue);     
  Serial.print("\t output = ");     
  Serial.println(outputValue);   
 
  // wait 2 milliseconds before the next loop
  // for the analog-to-digital converter to settle
  // after the last reading:
  delay(10);                     
}

fungus

Try something like this for the hue->RGB conversion:

Code: [Select]

...
  int outputValue = map(sensorValue, 0, 1023, 0, 1536); 
  int v = outputValue&255;

  int r=0,g=0,b=0;
  switch (outputValue/256) {
    case 0:    r = 255;    g = v;      b = 0;    break;  // red->yellow
    case 1:    r = 255-v;  g = 255;    b = 0;    break;  // yellow->green
    case 2:    r = 0;      g = 255;    b = v;    break;  // green->cyan
    case 3:    r = 0;      g = 255-v;  b = 255;  break;  // cyan->blue
    case 4:    r = v;      g = 0;      b = 255;  break;  // blue->magenta
    case 5:    r = 255;    g = 0;      b = 255-v;break;  // magenta->red
  }
  analogWrite(redPin,red); 
  analogWrite(greenPin,green); 
  analogWrite(bluePin,blue); 

...




nb. I haven't tested it, I just typed it in off the top of my head... :)
Advanced Arduino

Go Up