This program is an advanced "IoT" thermometer using LM75B component library. It displays the current value to the Serial host in Celcius or Fahrenheit (possible to change using a switch). It also stores an historic and displays it to the user using the LCD screen. Moreover, you can change the orientation of the screen by turning the device, just like a smartphone.
Dependencies: C12832 FXOS8700CQ LM75B mbed
main.cpp@0:362773c24009, 2016-02-09 (annotated)
- Committer:
- co838_gtvl2
- Date:
- Tue Feb 09 18:27:08 2016 +0000
- Revision:
- 0:362773c24009
- Child:
- 1:06713d1b69cf
Working version with Thermal sensor, report to USB Serial Host, change unit (C to F) from SW2 and visualisation on the top RGB LED.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
co838_gtvl2 | 0:362773c24009 | 1 | #include "mbed.h" |
co838_gtvl2 | 0:362773c24009 | 2 | #include "LM75B.h" |
co838_gtvl2 | 0:362773c24009 | 3 | |
co838_gtvl2 | 0:362773c24009 | 4 | // #undef _DEBUG |
co838_gtvl2 | 0:362773c24009 | 5 | #define _DEBUG |
co838_gtvl2 | 0:362773c24009 | 6 | |
co838_gtvl2 | 0:362773c24009 | 7 | Serial host(USBTX, USBRX); |
co838_gtvl2 | 0:362773c24009 | 8 | LM75B lm_temp(D14, D15); |
co838_gtvl2 | 0:362773c24009 | 9 | InterruptIn sw2_int (SW2); |
co838_gtvl2 | 0:362773c24009 | 10 | Ticker rgb_tick; |
co838_gtvl2 | 0:362773c24009 | 11 | DigitalOut r_led (PTA2); |
co838_gtvl2 | 0:362773c24009 | 12 | DigitalOut g_led (PTC4); |
co838_gtvl2 | 0:362773c24009 | 13 | DigitalOut b_led (PTA0); // Won't use it because not working on my Shield ! |
co838_gtvl2 | 0:362773c24009 | 14 | |
co838_gtvl2 | 0:362773c24009 | 15 | volatile bool celcius = true; |
co838_gtvl2 | 0:362773c24009 | 16 | |
co838_gtvl2 | 0:362773c24009 | 17 | void rgb_handler() { |
co838_gtvl2 | 0:362773c24009 | 18 | if (celcius) { |
co838_gtvl2 | 0:362773c24009 | 19 | r_led = 1.0; |
co838_gtvl2 | 0:362773c24009 | 20 | g_led = 0.0; |
co838_gtvl2 | 0:362773c24009 | 21 | } else { |
co838_gtvl2 | 0:362773c24009 | 22 | r_led = 0.0; |
co838_gtvl2 | 0:362773c24009 | 23 | g_led = 1.0; |
co838_gtvl2 | 0:362773c24009 | 24 | } |
co838_gtvl2 | 0:362773c24009 | 25 | } |
co838_gtvl2 | 0:362773c24009 | 26 | |
co838_gtvl2 | 0:362773c24009 | 27 | void sw_interrupt (void) { |
co838_gtvl2 | 0:362773c24009 | 28 | celcius = !celcius; |
co838_gtvl2 | 0:362773c24009 | 29 | } |
co838_gtvl2 | 0:362773c24009 | 30 | |
co838_gtvl2 | 0:362773c24009 | 31 | int main(void) { |
co838_gtvl2 | 0:362773c24009 | 32 | host.baud(38400); |
co838_gtvl2 | 0:362773c24009 | 33 | host.printf("Hello LeNiglo, from FRDM-K64F!\r\n"); |
co838_gtvl2 | 0:362773c24009 | 34 | |
co838_gtvl2 | 0:362773c24009 | 35 | r_led = 0.0; |
co838_gtvl2 | 0:362773c24009 | 36 | g_led = 0.0; |
co838_gtvl2 | 0:362773c24009 | 37 | b_led = 1.0; |
co838_gtvl2 | 0:362773c24009 | 38 | |
co838_gtvl2 | 0:362773c24009 | 39 | rgb_tick.attach(&rgb_handler, 0.001); |
co838_gtvl2 | 0:362773c24009 | 40 | sw2_int.mode(PullUp); |
co838_gtvl2 | 0:362773c24009 | 41 | sw2_int.fall(&sw_interrupt); |
co838_gtvl2 | 0:362773c24009 | 42 | |
co838_gtvl2 | 0:362773c24009 | 43 | /* forever.. */ |
co838_gtvl2 | 0:362773c24009 | 44 | while (true) { |
co838_gtvl2 | 0:362773c24009 | 45 | float temperature = lm_temp.read(); |
co838_gtvl2 | 0:362773c24009 | 46 | |
co838_gtvl2 | 0:362773c24009 | 47 | #if defined _DEBUG |
co838_gtvl2 | 0:362773c24009 | 48 | if (celcius) { |
co838_gtvl2 | 0:362773c24009 | 49 | host.printf("Temp: %.2f deg Celcius.\r\n", temperature); |
co838_gtvl2 | 0:362773c24009 | 50 | } else { |
co838_gtvl2 | 0:362773c24009 | 51 | host.printf("Temp: %.2f deg Fahrenheit.\r\n", (temperature * 1.80 + 32.00)); |
co838_gtvl2 | 0:362773c24009 | 52 | } |
co838_gtvl2 | 0:362773c24009 | 53 | #endif |
co838_gtvl2 | 0:362773c24009 | 54 | wait_ms(3000); |
co838_gtvl2 | 0:362773c24009 | 55 | } |
co838_gtvl2 | 0:362773c24009 | 56 | } |