Announcements

November 24, 2007
Reading for November 27th, are now posted. Enjoy!

October 2, 2007
To upload your thoughtless acts, create a new assignment page like any other lab. You'll see "Thoughtless Acts" listed as one of the assignment options.

May 24, 2008
This site has been archived and is no longer editable. Stay tuned for the next version, coming in the fall!


Color Space Translation

Project Members: 
Andrew McDiarmid

Description

I wrote/modified three programs for adruino boards with three potentiometers and three LEDs. The first assigns one dimension of the CMY color space to each potentiometer, illuminating the LEDs in the appropriate RGB values depending on what is dialed in for cyan, magenta, and yellow. The second similarly translates from the HSV color space to the RGB color space. The third modifies the second, such that value is held constant at 255, and the thrid potentiometer controls the blink rate.

Components Used

Three LEDs

Three 220-ohm resistors

Three 100k-ohm potentiometers

Arduino board 

Metal cone top to lava lamp (diffuser) 

 

CMY to RGB translation

/*
* Three pots control CMY color space dimensions
* Andrew McDiarmid
*
* modified version of one pot fades one led
* modified version of AnalogInput
* by DojoDave <http://www.0j0.org>
* http://www.arduino.cc/en/Tutorial/AnalogInput
*/
int cyanPotPin = 2; // select the input pin for the potentiometer
int magentaPotPin = 1;
int yellowPotPin = 0;

int redLedPin = 9; // select the pin for the LED
int greenLedPin = 10;
int blueLedPin = 11;

int cyanVal = 0; // variable to store the value coming from the sensor
int magentaVal = 0;
int yellowVal = 0;

void setup() {
    Serial.begin(9600);
}
void loop() {
    cyanVal = analogRead(cyanPotPin); // read the value from the sensor,                                                          between 0 - 1024
    magentaVal = analogRead(magentaPotPin);
    yellowVal = analogRead(yellowPotPin);

    Serial.print("cyan: ");
    Serial.println(cyanVal);
    Serial.print("magenta: ");
    Serial.println(magentaVal);
    Serial.print("yellow: ");
    Serial.println(yellowVal);

    analogWrite(redLedPin, (255 - (cyanVal/4))); // analogWrite can be                                                                         between 0-255
    analogWrite(greenLedPin, (255 - (magentaVal/4)));
    analogWrite(blueLedPin, (255 - (yellowVal/4)));
}

HSV to RGB translation

/*
* Three pots control hue, saturation, and value
* Andrew McDiarmid
*
* modified version of one pot fades one led
* modified version of AnalogInput
* by DojoDave <http://www.0j0.org>
* http://www.arduino.cc/en/Tutorial/AnalogInput
*/
boolean DEBUG = 0;

int huePotPin = 2; // select the input pins for the potentiometers
int saturationPotPin = 1;
int valuePotPin = 0;

int redLedPin = 9; // select the pins for the LEDs
int greenLedPin = 10;
int blueLedPin = 11;

int hueVal = 0; // variables to store the value coming from the sensor
int saturationVal = 0;
int valueVal = 0;

float hue = 0; //variables for normalized HSV values, passed to HSVtoRGB function
float saturation = 0;
float value = 0;

int r = 0; //variables for HSV conversion to adjust
int g = 0;
int b = 0;

float gCorrection = 225.0/255.0; //values to correct for imbalance                                                             between the LEDs
float bCorrection = 127.0/255.0;

void setup() {
    Serial.begin(9600);
}

void HSVtoRGB(float h, float s, float v ) //modified from                                                                                     http://www.cs.rit.edu/~ncs/color/t_convert.html
{
    int i;
    float f, p, q, t;

    if( s == 0 ) { // achromatic (grey)
        r = g = b = v;
        return;
    }

    h /= 60; // sector 0 to 5
    i = (int) h;
    f = h - i; // factorial part of h
    p = v * ( 1 - s );
    q = v * ( 1 - s * f );
    t = v * ( 1 - s * ( 1 - f ) );
   
    switch( i ) {
        case 0:
        r = v;
        g = t;
        b = p;
        break;
        case 1:
        r = q;
        g = v;
        b = p;
        break;
        case 2:
        r = p;
        g = v;
        b = t;
        break;
        case 3:
        r = p;
        g = q;
        b = v;
        break;
        case 4:
        r = t;
        g = p;
        b = v;
        break;
        default: // case 5:
        r = v;
        g = p;
        b = q;
        break;
    }
}

