Example program for the Seeed Studio Grove RGB Chainable LED's.
Dependencies: Chainable_RGB_LED mbed
Fork of be_colorful by
main.cpp@1:10700868060f, 2013-10-16 (annotated)
- Committer:
- yihui
- Date:
- Wed Oct 16 05:39:07 2013 +0000
- Revision:
- 1:10700868060f
- Parent:
- 0:71564ae5e242
- Child:
- 2:4dae2f80828e
changing color with temperature
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yihui | 0:71564ae5e242 | 1 | #include "mbed.h" |
yihui | 0:71564ae5e242 | 2 | #include "ChainableLED.h" |
yihui | 0:71564ae5e242 | 3 | |
yihui | 1:10700868060f | 4 | //#define DEBUG |
yihui | 1:10700868060f | 5 | |
yihui | 1:10700868060f | 6 | #ifdef DEBUG |
yihui | 1:10700868060f | 7 | |
yihui | 1:10700868060f | 8 | #include "USBSerial.h" |
yihui | 1:10700868060f | 9 | #define LOG(args...) pc.printf(args) |
yihui | 1:10700868060f | 10 | USBSerial pc; |
yihui | 1:10700868060f | 11 | |
yihui | 1:10700868060f | 12 | #else |
yihui | 1:10700868060f | 13 | |
yihui | 1:10700868060f | 14 | #define LOG(args...) |
yihui | 1:10700868060f | 15 | |
yihui | 1:10700868060f | 16 | #endif // DEBUG |
yihui | 1:10700868060f | 17 | |
yihui | 1:10700868060f | 18 | |
yihui | 0:71564ae5e242 | 19 | // ChainableLED(clk, data, number_of_leds) |
yihui | 0:71564ae5e242 | 20 | ChainableLED color_led(P1_14, P1_13, 1); |
yihui | 1:10700868060f | 21 | AnalogIn thermistor(P0_12); |
yihui | 1:10700868060f | 22 | |
yihui | 1:10700868060f | 23 | float get_temperature() |
yihui | 1:10700868060f | 24 | { |
yihui | 1:10700868060f | 25 | unsigned int a, beta = 3975; |
yihui | 1:10700868060f | 26 | float temperature, resistance; |
yihui | 1:10700868060f | 27 | |
yihui | 1:10700868060f | 28 | a = thermistor.read_u16(); |
yihui | 1:10700868060f | 29 | |
yihui | 1:10700868060f | 30 | /* Calculate the resistance of the thermistor from analog votage read. */ |
yihui | 1:10700868060f | 31 | resistance = (float) 10000.0 * ((65536.0 / a) - 1); |
yihui | 1:10700868060f | 32 | |
yihui | 1:10700868060f | 33 | /* Convert the resistance to temperature using Steinhart's Hart equation */ |
yihui | 1:10700868060f | 34 | temperature=(1/((log(resistance/10000.0)/beta) + (1.0/298.15)))-273.15; |
yihui | 1:10700868060f | 35 | |
yihui | 1:10700868060f | 36 | return temperature; |
yihui | 1:10700868060f | 37 | } |
yihui | 1:10700868060f | 38 | |
yihui | 1:10700868060f | 39 | int temperature2color(float t) |
yihui | 1:10700868060f | 40 | { |
yihui | 1:10700868060f | 41 | float low = 26; |
yihui | 1:10700868060f | 42 | float high = 30; |
yihui | 1:10700868060f | 43 | int min = 0; |
yihui | 1:10700868060f | 44 | int max = 255; |
yihui | 1:10700868060f | 45 | int color; |
yihui | 1:10700868060f | 46 | |
yihui | 1:10700868060f | 47 | if (t < low) { |
yihui | 1:10700868060f | 48 | color = min; |
yihui | 1:10700868060f | 49 | } else if (t > high) { |
yihui | 1:10700868060f | 50 | color = max; |
yihui | 1:10700868060f | 51 | } else { |
yihui | 1:10700868060f | 52 | color = min + (max - min) * ((t - min) / (max - min)); |
yihui | 1:10700868060f | 53 | } |
yihui | 1:10700868060f | 54 | |
yihui | 1:10700868060f | 55 | return color; |
yihui | 1:10700868060f | 56 | } |
yihui | 1:10700868060f | 57 | |
yihui | 0:71564ae5e242 | 58 | |
yihui | 0:71564ae5e242 | 59 | int main() { |
yihui | 1:10700868060f | 60 | |
yihui | 0:71564ae5e242 | 61 | while(1) { |
yihui | 1:10700868060f | 62 | float t = get_temperature(); |
yihui | 1:10700868060f | 63 | uint8_t color = temperature2color(t); |
yihui | 1:10700868060f | 64 | |
yihui | 1:10700868060f | 65 | LOG("Temperature: %f\r\n", t); |
yihui | 1:10700868060f | 66 | LOG("Color: %d\r\n", color); |
yihui | 1:10700868060f | 67 | |
yihui | 0:71564ae5e242 | 68 | // ChainableLED.setColorRGB(index_of_led, red, green, blue) |
yihui | 1:10700868060f | 69 | color_led.setColorRGB(0, color, 0xFF - color, 0); |
yihui | 1:10700868060f | 70 | wait(0.05); |
yihui | 0:71564ae5e242 | 71 | } |
yihui | 0:71564ae5e242 | 72 | } |
yihui | 0:71564ae5e242 | 73 |