Skip to content

Instantly share code, notes, and snippets.

@ekampf
Created November 30, 2013 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ekampf/7721226 to your computer and use it in GitHub Desktop.
Save ekampf/7721226 to your computer and use it in GitHub Desktop.
Code to convert temperature from Arduino's temperature sensor into heatmap RGB
const int rLED = 11;
const int gLED = 9;
const int bLED = 10;
const int sensor = A0;
const float minTemp = 10.0;
const float maxTemp = 50.0;
int redV = 0;
int greenV = 0;
int blueV = 0;
void setup() {
Serial.begin(9600);
pinMode(rLED, OUTPUT);
pinMode(gLED, OUTPUT);
pinMode(bLED, OUTPUT);
}
void loop() {
int sensorVal = analogRead(sensor);
Serial.print("Sensor value: "); Serial.println(sensorVal);
float voltage = (sensorVal/1024.0) * 5.0;
Serial.print("Voltage value: "); Serial.println(voltage);
float temperature = (voltage - 0.5) * 100;
Serial.print("Temperature is: ");
Serial.print(temperature);
Serial.println(" degrees celsius");
float wavelength = wavelength_for_point(temperature, minTemp, maxTemp);
Serial.print("wavelength is "); Serial.println(wavelength);
wavelength_to_rgb(wavelength, redV, greenV, blueV);
Serial.print("R: "); Serial.print(redV);
Serial.print(" G: "); Serial.print(greenV);
Serial.print(" B: "); Serial.println(blueV);
analogWrite(rLED, redV);
analogWrite(gLED, greenV);
analogWrite(bLED, blueV);
Serial.println("");
delay(20);
}
float wavelength_for_point(float v, float minValue, float maxValue) {
const float minVisibleWaveLength = 350.0;
const float maxVisibleWaveLength = 650.0;
//Convert data value in the range of MinValues..MaxValues to the
//range 350..650
float range = maxValue - minValue;
float result = (v - minValue) / range;
result = result * (maxVisibleWaveLength - minVisibleWaveLength) + minVisibleWaveLength;
return result;
}
const float Gamma = 0.80;
const int IntensityMax = 255;
void wavelength_to_rgb(float wavelength, int& rV, int& gV, int& bV) {
int w = int(wavelength);
float r;
float g;
float b;
float SSS;
// colour
if (w >= 380 && w < 440) {
r = -(w - 440.0) / (440.0 - 350.0);
g = 0.0;
b = 1.0;
}
else if (w >= 440 && w < 490) {
r = 0.0;
g = (w - 440.0) / (490.0 - 440.0);
b = 1.0;
}
else if (w >= 490 && w < 510) {
r = 0.0;
g = 1.0;
b = -(w - 510.) / (510. - 490.);
}
else if (w >= 510 && w < 580) {
r = (w - 510.) / (580. - 510.);
g = 1.0;
b = 0.0;
}
else if (w >= 580 && w < 645) {
r = 1.0;
g = -(w - 645.) / (645. - 580.);
b = 0.0;
}
else if (w >= 645 && w <= 780) {
r = 1.0;
g = 0.0;
b = 0.0;
}
else {
r = 0.0;
g = 0.0;
b = 0.0;
}
// intensity correction
if (w >= 380 && w < 420) {
SSS = 0.3 + 0.7*(w - 350) / (420 - 350);
}
else if (w >= 420 && w <= 700) {
SSS = 1.0;
}
else if (w > 700 && w <= 780) {
SSS = 0.3 + 0.7*(780 - w) / (780 - 700);
}
else {
SSS = 0.0;
}
SSS *= 255;
rV = SSS*r;
gV = SSS*g;
bV = SSS*b;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment