Show the temperature using TMP36

Dependencies:   mbed

temperature.cpp

Committer:
faif
Date:
2017-01-14
Revision:
0:bf61ac6be2d7

File content as of revision 0:bf61ac6be2d7:

#include "mbed.h"
#include "temperature.h"

static const char CLS[] = "\x1B[2J";    // VT100 erase screen
static const char HOME[] = "\x1B[H";    // VT100 home

static const int SAMPLES = 100;         // use an average for better precision
enum { delay = 1 };

int main() 
{
    clear_screen();
    pc.printf("========[ Temperature ]========");
    
    while (true) 
    {
        pc.printf(HOME);
        float voltage = measure_voltage(SAMPLES);
        float temperature = voltage_to_temperature(voltage, SAMPLES);
        pc.printf("\n\rtemperature: %4.1f C", temperature);
        wait(delay);
    }
}

void clear_screen()
{
    pc.printf(CLS);      
    pc.printf(HOME);               
}

float measure_voltage(int samples)
{
    float voltage = 0;
    for (int i = 0; i < samples; ++i)
    {
        voltage += temperatureIn;
    }
    return voltage;
}

float voltage_to_temperature(int voltage, int samples)
{
    float temperature = (voltage / 0.01) / samples;
    return temperature;
}