eka sinambela
/
g3_waterplay
ok
Fork of g3_waterplay by
SalinitySensor.cpp@3:7c648d1d8802, 2016-06-16 (annotated)
- Committer:
- mariosimaremare
- Date:
- Thu Jun 16 11:28:59 2016 +0000
- Revision:
- 3:7c648d1d8802
- Parent:
- 2:ed17e258da0d
- Child:
- 5:4cbe44452889
Spread the code into classes.; Median is implemented.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mariosimaremare | 0:ad9362f18797 | 1 | /* |
mariosimaremare | 0:ad9362f18797 | 2 | * G3: WATERPLAY |
mariosimaremare | 0:ad9362f18797 | 3 | */ |
mariosimaremare | 0:ad9362f18797 | 4 | |
mariosimaremare | 0:ad9362f18797 | 5 | #include "SalinitySensor.h" |
mariosimaremare | 0:ad9362f18797 | 6 | #include "mbed.h" |
mariosimaremare | 3:7c648d1d8802 | 7 | #include <algorithm> |
mariosimaremare | 3:7c648d1d8802 | 8 | #include "Printer.h" |
mariosimaremare | 0:ad9362f18797 | 9 | |
mariosimaremare | 0:ad9362f18797 | 10 | SalinitySensor::SalinitySensor( |
mariosimaremare | 3:7c648d1d8802 | 11 | Printer &printer, |
mariosimaremare | 0:ad9362f18797 | 12 | PinName pin, |
mariosimaremare | 0:ad9362f18797 | 13 | double const_voltage, |
mariosimaremare | 0:ad9362f18797 | 14 | double const_converter, |
mariosimaremare | 2:ed17e258da0d | 15 | double const_multiplier, |
mariosimaremare | 2:ed17e258da0d | 16 | double const_lower_boundary, |
mariosimaremare | 2:ed17e258da0d | 17 | double const_upper_boundary |
mariosimaremare | 0:ad9362f18797 | 18 | ): |
mariosimaremare | 3:7c648d1d8802 | 19 | _printer(printer), |
mariosimaremare | 0:ad9362f18797 | 20 | _analog_in(pin), |
mariosimaremare | 0:ad9362f18797 | 21 | _const_voltage(const_voltage), |
mariosimaremare | 0:ad9362f18797 | 22 | _const_converter(const_converter), |
mariosimaremare | 1:f448c12d2c5b | 23 | _const_multiplier(const_multiplier), |
mariosimaremare | 2:ed17e258da0d | 24 | _const_lower_boundary(const_lower_boundary), |
mariosimaremare | 2:ed17e258da0d | 25 | _const_upper_boundary(const_upper_boundary), |
mariosimaremare | 3:7c648d1d8802 | 26 | _const_sample_number(100), |
mariosimaremare | 1:f448c12d2c5b | 27 | _reading(0.0), |
mariosimaremare | 1:f448c12d2c5b | 28 | _voltage(0.0), |
mariosimaremare | 2:ed17e258da0d | 29 | _salinity(0.0), |
mariosimaremare | 3:7c648d1d8802 | 30 | _status(0.0), |
mariosimaremare | 3:7c648d1d8802 | 31 | _strStatus("OK") |
mariosimaremare | 0:ad9362f18797 | 32 | { |
mariosimaremare | 2:ed17e258da0d | 33 | this->reload(); |
mariosimaremare | 0:ad9362f18797 | 34 | } |
mariosimaremare | 0:ad9362f18797 | 35 | |
mariosimaremare | 2:ed17e258da0d | 36 | void SalinitySensor::reload() |
mariosimaremare | 0:ad9362f18797 | 37 | { |
mariosimaremare | 3:7c648d1d8802 | 38 | double readings[this->_const_sample_number]; |
mariosimaremare | 3:7c648d1d8802 | 39 | |
mariosimaremare | 3:7c648d1d8802 | 40 | for(int counter = 0; counter < this->_const_sample_number; ++counter){ |
mariosimaremare | 3:7c648d1d8802 | 41 | // the reading from sensor. |
mariosimaremare | 3:7c648d1d8802 | 42 | readings[counter] = this->_analog_in.read(); |
mariosimaremare | 3:7c648d1d8802 | 43 | } |
mariosimaremare | 3:7c648d1d8802 | 44 | |
mariosimaremare | 3:7c648d1d8802 | 45 | sort(readings, readings + this->_const_sample_number); |
mariosimaremare | 3:7c648d1d8802 | 46 | |
mariosimaremare | 3:7c648d1d8802 | 47 | this->_reading = readings[this->_const_sample_number / 2]; |
mariosimaremare | 3:7c648d1d8802 | 48 | |
mariosimaremare | 1:f448c12d2c5b | 49 | // converted voltage. |
mariosimaremare | 1:f448c12d2c5b | 50 | this->_voltage = |
mariosimaremare | 1:f448c12d2c5b | 51 | this->_reading * |
mariosimaremare | 1:f448c12d2c5b | 52 | this->_const_voltage * |
mariosimaremare | 1:f448c12d2c5b | 53 | this->_const_converter; |
mariosimaremare | 1:f448c12d2c5b | 54 | // the salinity value. |
mariosimaremare | 1:f448c12d2c5b | 55 | this->_salinity = |
mariosimaremare | 1:f448c12d2c5b | 56 | this->_voltage * |
mariosimaremare | 1:f448c12d2c5b | 57 | this->_const_multiplier; |
mariosimaremare | 2:ed17e258da0d | 58 | |
mariosimaremare | 2:ed17e258da0d | 59 | this->_status = 0.0; |
mariosimaremare | 3:7c648d1d8802 | 60 | this->_strStatus = "OK"; |
mariosimaremare | 2:ed17e258da0d | 61 | if(this->_salinity < this->_const_lower_boundary){ |
mariosimaremare | 2:ed17e258da0d | 62 | this->_status = this->_salinity - this->_const_lower_boundary; |
mariosimaremare | 3:7c648d1d8802 | 63 | this->_strStatus = "LW"; |
mariosimaremare | 2:ed17e258da0d | 64 | } else if(this->_salinity > this->_const_upper_boundary){ |
mariosimaremare | 2:ed17e258da0d | 65 | this->_status = this->_const_lower_boundary - this->_salinity; |
mariosimaremare | 3:7c648d1d8802 | 66 | this->_strStatus = "HI"; |
mariosimaremare | 2:ed17e258da0d | 67 | } |
mariosimaremare | 2:ed17e258da0d | 68 | } |
mariosimaremare | 1:f448c12d2c5b | 69 | |
mariosimaremare | 2:ed17e258da0d | 70 | double SalinitySensor::getReading() |
mariosimaremare | 2:ed17e258da0d | 71 | { |
mariosimaremare | 1:f448c12d2c5b | 72 | return(this->_reading); |
mariosimaremare | 0:ad9362f18797 | 73 | } |
mariosimaremare | 0:ad9362f18797 | 74 | |
mariosimaremare | 0:ad9362f18797 | 75 | double SalinitySensor::getVoltage() |
mariosimaremare | 0:ad9362f18797 | 76 | { |
mariosimaremare | 1:f448c12d2c5b | 77 | return(this->_voltage); |
mariosimaremare | 0:ad9362f18797 | 78 | } |
mariosimaremare | 0:ad9362f18797 | 79 | |
mariosimaremare | 0:ad9362f18797 | 80 | double SalinitySensor::getSalinity() |
mariosimaremare | 0:ad9362f18797 | 81 | { |
mariosimaremare | 1:f448c12d2c5b | 82 | return(this->_salinity); |
mariosimaremare | 0:ad9362f18797 | 83 | } |
mariosimaremare | 2:ed17e258da0d | 84 | |
mariosimaremare | 2:ed17e258da0d | 85 | double SalinitySensor::getStatus() |
mariosimaremare | 2:ed17e258da0d | 86 | { |
mariosimaremare | 2:ed17e258da0d | 87 | return(this->_status); |
mariosimaremare | 2:ed17e258da0d | 88 | } |
mariosimaremare | 3:7c648d1d8802 | 89 | |
mariosimaremare | 3:7c648d1d8802 | 90 | char* SalinitySensor::getStrStatus() |
mariosimaremare | 3:7c648d1d8802 | 91 | { |
mariosimaremare | 3:7c648d1d8802 | 92 | return(this->_strStatus); |
mariosimaremare | 3:7c648d1d8802 | 93 | } |