Fixed mq-3 example

This commit is contained in:
miguel5612
2020-03-26 10:23:51 -05:00
parent a03ceab3dc
commit aa11343a97
3 changed files with 34 additions and 14 deletions

View File

@ -6,7 +6,13 @@ MQUnifiedsensor::MQUnifiedsensor(String Placa, int Voltage_Resolution, int pin,
this->_placa = Placa;
this-> _VOLT_RESOLUTION = Voltage_Resolution;
}
MQUnifiedsensor::serialDebug(boolean onSetup)
MQUnifiedsensor::setA(double a) {
this->_a = a;
}
MQUnifiedsensor::setB(double b) {
this->_b = b;
}
MQUnifiedsensor::serialDebug(boolean onSetup, String regressionMethod)
{
if(onSetup)
{
@ -40,6 +46,7 @@ MQUnifiedsensor::serialDebug(boolean onSetup)
else
{
String eq = "";
if(regression == "Linear") eq = "ratio*a + b"
if(regression == "Exponential") eq = "a*ratio^b"
Serial.println("|" + _adc + "|" + "v = ADC*" + _VOLT_RESOLUTION + "/1024" + "|" + _sensor_volt + "|" + "RS = ((" + _VOLT_RESOLUTION + "*RL)/Voltage) - RL" + "|" + _RS_Calc + "|" + "Ratio = RS/R0" + "|" + _ratio + "|" + eq + "|" + _PPM);
}
@ -57,15 +64,15 @@ void MQUnifiedsensor::init()
{
pinMode(_pin, INPUT);
}
float MQUnifiedsensor::readSensor(String nameLectureRequeired)
float MQUnifiedsensor::readSensor(String regressionMethod)
{
setSensorCharacteristics(nameLectureRequeired, print); //In this function update _a and _b
//More explained in: https://jayconsystems.com/blog/understanding-a-gas-sensor
_RS_Calc = ((_VOLT_RESOLUTION*_RLValue)/_sensor_volt)-_RLValue; //Get value of RS in a gas
if(_RS_Calc < 0) _RS_Calc = 0; //No negative values accepted.
_ratio = _RS_Calc / this->_R0; // Get ratio RS_gas/RS_air
if(_ratio <= 0 || _ratio>100) _ratio = 0.01; //No negative values accepted or upper datasheet recomendation.
_PPM= _a*pow(_ratio, _b);
if(regressionMethod == "Exponential") _PPM= _a*pow(_ratio, _b);
if(regressionMethod == "Linear") _PPM= _a*_ratio + _b);
if(_PPM < 0) _PPM = 0; //No negative values accepted or upper datasheet recomendation.
if(_PPM > 10000) _PPM = 9999; //No negative values accepted or upper datasheet recomendation.
return _PPM;

View File

@ -20,12 +20,14 @@ class MQUnifiedsensor
void update();
void setR0(double R0 = 10);
void setRL(double RL = 10);
void setA(double a);
void setB(double b);
void setVoltResolution(double voltage_resolution = 5);
void serialDebug(boolean onSetup = false); //Show on serial port information about sensor
//user functions
float calibrate(boolean print = false);
float readSensor(<String regressionMethod = "Exponential", float _a, float _b);
float calibrate(boolean print = false, String regressionMethod = "Exponential");
float readSensor(String regressionMethod = "Exponential", float _a, float _b);
//get function for info
double getR0();
@ -43,7 +45,7 @@ class MQUnifiedsensor
byte _VOLT_RESOLUTION = 5.0; // if 3.3v use 3.3
byte _ratioInCleanAir, _sensor_volt;
byte _RLValue = 10; //Value in KiloOhms
double _adc;
double _adc, _a, _b;
float _R0, RS_air, _ratio, _PPM, _RS_Calc;
};