library for SEN0169 ph probe from DFRobot

Dependents:   2022_TICE_Electrolyse

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers lib_SEN0169.cpp Source File

lib_SEN0169.cpp

00001 #include "lib_SEN0169.h"
00002 
00003 
00004 SEN0169::SEN0169(PinName data_pin) : _datapin(data_pin)
00005 {
00006     this->_phValue  = 7.0f;
00007     this->_voltage  = 1500.0f;
00008 }
00009 
00010 void SEN0169::calibration(uint8_t ph_tampon)
00011 {
00012     float avg_voltage, buf_[20], temp_;
00013 
00014     for(int i=0; i<20; i++) {
00015         buf_[i]=_datapin.read();
00016         ThisThread::sleep_for(10ms);
00017     }
00018     for(int i=0; i<19; i++) {
00019         for(int j=i+1; j<20; j++) {
00020             if(buf_[i]>buf_[j]) {
00021                 temp_=buf_[i];
00022                 buf_[i]=buf_[j];
00023                 buf_[j]=temp_;
00024             }
00025         }
00026     }
00027     avg_voltage = 0.0f;
00028     for(int i=5; i<15; i++) avg_voltage += buf_[i];
00029     avg_voltage = avg_voltage*3.3/10.0f;
00030 
00031 
00032     switch (ph_tampon) {
00033         case 4 :
00034             this->_params[ACIDE] = avg_voltage;
00035             break;
00036 
00037         case 7 :
00038             this->_params[NEUTRE] = avg_voltage;
00039             break;
00040 
00041         case 10 :
00042             this->_params[BASIC] = avg_voltage;
00043             break;
00044 
00045         default :
00046             break;
00047     }
00048 }
00049 
00050 float*   SEN0169::get_parameters(void)
00051 {
00052     return _params;
00053 }
00054 
00055 float SEN0169::read_voltage(void)
00056 {
00057     this->_voltage = _datapin.read()*3.3;
00058     return _voltage;
00059 }
00060 
00061 float SEN0169::read_ph(void)
00062 {
00063     float avg_voltage, buf_[10], temp_;
00064 
00065     for(int i=0; i<10; i++) {           // take 10 samples of voltage
00066         buf_[i]=_datapin.read();
00067         ThisThread::sleep_for(10ms);
00068     }
00069     for(int i=0; i<9; i++) {            // sort the voltages from small to large
00070         for(int j=i+1; j<10; j++) {
00071             if(buf_[i]>buf_[j]) {
00072                 temp_=buf_[i];
00073                 buf_[i]=buf_[j];
00074                 buf_[j]=temp_;
00075             }
00076         }
00077     }
00078     avg_voltage = 0.0f;
00079     for(int i=2; i<8; i++) avg_voltage += buf_[i];      // supress the 2 smallest and 2 largest samples
00080     avg_voltage = avg_voltage*3.3/6.0f;
00081 
00082     this->_voltage = avg_voltage;
00083     this->_params[SLOPE] = ((3.0f*(4.0f*_params[ACIDE]+7.0f*_params[NEUTRE]+10.0f*_params[BASIC]))-(21.0f*(_params[ACIDE]+_params[NEUTRE]+_params[BASIC])))/54.0f;
00084     // Slope = [3*sum(xy)-sum(x)*sum(y)]/[3*sum(x²)-sum(x)²]
00085     this->_params[INTERCEPT] =  ((_params[ACIDE] + _params[NEUTRE] + _params[BASIC])/3.0f) - (_params[SLOPE] * 7.0f);
00086     this->_phValue = (avg_voltage*1000.0 - _params[INTERCEPT]) / _params[SLOPE];
00087 
00088     return _phValue;
00089 }