void loop() {
    hueVal = analogRead(huePotPin); // read the value from the sensor,                                                             between 0 - 1023
    hue = ((float)hueVal) / (1023.0/359.0); //limits hue to 0 - 359
    saturationVal = analogRead(saturationPotPin);
    saturation = ((float)saturationVal / 1023.0); //limits saturation to 0 - 1
    valueVal = analogRead(valuePotPin);
    value = valueVal / 4; //limtis value to 0 - 255
   
    HSVtoRGB(hue, saturation, value);
    analogWrite(redLedPin, r);
    analogWrite(greenLedPin, (int)((float)g * gCorrection));
    analogWrite(blueLedPin, (int)((float)b * bCorrection));
   
    if (DEBUG){
        Serial.print("HSV= ");
        Serial.print((int)hue);
        Serial.print(" ");
        Serial.print((int)(100 * saturation));
        Serial.print("% ");
        Serial.println((int)value);
        Serial.print("RGB= ");
        Serial.print(r);
        Serial.print(" ");
        Serial.print(g);
        Serial.print(" ");
        Serial.println(b);
        Serial.println("----------");
    }
}

HS (constant V) to RGB, plus blink rate

/*
* Three pots control hue, saturation, and blink rate (value = 255)
* Andrew McDiarmid
*
* modified version of one pot fades one led
* modified version of AnalogInput
* by DojoDave <http://www.0j0.org>
* http://www.arduino.cc/en/Tutorial/AnalogInput
*/
boolean DEBUG = 0;

int huePotPin = 2; // select the input pins for the potentiometers
int saturationPotPin = 1;
int blinkPotPin = 0;

int redLedPin = 9; // select the pins for the LEDs
int greenLedPin = 10;
int blueLedPin = 11;

int hueVal = 0; // variables to store the value coming from the sensor
int saturationVal = 0;

int blinkRate = 0; //dealay time for controlling blinking (ms)

float hue = 0; //variables for normalized HSV values, passed to HSVtoRGB                         function
float saturation = 0;

int r = 0; //variables for HSV conversion to adjust
int g = 0;
int b = 0;

float gCorrection = 225.0/255.0; //values to correct for imbalance                                                             between the LEDs
float bCorrection = 127.0/255.0;

void setup() {
    Serial.begin(9600);
}

void HSVtoRGB(float h, float s, float v ) //modified from                                                                http://www.cs.rit.edu/~ncs/color/t_convert.html
{
    int i;
    float f, p, q, t;

    if( s == 0 ) { // achromatic (grey)
        r = g = b = v;
        return;
    }

    h /= 60; // sector 0 to 5
    i = (int) h;
    f = h - i; // factorial part of h
    p = v * ( 1 - s );
    q = v * ( 1 - s * f );
    t = v * ( 1 - s * ( 1 - f ) );
   
    switch( i ) {
        case 0:
        r = v;
        g = t;
        b = p;
        break;
        case 1:
        r = q;
        g = v;
        b = p;
        break;
        case 2:
        r = p;
        g = v;
        b = t;
        break;
        case 3:
        r = p;
        g = q;
        b = v;
        break;
        case 4:
        r = t;
        g = p;
        b = v;
        break;
        default: // case 5:
        r = v;
        g = p;
        b = q;
        break;
    }
}

void loop() {
    hueVal = analogRead(huePotPin); // read the value from the sensor,                                                                     between 0 - 1023
    hue = ((float)hueVal) / (1023.0/359.0); //limits hue to 0 - 359
    saturationVal = analogRead(saturationPotPin);
    saturation = ((float)saturationVal / 1023.0); //limits saturation to 0 - 1
    blinkRate = analogRead(blinkPotPin);
   
    HSVtoRGB(hue, saturation, 255.0);
    analogWrite(redLedPin, r);
    analogWrite(greenLedPin, (int)((float)g * gCorrection));
    analogWrite(blueLedPin, (int)((float)b * bCorrection));
   
    delay(blinkRate);
    analogWrite(redLedPin, 0);
    analogWrite(greenLedPin, 0);
    analogWrite(blueLedPin, 0);
    delay(blinkRate);

    if (DEBUG){
        Serial.print("HSV= ");
        Serial.print((int)hue);
        Serial.print(" ");
        Serial.print((int)(100 * saturation));
        Serial.print("% ");
        Serial.println("255");
        Serial.print("RGB= ");
        Serial.print(r);
        Serial.print(" ");
        Serial.print(g);
        Serial.print(" ");
        Serial.println(b);
        Serial.println("----------");
    }
}


Comments

nice job! three programs?

nice job! three programs? you went all out on this one, didn't ya?


Powered by Drupal - Design by Artinet