Skip to content

Instantly share code, notes, and snippets.

@aaronparsekian
Created October 21, 2015 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronparsekian/f79ec109f9da6a151624 to your computer and use it in GitHub Desktop.
Save aaronparsekian/f79ec109f9da6a151624 to your computer and use it in GitHub Desktop.
int ledPins[] = {3, 5, 6 }; //Red Green Blue pins going out to the LED from the arduino
int proxPin = A0; //Proximity sensor - increases voltage as your hand gets closer - controls HUE
int potPin = A1; //potentiometer for brightness control
int maxVal = 500;
int minVal = 180;
int latestVal;
int previousVal;
int bottomRange = 180;
double bright;
void setup()
{
Serial.begin(9600);
pinMode(proxPin, INPUT);
pinMode(potPin, INPUT);
for (int i = 0; i < 3; i++)
pinMode(ledPins[i], OUTPUT);
}
void loop()
{
double tempBright = analogRead(potPin);
bright = map(tempBright, 0, 1023, 0, 100) / 100.0; //0 to 1023 gets mapped to 0.00 to 1.00
int aux = analogRead(proxPin);
Serial.println(aux);
if (aux > bottomRange) {
latestVal = aux;
delay(100);
}
if (latestVal > minVal) {
minVal = latestVal;
delay(100);
}
if (latestVal < maxVal) {
maxVal = latestVal;
delay(100);
}
int hueValue = map(latestVal, 180, 480, 0, 360); //Proximity sensor range (180 to 480) with a hand, mapped to 0 to 360 (hue)
delay(100);
setHueValue(hueValue);
}
void setHueValue(int hueValue)
{
setColor(ledPins, hsvToRgb(hueValue, 1, 1));
}
void setColor(int* led, const byte* color)
{
for (int i = 0; i < 3; i++)
analogWrite( led[i], color[i]);
}
byte rgb[3];
byte * hsvToRgb(int h, double s, double v)
{
v = bright;
// Make sure our arguments stay in-range
h = max(0, min(360, h));
s = max(0, min(1.0, s));
v = max(0, min(1.0, v));
if (s == 0)
{
// Achromatic (grey)
rgb[0] = rgb[1] = rgb[2] = round(v * 255);
return rgb;
}
double hs = h / 60.0; // sector 0 to 5
int i = floor(hs);
double f = hs - i; // factorial part of h
double p = v * (1 - s);
double q = v * (1 - s * f);
double t = v * (1 - s * (1 - f));
double r, g, b;
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;
}
rgb[0] = round(r * 255.0);
rgb[1] = round(g * 255.0);
rgb[2] = round(b * 255.0);
return rgb;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment