eka sinambela / Mbed 2 deprecated g3_waterplay

Dependencies:   mbed

Fork of g3_waterplay by Mario Simaremare

Revision:
3:7c648d1d8802
Parent:
2:ed17e258da0d
Child:
5:4cbe44452889
diff -r ed17e258da0d -r 7c648d1d8802 TemperatureSensor.cpp
--- a/TemperatureSensor.cpp	Tue Jun 14 09:14:24 2016 +0000
+++ b/TemperatureSensor.cpp	Thu Jun 16 11:28:59 2016 +0000
@@ -3,10 +3,11 @@
 */
 
 #include "TemperatureSensor.h"
+#include <algorithm>
 #include "mbed.h"
 
 TemperatureSensor::TemperatureSensor(
-    mbed::Serial &serial,
+    Printer &printer,
     PinName pin,
     double const_voltage,
     double const_converter,
@@ -16,12 +17,13 @@
     double vin,
     double resistance
 ):
-    _serial(serial),
+    _printer(printer),
     _analog_in(pin),
     _const_voltage(const_voltage),
     _const_converter(const_converter),
     _const_lower_boundary(const_lower_boundary),
     _const_upper_boundary(const_upper_boundary),
+    _const_sample_number(100),
     _variance(variance),
     _vin(vin),
     _resistance(resistance),
@@ -29,6 +31,7 @@
     _voltage(0.0),
     _temperature(0.0),
     _status(0.0),
+    _strStatus("OK"),
     _k0(0.00102119),
     _k1(0.000222468),
     _k2(0.000000133342),
@@ -39,7 +42,14 @@
 
 void TemperatureSensor::reload()
 {
-    this->_reading = _analog_in.read();
+    double readings[this->_const_sample_number];
+    for(int counter = 0; counter < this->_const_sample_number; ++counter){
+        readings[counter] = _analog_in.read();
+    }
+    
+    sort(readings, readings + this->_const_sample_number);
+    
+    this->_reading = readings[this->_const_sample_number / 2];
     this->_voltage = this->_reading * this->_const_voltage * this->_const_converter;
     double RT = (this->_voltage * this->_resistance) / (this->_vin - this->_voltage);
     double logRT = log(RT);
@@ -50,21 +60,16 @@
     this->_temperature = (kelvin + this->_kelvin_to_celcius) + this->_variance;
     
     this->_status = 0.0;
+    this->_strStatus = "OK";
     if(this->_temperature < this->_const_lower_boundary){
         this->_status = this->_temperature - this->_const_lower_boundary;
+        this->_strStatus = "LW";
     } else if(this->_temperature > this->_const_upper_boundary){
         this->_status = this->_temperature - this->_const_upper_boundary;
+        this->_strStatus = "HI";
     }
-    
-    _serial.printf(
-        "\ttemperature: %5.4F | %5.4F | %5.4F\n\r",
-        this->_reading,
-        this->_voltage,
-        this->_temperature
-    );
 }
 
-
 double TemperatureSensor::getReading()
 {
     return(this->_reading);
@@ -84,4 +89,9 @@
 double TemperatureSensor::getStatus()
 {
     return(this->_status);
+}
+
+char* TemperatureSensor::getStrStatus()
+{
+    return(this->_strStatus);
 }
\ No newline at end of file