Skip to content

Instantly share code, notes, and snippets.

@jywarren
Created December 8, 2011 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jywarren/1447863 to your computer and use it in GitHub Desktop.
Save jywarren/1447863 to your computer and use it in GitHub Desktop.
HSV RGB LED from thermistor
#include <math.h>
double Thermister(int RawADC) {
double Temp;
Temp = log(((10240000/RawADC) - 10000));
Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp );
Temp = Temp - 273.15; // Convert Kelvin to Celcius
//Temp = (Temp * 9.0)/ 5.0 + 32.0; // Convert Celcius to Fahrenheit
return Temp;
}
void setup() {
Serial.begin(115200);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
}
void loop() {
//int temp(analogRead(0);
Serial.println(int(Thermister(analogRead(0)))); // display Fahrenheit
int hue = map(int(Thermister(analogRead(0))),-15,0,0,360);
analogWrite(9,map(int(Thermister(analogRead(0))),-15,0,0,1024)); //Red pin attached to 9
analogWrite(11,1024-map(int(Thermister(analogRead(0))),-15,0,0,1024)); //Red pin attached
//The Hue value will vary from 0 to 360, which represents degrees in the color wheel
// for(int hue=0;hue<360;hue++)
// {
// setLedColorHSV(hue,1,1); //We are using Saturation and Value constant at 1
// delay(10); //each color will be shown for 10 milliseconds
// }
}
//Convert a given HSV (Hue Saturation Value) to RGB(Red Green Blue) and set the led to the color
// h is hue value, integer between 0 and 360
// s is saturation value, double between 0 and 1
// v is value, double between 0 and 1
//http://splinter.com.au/blog/?p=29
void setLedColorHSV(int h, double s, double v) {
//this is the algorithm to convert from RGB to HSV
double r=0;
double g=0;
double b=0;
double hf=h/60.0;
int i=(int)floor(h/60.0);
double f = h/60.0 - i;
double pv = v * (1 - s);
double qv = v * (1 - s*f);
double tv = v * (1 - s * (1 - f));
switch (i)
{
case 0: //rojo dominante
r = v;
g = tv;
b = pv;
break;
case 1: //verde
r = qv;
g = v;
b = pv;
break;
case 2:
r = pv;
g = v;
b = tv;
break;
case 3: //azul
r = pv;
g = qv;
b = v;
break;
case 4:
r = tv;
g = pv;
b = v;
break;
case 5: //rojo
r = v;
g = pv;
b = qv;
break;
}
//set each component to a integer value between 0 and 255
int red=constrain((int)255*r,0,255);
int green=constrain((int)255*g,0,255);
int blue=constrain((int)255*b,0,255);
setLedColor(red,green,blue);
}
//Sets the current color for the RGB LED
void setLedColor(int red, int green, int blue) {
//Note that we are reducing 1/4 the intensity for the green and blue components because
// the red one is too dim on my LED. You may want to adjust that.
analogWrite(9,red); //Red pin attached to 9
analogWrite(10,green/3); //Red pin attached to 9
analogWrite(11,blue/3); //Red pin attached to 9
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment