Mike Fiore / Mbed 2 deprecated Inspirado_demo

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "mtsas.h"
00003 
00004 AnalogIn light(A0);
00005 AnalogIn temp(A1);
00006 DigitalOut led(D4);
00007 
00008 bool lights_on() {
00009     float val = light.read();
00010     if (val > 0.4f)
00011         return true;
00012     return false;
00013 }
00014 
00015 float get_temp_c() {
00016     int beta = 3975;
00017     int val = temp.read_u16();
00018     float res = (float) 10000.0 * ((65536.0 / val) - 1.0);
00019     float temp_c = (1 / ((log(res / 5000.0) / beta) + (1.0 / 298.15))) - 273.15 - 7;
00020     
00021     return temp_c;
00022 }
00023 
00024 float get_temp_f() {
00025     float temp_f = get_temp_c() * 9 / 5 + 32;
00026     
00027     return temp_f;
00028 }
00029 
00030 int main() {
00031     while (true) {
00032         printf("temp: %f C\t %f F\r\n", get_temp_c(), get_temp_f());
00033         if (lights_on()) {
00034             printf("lights on!\r\n");
00035             led = 0;
00036         } else {
00037             printf("lights off!\r\n");
00038             led = 1;
00039         }
00040         wait(2);
00041     }
00042     
00043     return 0;
00044 }