eka sinambela / Mbed 2 deprecated g3_waterplay

Dependencies:   mbed

Fork of g3_waterplay by Mario Simaremare

Revision:
1:f448c12d2c5b
Parent:
0:ad9362f18797
Child:
2:ed17e258da0d
diff -r ad9362f18797 -r f448c12d2c5b TemperatureSensor.cpp
--- a/TemperatureSensor.cpp	Thu Jun 02 17:53:02 2016 +0000
+++ b/TemperatureSensor.cpp	Tue Jun 07 10:21:07 2016 +0000
@@ -6,6 +6,7 @@
 #include "mbed.h"
 
 TemperatureSensor::TemperatureSensor(
+    mbed::Serial &serial,
     PinName pin,
     double const_voltage,
     double const_converter,
@@ -13,12 +14,14 @@
     double vin,
     double resistance
 ):
+    _serial(serial),
     _analog_in(pin),
     _const_voltage(const_voltage),
     _const_converter(const_converter),
     _variance(variance),
     _vin(vin),
     _resistance(resistance),
+    _temperature(0.0),
     _k0(0.00102119),
     _k1(0.000222468),
     _k2(0.000000133342),
@@ -29,28 +32,35 @@
 
 double TemperatureSensor::read()
 {
-    _voltage = _analog_in.read();
+    this->_reading = _analog_in.read();
+    this->_voltage = this->_reading * _const_voltage;
+    this->_voltage = this->_reading * _const_voltage;
+    double RT = (this->_voltage * _resistance) / (_vin - this->_voltage);
+    double logRT = log(RT);
+    double K0 = this->_k0;
+    double K1 = this->_k1 * logRT;
+    double K2 = this->_k2 * pow(logRT, 3.0);
+    double kelvin = 1.0 / (K0 + K1 + K2);
+    this->_temperature = (kelvin + this->_kelvin_to_celcius) + this->_variance;
+    
+    /*
+    _serial.printf(
+        "\ttemperature: %5.4F | %5.4F | %5.4F\n\r",
+        this->_reading,
+        this->_voltage,
+        this->_temperature
+    );
+    */
 
-    return(_voltage);
+    return(this->_reading);
 }
 
 double TemperatureSensor::getVoltage()
 {
-    float retVal = _voltage * _const_voltage;
-
-    return(retVal);
+    return(this->_voltage);
 }
 
 double TemperatureSensor::getTemperature()
 {
-    double vout = getVoltage();
-    double RT = (vout * _resistance) / (_vin - vout);
-    double logRT = log(RT);
-    double K0 = _k0;
-    double K1 = _k1 * logRT;
-    double K2 = _k2 * pow(logRT, 3.0);
-    double kelvin = 1.0 / (K0 + K1 + K2);
-    double celcius = (kelvin + _kelvin_to_celcius) + _variance;
-
-    return(celcius);
+    return(this->_temperature);
 }