/*
* The Rainbow-Strobe
*
* Using the three potentiometers it is possible to
* 1. change the color in the rainbow spectrum (red-orange-yellow-green-blue-purple-red)
* 2. modify the brightness
* 3. control the strobe effect
*
* Theory and Practise of Tangible User Interfaces
* Lab Assignment 3
*
* Martin Kiechle
* 9/20/2011
*/
/*
* variables and allocations
*/
// potentiometer pins
int pot1Pin = 0;
int pot2Pin = 1;
int pot3Pin = 2;
// input variables
int pot1Val = 0; // variable to store the value coming from pot 1
int pot2Val = 0; // variable to store the value coming from pot 2
int pot3Val = 0;
// LED pins
int led1Pin = 9;
int led2Pin = 10;
int led3Pin = 11;
// HSV variables
float hue, val;
// RGB variables
int r, g, b;
// debug variable
int DEBUG = 0;
void setup() {
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(led3Pin, OUTPUT);
Serial.begin(9600);
}
void loop() {
// read analog values from potentiometers
pot1Val = analogRead(pot1Pin);
pot2Val = analogRead(pot2Pin);
pot3Val = analogRead(pot3Pin);
// scale down values for HSV color space
hue = (float)pot1Val * 0.3515625; // color in spectrum => HSV_hue
val = (float)pot2Val / 1024; // brightness => HSV_value
// HSV to RGB color space conversion
HSVtoRGB(&r, &g, &b, hue, val);
// deub information
if(DEBUG){
Serial.print("Hue: ");
Serial.println(hue, DEC);
Serial.print("Val: ");
Serial.println(val, DEC);
Serial.print("Strobe: ");
Serial.println(pot3Val, DEC);
Serial.print("R: ");
Serial.println(r, DEC);
Serial.print("G: ");
Serial.println(g, DEC);
Serial.print("B: ");
Serial.println(b, DEC);
}
// write the RGB values to PWM outputs
analogWrite(led1Pin, r);
analogWrite(led2Pin, g);
analogWrite(led3Pin, b);
// delay program execution to create strobe effect
delay(pot3Val);
// turn off all PWM outputs
analogWrite(led1Pin, 0);
analogWrite(led2Pin, 0);
analogWrite(led3Pin, 0);
// second program delay before switching LEDs back on
delay(pot3Val);
}
// color space conversion HSV to RGB --> http://en.wikipedia.org/wiki/HSL_and_HSV
void HSVtoRGB(int *R, int *G, int *B, float h, float val){
float s=1;
float v=val;
float r,g,b;
int i;
float f, p, q, t;
if( s == 0 )
{
r = g = b = v;
return;
}
h /= 60;
i = floor( h );
f = h - i;
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: r = v; g = p; b = q; break;
}
*R = r * 255;
*G = g * 255;
*B = b * 255;
}