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

Committer:
co838_gtvl2
Date:
Wed Feb 10 01:01:54 2016 +0000
Revision:
2:5d0c209e5c61
Parent:
1:06713d1b69cf
Child:
3:a4dac093a968
Added scale management using POT 2.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
co838_gtvl2 1:06713d1b69cf 1 #include "main.h"
co838_gtvl2 1:06713d1b69cf 2 #include "ThermalDisplayer.h"
co838_gtvl2 0:362773c24009 3
co838_gtvl2 1:06713d1b69cf 4 Serial host(USBTX, USBRX); // Serial Host
co838_gtvl2 1:06713d1b69cf 5 LM75B lm_temp(D14, D15); // Thermal Sensor
co838_gtvl2 1:06713d1b69cf 6 InterruptIn sw2_int(SW2); // SW2
co838_gtvl2 1:06713d1b69cf 7 DigitalOut r_led(PTA2); // RGB LED
co838_gtvl2 1:06713d1b69cf 8 DigitalOut g_led(PTC4);
co838_gtvl2 1:06713d1b69cf 9 DigitalOut b_led(PTA0); // Won't use BLUE because not working on my device !
co838_gtvl2 1:06713d1b69cf 10 AnalogIn pot(A0); // Potentiometer POT1
co838_gtvl2 0:362773c24009 11
co838_gtvl2 1:06713d1b69cf 12 Ticker rgb_tick; // Ticker for RGB LED control
co838_gtvl2 1:06713d1b69cf 13 volatile bool celcius = true; // Global boolean for unit Celcius / Fahrenheit
co838_gtvl2 0:362773c24009 14
co838_gtvl2 1:06713d1b69cf 15 /*
co838_gtvl2 1:06713d1b69cf 16 * RGB LED control
co838_gtvl2 1:06713d1b69cf 17 */
co838_gtvl2 2:5d0c209e5c61 18 void rgb_handler()
co838_gtvl2 2:5d0c209e5c61 19 {
co838_gtvl2 0:362773c24009 20 if (celcius) {
co838_gtvl2 0:362773c24009 21 r_led = 1.0;
co838_gtvl2 0:362773c24009 22 g_led = 0.0;
co838_gtvl2 0:362773c24009 23 } else {
co838_gtvl2 0:362773c24009 24 r_led = 0.0;
co838_gtvl2 0:362773c24009 25 g_led = 1.0;
co838_gtvl2 2:5d0c209e5c61 26 }
co838_gtvl2 0:362773c24009 27 }
co838_gtvl2 0:362773c24009 28
co838_gtvl2 1:06713d1b69cf 29 /*
co838_gtvl2 1:06713d1b69cf 30 * SW2 handler
co838_gtvl2 1:06713d1b69cf 31 */
co838_gtvl2 2:5d0c209e5c61 32 void sw_handler (void)
co838_gtvl2 2:5d0c209e5c61 33 {
co838_gtvl2 0:362773c24009 34 celcius = !celcius;
co838_gtvl2 0:362773c24009 35 }
co838_gtvl2 0:362773c24009 36
co838_gtvl2 2:5d0c209e5c61 37 int main(void)
co838_gtvl2 2:5d0c209e5c61 38 {
co838_gtvl2 0:362773c24009 39 host.baud(38400);
co838_gtvl2 2:5d0c209e5c61 40 host.printf("Hello gtvl2, from FRDM-K64F!\r\nUse POT1 to change the refresh rate, POT2 to change the scale of the graphic and SW2 to change the unit\r\n");
co838_gtvl2 2:5d0c209e5c61 41
co838_gtvl2 0:362773c24009 42 r_led = 0.0;
co838_gtvl2 0:362773c24009 43 g_led = 0.0;
co838_gtvl2 2:5d0c209e5c61 44 b_led = 1.0;
co838_gtvl2 2:5d0c209e5c61 45
co838_gtvl2 1:06713d1b69cf 46 int refresh_time = 3000;
co838_gtvl2 2:5d0c209e5c61 47
co838_gtvl2 1:06713d1b69cf 48 /*
co838_gtvl2 1:06713d1b69cf 49 * Thermal unit control
co838_gtvl2 1:06713d1b69cf 50 */
co838_gtvl2 2:5d0c209e5c61 51 ThermalDisplayer *temp_display = new ThermalDisplayer();
co838_gtvl2 1:06713d1b69cf 52 rgb_tick.attach(&rgb_handler, 0.001);
co838_gtvl2 0:362773c24009 53 sw2_int.mode(PullUp);
co838_gtvl2 2:5d0c209e5c61 54 sw2_int.fall(&sw_handler);
co838_gtvl2 2:5d0c209e5c61 55
co838_gtvl2 2:5d0c209e5c61 56 while (true) {
co838_gtvl2 2:5d0c209e5c61 57
co838_gtvl2 1:06713d1b69cf 58 /*
co838_gtvl2 1:06713d1b69cf 59 * Get temperature using LM75B
co838_gtvl2 1:06713d1b69cf 60 * and display it to LCD screen
co838_gtvl2 1:06713d1b69cf 61 */
co838_gtvl2 0:362773c24009 62 float temperature = lm_temp.read();
co838_gtvl2 1:06713d1b69cf 63 temp_display->addTemp(temperature);
co838_gtvl2 2:5d0c209e5c61 64 temp_display->adjustScale();
co838_gtvl2 1:06713d1b69cf 65 temp_display->display();
co838_gtvl2 2:5d0c209e5c61 66
co838_gtvl2 2:5d0c209e5c61 67 /*
co838_gtvl2 2:5d0c209e5c61 68 * Display value to host according to unit choosed with SW2.
co838_gtvl2 2:5d0c209e5c61 69 * Could have been a ternary, but it's easier to read like this
co838_gtvl2 1:06713d1b69cf 70 */
co838_gtvl2 0:362773c24009 71 if (celcius) {
co838_gtvl2 0:362773c24009 72 host.printf("Temp: %.2f deg Celcius.\r\n", temperature);
co838_gtvl2 0:362773c24009 73 } else {
co838_gtvl2 0:362773c24009 74 host.printf("Temp: %.2f deg Fahrenheit.\r\n", (temperature * 1.80 + 32.00));
co838_gtvl2 0:362773c24009 75 }
co838_gtvl2 2:5d0c209e5c61 76
co838_gtvl2 1:06713d1b69cf 77 /*
co838_gtvl2 2:5d0c209e5c61 78 * Change refresh_time according to POT1's value
co838_gtvl2 1:06713d1b69cf 79 */
co838_gtvl2 1:06713d1b69cf 80 refresh_time = (int) (MIN_REFRESH_TIME + (pot * (MAX_REFRESH_TIME - MIN_REFRESH_TIME)));
co838_gtvl2 1:06713d1b69cf 81 wait_ms(refresh_time);
co838_gtvl2 0:362773c24009 82 }
co838_gtvl2 0:362773c24009 83 }