Program for the water play project for the course Software Testing Practical 2016 given at the VU University

Dependencies:   mbed DRV88255 TextLCD Ping mbed-rtos

TemperatureController.cpp

Committer:
6366295
Date:
2016-06-03
Revision:
0:dab140a197e0
Child:
1:eb527bc93b62

File content as of revision 0:dab140a197e0:

#include "TemperatureController.h"

#include "stdio.h"

AnalogIn temperature_sensor(p20);

TemperatureController::TemperatureController()
{
    printf("Initiate Temperature Controller\r\n");
}

// Function reads sensor values and averages N sensor values
float TemperatureController::readSensor()
{
    float analogin_value;
    float temperature;
    
    // Read 0-1.0 value
    for(int i = 0; i < N; i++)
    {
        analogin_value += temperature_sensor.read();
    }
    // Average 10 sensor values
    analogin_value /= (float) N;
    
    temperature = analoginToCelsius(analogin_value);
    
    return temperature;
}

// Function converts average sensor value to ppt
float TemperatureController::analoginToCelsius(float analogin_value)
{
    float voltage;
    float rt, logrt;
    float temperature;
    
    // Convert average sensor value to value with range 0-5.0v
    voltage = analogin_value * 3.3f * (5.0f/3.0f) + 0.45f;
    
    // Calcuate R_T
    rt = (15000.0f * voltage) / (4.8f - voltage);
    
    logrt = log(rt);
    temperature = 1.0 / (0.00102119 + (0.000222468 * logrt) + (0.000000133342 * logrt*logrt*logrt));
    
    temperature = temperature - 273.15;
    
    return temperature;
}

// Function prints temperature sensor value in celsius
void TemperatureController::displayCelsius()
{
    float temperature;
    
    temperature = readSensor();
    
    printf("Temperature: %f Celsius\r\n", temperature);
}