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

Committer:
6366295
Date:
Fri Jun 03 14:24:25 2016 +0000
Revision:
0:dab140a197e0
Child:
1:eb527bc93b62
Implemented SalinityController and TemperatureController

Who changed what in which revision?

UserRevisionLine numberNew contents of line
6366295 0:dab140a197e0 1 #include "TemperatureController.h"
6366295 0:dab140a197e0 2
6366295 0:dab140a197e0 3 #include "stdio.h"
6366295 0:dab140a197e0 4
6366295 0:dab140a197e0 5 AnalogIn temperature_sensor(p20);
6366295 0:dab140a197e0 6
6366295 0:dab140a197e0 7 TemperatureController::TemperatureController()
6366295 0:dab140a197e0 8 {
6366295 0:dab140a197e0 9 printf("Initiate Temperature Controller\r\n");
6366295 0:dab140a197e0 10 }
6366295 0:dab140a197e0 11
6366295 0:dab140a197e0 12 // Function reads sensor values and averages N sensor values
6366295 0:dab140a197e0 13 float TemperatureController::readSensor()
6366295 0:dab140a197e0 14 {
6366295 0:dab140a197e0 15 float analogin_value;
6366295 0:dab140a197e0 16 float temperature;
6366295 0:dab140a197e0 17
6366295 0:dab140a197e0 18 // Read 0-1.0 value
6366295 0:dab140a197e0 19 for(int i = 0; i < N; i++)
6366295 0:dab140a197e0 20 {
6366295 0:dab140a197e0 21 analogin_value += temperature_sensor.read();
6366295 0:dab140a197e0 22 }
6366295 0:dab140a197e0 23 // Average 10 sensor values
6366295 0:dab140a197e0 24 analogin_value /= (float) N;
6366295 0:dab140a197e0 25
6366295 0:dab140a197e0 26 temperature = analoginToCelsius(analogin_value);
6366295 0:dab140a197e0 27
6366295 0:dab140a197e0 28 return temperature;
6366295 0:dab140a197e0 29 }
6366295 0:dab140a197e0 30
6366295 0:dab140a197e0 31 // Function converts average sensor value to ppt
6366295 0:dab140a197e0 32 float TemperatureController::analoginToCelsius(float analogin_value)
6366295 0:dab140a197e0 33 {
6366295 0:dab140a197e0 34 float voltage;
6366295 0:dab140a197e0 35 float rt, logrt;
6366295 0:dab140a197e0 36 float temperature;
6366295 0:dab140a197e0 37
6366295 0:dab140a197e0 38 // Convert average sensor value to value with range 0-5.0v
6366295 0:dab140a197e0 39 voltage = analogin_value * 3.3f * (5.0f/3.0f) + 0.45f;
6366295 0:dab140a197e0 40
6366295 0:dab140a197e0 41 // Calcuate R_T
6366295 0:dab140a197e0 42 rt = (15000.0f * voltage) / (4.8f - voltage);
6366295 0:dab140a197e0 43
6366295 0:dab140a197e0 44 logrt = log(rt);
6366295 0:dab140a197e0 45 temperature = 1.0 / (0.00102119 + (0.000222468 * logrt) + (0.000000133342 * logrt*logrt*logrt));
6366295 0:dab140a197e0 46
6366295 0:dab140a197e0 47 temperature = temperature - 273.15;
6366295 0:dab140a197e0 48
6366295 0:dab140a197e0 49 return temperature;
6366295 0:dab140a197e0 50 }
6366295 0:dab140a197e0 51
6366295 0:dab140a197e0 52 // Function prints temperature sensor value in celsius
6366295 0:dab140a197e0 53 void TemperatureController::displayCelsius()
6366295 0:dab140a197e0 54 {
6366295 0:dab140a197e0 55 float temperature;
6366295 0:dab140a197e0 56
6366295 0:dab140a197e0 57 temperature = readSensor();
6366295 0:dab140a197e0 58
6366295 0:dab140a197e0 59 printf("Temperature: %f Celsius\r\n", temperature);
6366295 0:dab140a197e0 60 }