diff --git a/src/MQUnifiedsensor.cpp b/src/MQUnifiedsensor.cpp index 5ee921a..0e33764 100644 --- a/src/MQUnifiedsensor.cpp +++ b/src/MQUnifiedsensor.cpp @@ -1,6 +1,5 @@ #include "MQUnifiedsensor.h" - MQUnifiedsensor::MQUnifiedsensor(int pin, int type) { this->_pin = pin; @@ -67,11 +66,10 @@ //_MQ = MQ309; } } - -void MQUnifiedsensor::setR0(double R0) { - this->_R0 = R0; +void MQUnifiedsensor::inicializar() +{ + pinMode(_pin, INPUT); } - int MQUnifiedsensor::readSensor(String nameLectureRequeired, bool print) { setSensorCharacteristics(nameLectureRequeired); @@ -87,78 +85,92 @@ int MQUnifiedsensor::readSensor(String nameLectureRequeired, bool print) } return _PPM; } -void MQUnifiedsensor::inicializar() +String MQUnifiedsensor::getnameLecture() { - pinMode(_pin, INPUT); + return nameLecture[_lecturePosInArray]; } void MQUnifiedsensor::setSensorCharacteristics(String nameLectureRequeired) { - int wantedpos = 0; if(nameLectureRequeired == "") { if(_type == 2) { - wantedpos = defaultMQ2; + _lecturePosInArray = defaultMQ2; } else if(_type == 3) { - wantedpos = defaultMQ3; + _lecturePosInArray = defaultMQ3; } else if(_type == 4) { - wantedpos = defaultMQ4; + _lecturePosInArray = defaultMQ4; } else if(_type == 5) { - wantedpos = defaultMQ5; + _lecturePosInArray = defaultMQ5; } else if(_type == 6) { - wantedpos = defaultMQ6; + _lecturePosInArray = defaultMQ6; } else if(_type == 7) { - wantedpos = defaultMQ7; + _lecturePosInArray = defaultMQ7; } else if(_type == 8) { - wantedpos = defaultMQ8; + _lecturePosInArray = defaultMQ8; } else if(_type == 9) { - wantedpos = defaultMQ9; + _lecturePosInArray = defaultMQ9; } else if(_type == 131) { - wantedpos = defaultMQ131; + _lecturePosInArray = defaultMQ131; } else if(_type == 135) { - wantedpos = defaultMQ135; + _lecturePosInArray = defaultMQ135; } else if(_type == 303) { - wantedpos = defaultMQ303; + _lecturePosInArray = defaultMQ303; } else if(_type == 309) { - wantedpos = defaultMQ309; + _lecturePosInArray = defaultMQ309; } } else { for (int i=0; igetVoltage(); + double RS_gas; //Define variable for sensor resistance + double ratio; //Define variable for ratio + RS_gas = ((5.0 * 10.0) / sensor_volt) - 10.0; //Get value of RS in a gas + + ratio = RS_gas / this->_R0; // Get ratio RS_gas/RS_air + + double ppm_log = (log10(ratio) - b) / m; //Get ppm value in linear scale according to the the ratio value + double ppm = pow(10, ppm_log); //Convert ppm value to log scale + return floor(ppm); +} double MQUnifiedsensor::calibrate() { float sensor_volt; //Define variable for sensor voltage float RS_air; //Define variable for sensor resistance @@ -169,7 +181,6 @@ double MQUnifiedsensor::calibrate() { R0 = RS_air / 4.4; //Calculate R0 return R0; } - double MQUnifiedsensor::getVoltage() { double avg = 0.0; for (int i = 0; i < retries; i ++) { @@ -181,20 +192,6 @@ double MQUnifiedsensor::getVoltage() { return voltage; } - - /** - * Returns the PPM concentration - */ - int MQUnifiedsensor::readPPM(int m, int b) { - double sensor_volt = this->getVoltage(); - double RS_gas; //Define variable for sensor resistance - double ratio; //Define variable for ratio - - RS_gas = ((5.0 * 10.0) / sensor_volt) - 10.0; //Get value of RS in a gas - - ratio = RS_gas / this->_R0; // Get ratio RS_gas/RS_air - - double ppm_log = (log10(ratio) - b) / m; //Get ppm value in linear scale according to the the ratio value - double ppm = pow(10, ppm_log); //Convert ppm value to log scale - return floor(ppm); - } \ No newline at end of file +void MQUnifiedsensor::setR0(double R0) { + this->_R0 = R0; +} \ No newline at end of file diff --git a/src/MQUnifiedsensor.h b/src/MQUnifiedsensor.h index 4c7583a..580e65f 100644 --- a/src/MQUnifiedsensor.h +++ b/src/MQUnifiedsensor.h @@ -13,24 +13,17 @@ class MQUnifiedsensor * Constructor */ MQUnifiedsensor(int pin, int type); + void inicializar(); void setR0(double R0); void setSensorCharacteristics(String nameLectureReqeuired); int readSensor(String nameLectureRequeired = "", bool print = false); int readPPM(int m, int b); - void inicializar(); - - /** - * Calibrates the start point of 400 - */ - double calibrate(); - - /** - * Returns the voltage - */ - double getVoltage(); + double calibrate(); + double getVoltage(); + String getnameLecture(); /** * Returns the PPM concentration MQ-2 - Methane, Butane, LPG, smoke @@ -52,9 +45,9 @@ class MQUnifiedsensor /* Index definitions and header names*/ - String nameLecture[lecturesAvailable] = {"H2","LPG","CH4","CO","Alcohol","Propane","Benzine","Hexane","Smoke","Nox","CL2","O3","CO2","Tolueno","NH4","Acetona","Iso-butano","Hydrogeno","Ethanol"}; - int indexSlopeLectures[lecturesAvailable] ={0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36}; - int indexBPointLectures[lecturesAvailable] ={1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37}; + const String nameLecture[lecturesAvailable] = {"H2","LPG","CH4","CO","Alcohol","Propane","Benzine","Hexane","Smoke","Nox","CL2","O3","CO2","Tolueno","NH4","Acetona","Iso-butano","Hydrogeno","Ethanol"}; + const int indexSlopeLectures[lecturesAvailable] ={0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36}; + const int indexBPointLectures[lecturesAvailable] ={1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37}; const int defaultMQ2 = 4; // LPG const int defaultMQ3 = 10; // Alcohol const int defaultMQ4 = 6; // CH4 @@ -69,22 +62,22 @@ class MQUnifiedsensor const int defaultMQ309 = 8; //CO /* Value of m (Slope) and b (Cut on x axis) points */ - double MQ2[38] = {-2.2459,2.9845,-2.2879,2.7901,-2.6208,3.6075,-3.1157,4.5134,-2.7028,3.5595,-2.2879,2.7901,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; - double MQ3[38] = {0,0,-3.1851,4.7048,-17.531,28.785,-4.339,6.4432,-1.435,0.4103,0,0,-2.7009,0.632,-2.7268,3.6299,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; - double MQ4[38] = {0,0,-2.5818,3.6303,0.9873,2.6386,-5.5945,5.6693,-11.89,9.0375,0,0,0,0,0,0,-11.189,9.0375,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; - double MQ5[38] = {-4.368,2.9667,-2.5723,1.8943,-2.4438,2.3044,-4.8188,5.2023,-4.419,4.8044,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; - double MQ6[38] = {-3.6775,5.0286,-1.6567,2.8775,-1,3.301,-12.791,14.523,-5.8057,7.5292,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; - double MQ7[38] = {-1.329,1.8864,-7.8626,9.1056,-5.4878,8.8387,-1.4065,2.0162,-6.3219,9.924,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; - double MQ8[38] = {-0.7152,2.9891,-3.419,7.3513,-7.5609,15.243,-7.0753,15.396,-1.7459,4.7575,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; - double MQ9[38] = {0,0,-2.2535,2.9855,-1.6012,3.1476,-1.749,2.827,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; - double MQ131[38] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-2.7245,3.3004,-1.0333,1.7117,-1.2037,1.6455,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; - double MQ135[38] = {0,0,0,0,0,0,-2.7268,2.301,-2.8608,1.8627,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-3.2819,1.9903,-5.7015,1.1612,-2.2119,2.0473,-5.9682,1.0175,0,0,0,0,0,0}; - double MQ303A[38] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-2.3543,1.144,-2.4338,0.7558,-2.5597,0.4436}; - double MQ309A[38] = {-2.1311,3.0886,0,0,-1.6554,2.985,-4.7623,6.7413,-3.7686,5.6744,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; + const double MQ2[38] = {-2.2459,2.9845,-2.2879,2.7901,-2.6208,3.6075,-3.1157,4.5134,-2.7028,3.5595,-2.2879,2.7901,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; + const double MQ3[38] = {0,0,-3.1851,4.7048,-17.531,28.785,-4.339,6.4432,-1.435,0.4103,0,0,-2.7009,0.632,-2.7268,3.6299,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; + const double MQ4[38] = {0,0,-2.5818,3.6303,0.9873,2.6386,-5.5945,5.6693,-11.89,9.0375,0,0,0,0,0,0,-11.189,9.0375,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; + const double MQ5[38] = {-4.368,2.9667,-2.5723,1.8943,-2.4438,2.3044,-4.8188,5.2023,-4.419,4.8044,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; + const double MQ6[38] = {-3.6775,5.0286,-1.6567,2.8775,-1,3.301,-12.791,14.523,-5.8057,7.5292,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; + const double MQ7[38] = {-1.329,1.8864,-7.8626,9.1056,-5.4878,8.8387,-1.4065,2.0162,-6.3219,9.924,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; + const double MQ8[38] = {-0.7152,2.9891,-3.419,7.3513,-7.5609,15.243,-7.0753,15.396,-1.7459,4.7575,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; + const double MQ9[38] = {0,0,-2.2535,2.9855,-1.6012,3.1476,-1.749,2.827,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; + const double MQ131[38] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-2.7245,3.3004,-1.0333,1.7117,-1.2037,1.6455,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; + const double MQ135[38] = {0,0,0,0,0,0,-2.7268,2.301,-2.8608,1.8627,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-3.2819,1.9903,-5.7015,1.1612,-2.2119,2.0473,-5.9682,1.0175,0,0,0,0,0,0}; + const double MQ303A[38] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-2.3543,1.144,-2.4338,0.7558,-2.5597,0.4436}; + const double MQ309A[38] = {-2.1311,3.0886,0,0,-1.6554,2.985,-4.7623,6.7413,-3.7686,5.6744,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; private: - int _pin, _type, _PPM; + int _pin, _type, _PPM, _lecturePosInArray; double _R0, _m, _b, _MQ[38]; const float VOLT_RESOLUTION = 5.0; // if 3.3v use 3.3 const int ADC_RESOLUTION = 10; // for 10bit analog to digital converter.