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

Revision:
1:eb527bc93b62
Parent:
0:dab140a197e0
Child:
2:f6ebc225f232
--- a/TemperatureController.cpp	Fri Jun 03 14:24:25 2016 +0000
+++ b/TemperatureController.cpp	Mon Jun 06 11:59:23 2016 +0000
@@ -1,8 +1,7 @@
 #include "TemperatureController.h"
 
-#include "stdio.h"
-
 AnalogIn temperature_sensor(p20);
+DigitalOut heater(p21);
 
 TemperatureController::TemperatureController()
 {
@@ -12,8 +11,8 @@
 // Function reads sensor values and averages N sensor values
 float TemperatureController::readSensor()
 {
-    float analogin_value;
-    float temperature;
+    float analogin_value = 0;
+    float temperature = 0;
     
     // Read 0-1.0 value
     for(int i = 0; i < N; i++)
@@ -31,12 +30,20 @@
 // Function converts average sensor value to ppt
 float TemperatureController::analoginToCelsius(float analogin_value)
 {
-    float voltage;
-    float rt, logrt;
-    float temperature;
+    float voltage = 0;
+    float offset = 0;
+    float rt = 0;
+    float logrt = 0;
+    float temperature = 0;
     
     // Convert average sensor value to value with range 0-5.0v
-    voltage = analogin_value * 3.3f * (5.0f/3.0f) + 0.45f;
+    voltage = analogin_value * 3.3f * (5.0f/3.0f);
+    //voltage /= 5.0;
+    //voltage *= 1023;
+    
+    offset = getOffset(voltage);
+    
+    voltage += offset;
     
     // Calcuate R_T
     rt = (15000.0f * voltage) / (4.8f - voltage);
@@ -50,11 +57,57 @@
 }
 
 // Function prints temperature sensor value in celsius
-void TemperatureController::displayCelsius()
+void TemperatureController::displayTemperature()
 {
-    float temperature;
+    float temperature = 0;
     
     temperature = readSensor();
     
     printf("Temperature: %f Celsius\r\n", temperature);
+}
+
+float TemperatureController::getOffset(float voltage)
+{
+    float denominator_voltage, numerator_voltage, ratio, interpolation;
+    
+    float voltage_table[15] = {1.43, 1.55, 1.53, 1.56, 1.62, 1.65, 1.67, 1.73, 1.75, 1.80, 1.82, 1.88, 1.93, 1.98, 2.00};
+    float offset_table[15] = {0.33, 0.33, 0.40, 0.40, 0.39, 0.42, 0.43, 0.41, 0.45, 0.50, 0.54, 0.53, 0.52, 0.53, 0.56};
+    
+    for(int i = 0; i < 14; i++) 
+    {
+        if(voltage_table[i] <= voltage && voltage_table[i+1] >= voltage)
+        {
+            denominator_voltage = voltage_table[i+1] - voltage_table[i];
+            numerator_voltage = voltage - voltage_table[i];
+            
+            if(denominator_voltage != 0)
+            {
+                ratio = numerator_voltage / denominator_voltage;
+                
+                interpolation = ((offset_table[i+1] - offset_table[i]) * ratio) + offset_table[i];
+                
+                return interpolation;
+            } else {
+                return offset_table[i];
+            }
+        }
+    }
+    
+    if(voltage_table[14] < voltage)
+    {
+        return offset_table[14];
+    } else {
+        return offset_table[0];
+    }
+}
+
+void TemperatureController::controlHeater()
+{
+    if(readSensor() < 32.0f)
+    {
+        heater = 1;
+    } else {
+        heater = 0;
+    }
+        
 }
\ No newline at end of file