Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
SalinitySensor.cpp
00001 /* 00002 * G3: WATERPLAY 00003 */ 00004 00005 #include "SalinitySensor.h" 00006 #include "mbed.h" 00007 #include <algorithm> 00008 #include "Printer.h" 00009 00010 SalinitySensor::SalinitySensor( 00011 Printer &printer, 00012 PinName pin 00013 ): 00014 _printer(printer), 00015 _analog_in(pin), 00016 _reading(0.0), 00017 _voltage(0.0), 00018 _salinity(0.0), 00019 _status(0.0), 00020 _strStatus("OK"), 00021 _in_boundary(false), 00022 _in_danger(false) 00023 { 00024 this->reload(); 00025 } 00026 00027 void SalinitySensor::reload() 00028 { 00029 double readings[SAMPLING_NUMBER]; 00030 00031 for(int counter = 0; counter < SAMPLING_NUMBER; ++counter){ 00032 // the reading from sensor. 00033 readings[counter] = this->_analog_in.read(); 00034 } 00035 00036 sort(readings, readings + SAMPLING_NUMBER); 00037 00038 this->_reading = readings[SAMPLING_NUMBER / 2]; 00039 00040 // converted voltage. 00041 this->_voltage = 00042 this->_reading * 00043 VIN * 00044 CONVERTER; 00045 // the salinity value. 00046 this->_salinity = 00047 this->_voltage * 00048 MULTIPLIER; 00049 00050 00051 this->_in_boundary = false; 00052 if(this->_salinity > 0.25){ 00053 this->_salinity += VARIANCE; 00054 this->_in_boundary = true; 00055 } 00056 this->_status = 0.0; 00057 this->_strStatus = "OK"; 00058 if(this->_salinity < LOWER_BOUNDARY){ 00059 this->_status = this->_salinity - LOWER_BOUNDARY; 00060 this->_strStatus = "LW"; 00061 } else if(this->_salinity > UPPER_BOUNDARY){ 00062 this->_status = this->_salinity - UPPER_BOUNDARY; 00063 this->_strStatus = "HI"; 00064 } 00065 } 00066 00067 double SalinitySensor::getReading() 00068 { 00069 return(this->_reading); 00070 } 00071 00072 double SalinitySensor::getVoltage() 00073 { 00074 return(this->_voltage); 00075 } 00076 00077 double SalinitySensor::getSalinity() 00078 { 00079 return(this->_salinity); 00080 } 00081 00082 double SalinitySensor::getStatus() 00083 { 00084 return(this->_status); 00085 } 00086 00087 char* SalinitySensor::getStrStatus() 00088 { 00089 return(this->_strStatus); 00090 } 00091 00092 bool SalinitySensor::inBoundary() 00093 { 00094 return(this->_in_boundary); 00095 } 00096 00097 bool SalinitySensor::inDanger() 00098 { 00099 bool in_danger = false; 00100 if(this->_in_boundary && this->_salinity != 0.0){ 00101 in_danger = true; 00102 } 00103 return(in_danger); 00104 }
Generated on Tue Jul 12 2022 19:11:49 by
1.7.2