Map temperature to color

Dependencies:   Chainable_RGB_LED mbed

Fork of Seeed_Grove_Chainable_RGB_LED_Example by Yihui Xiong

Committer:
yihui
Date:
Fri Aug 22 08:59:43 2014 +0000
Revision:
1:b18ce9f622ff
Parent:
0:74eac9e7f286
Color changes with temperature changing

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yihui 0:74eac9e7f286 1 #include "mbed.h"
yihui 0:74eac9e7f286 2 #include "ChainableLED.h"
yihui 0:74eac9e7f286 3
yihui 1:b18ce9f622ff 4 //#define DEBUG
yihui 1:b18ce9f622ff 5
yihui 1:b18ce9f622ff 6 #ifdef DEBUG
yihui 1:b18ce9f622ff 7
yihui 1:b18ce9f622ff 8 #include "USBSerial.h"
yihui 1:b18ce9f622ff 9 #define LOG(args...) pc.printf(args)
yihui 1:b18ce9f622ff 10 USBSerial pc;
yihui 1:b18ce9f622ff 11
yihui 1:b18ce9f622ff 12 #else
yihui 1:b18ce9f622ff 13
yihui 1:b18ce9f622ff 14 #define LOG(args...)
yihui 1:b18ce9f622ff 15
yihui 1:b18ce9f622ff 16 #endif // DEBUG
yihui 1:b18ce9f622ff 17
yihui 1:b18ce9f622ff 18
yihui 0:74eac9e7f286 19 // ChainableLED(clk, data, number_of_leds)
yihui 0:74eac9e7f286 20 ChainableLED color_led(P1_14, P1_13, 1);
yihui 1:b18ce9f622ff 21 AnalogIn thermistor(P0_12);
yihui 1:b18ce9f622ff 22
yihui 1:b18ce9f622ff 23 float get_temperature()
yihui 1:b18ce9f622ff 24 {
yihui 1:b18ce9f622ff 25 unsigned int a, beta = 3975;
yihui 1:b18ce9f622ff 26 float temperature, resistance;
yihui 1:b18ce9f622ff 27
yihui 1:b18ce9f622ff 28 a = thermistor.read_u16();
yihui 1:b18ce9f622ff 29
yihui 1:b18ce9f622ff 30 /* Calculate the resistance of the thermistor from analog votage read. */
yihui 1:b18ce9f622ff 31 resistance = (float) 10000.0 * ((65536.0 / a) - 1);
yihui 1:b18ce9f622ff 32
yihui 1:b18ce9f622ff 33 /* Convert the resistance to temperature using Steinhart's Hart equation */
yihui 1:b18ce9f622ff 34 temperature=(1/((log(resistance/10000.0)/beta) + (1.0/298.15)))-273.15;
yihui 1:b18ce9f622ff 35
yihui 1:b18ce9f622ff 36 return temperature;
yihui 1:b18ce9f622ff 37 }
yihui 1:b18ce9f622ff 38
yihui 1:b18ce9f622ff 39 int temperature2color(float t)
yihui 1:b18ce9f622ff 40 {
yihui 1:b18ce9f622ff 41 float low = 26;
yihui 1:b18ce9f622ff 42 float high = 30;
yihui 1:b18ce9f622ff 43 int min = 0;
yihui 1:b18ce9f622ff 44 int max = 255;
yihui 1:b18ce9f622ff 45 int color;
yihui 1:b18ce9f622ff 46
yihui 1:b18ce9f622ff 47 if (t < low) {
yihui 1:b18ce9f622ff 48 color = min;
yihui 1:b18ce9f622ff 49 } else if (t > high) {
yihui 1:b18ce9f622ff 50 color = max;
yihui 1:b18ce9f622ff 51 } else {
yihui 1:b18ce9f622ff 52 color = min + (max - min) * ((t - min) / (max - min));
yihui 1:b18ce9f622ff 53 }
yihui 1:b18ce9f622ff 54
yihui 1:b18ce9f622ff 55 return color;
yihui 1:b18ce9f622ff 56 }
yihui 1:b18ce9f622ff 57
yihui 0:74eac9e7f286 58
yihui 0:74eac9e7f286 59 int main() {
yihui 1:b18ce9f622ff 60
yihui 0:74eac9e7f286 61 while(1) {
yihui 1:b18ce9f622ff 62 float t = get_temperature();
yihui 1:b18ce9f622ff 63 uint8_t color = temperature2color(t);
yihui 1:b18ce9f622ff 64
yihui 1:b18ce9f622ff 65 LOG("Temperature: %f\r\n", t);
yihui 1:b18ce9f622ff 66 LOG("Color: %d\r\n", color);
yihui 1:b18ce9f622ff 67
yihui 0:74eac9e7f286 68 // ChainableLED.setColorRGB(index_of_led, red, green, blue)
yihui 1:b18ce9f622ff 69 color_led.setColorRGB(0, color, 0xFF - color, 0);
yihui 1:b18ce9f622ff 70 wait(0.05);
yihui 0:74eac9e7f286 71 }
yihui 0:74eac9e7f286 72 }