mirror of
https://github.com/miguel5612/MQSensorsLib.git
synced 2025-03-15 05:17:30 +03:00
Updated regression model selection
This commit is contained in:
parent
c67ceab56a
commit
510e8dac53
@ -37,7 +37,7 @@ void setup() {
|
||||
Serial.begin(9600); //Init serial port
|
||||
|
||||
//Set math model to calculate the PPM concentration and the value of constants
|
||||
MQ3.setRegressionMethod("Exponential"); //_PPM = a*ratio^b
|
||||
MQ3.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ3.setA(0.3934); MQ3.setB(-1.504); // Configurate the ecuation values to get Alcohol concentration
|
||||
/*
|
||||
Exponential regression:
|
||||
|
@ -16,14 +16,19 @@
|
||||
#define Voltage_Resolution 5
|
||||
#define type "MQ-Board"
|
||||
#define ADC_Bit_Resolution 10 // For arduino UNO/MEGA/NANO
|
||||
// On this program pin value doesn't matter
|
||||
#define pin2 A2 //Analog input 2 of your arduino
|
||||
#define pin3 A3 //Analog input 3 of your arduino
|
||||
#define pin4 A4 //Analog input 4 of your arduino
|
||||
#define pin5 A5 //Analog input 5 of your arduino
|
||||
#define pin6 A6 //Analog input 6 of your arduino
|
||||
#define pin7 A7 //Analog input 7 of your arduino
|
||||
#define pin8 A8 //Analog input 8 of your arduino
|
||||
#define pin9 A9 //Analog input 9 of your arduino
|
||||
#define pin8 A6 //Analog input 6 of your arduino
|
||||
#define pin9 A7 //Analog input 7 of your arduino
|
||||
#define pin10 A6 //Analog input 6 of your arduino
|
||||
#define pin11 A7 //Analog input 7 of your arduino
|
||||
#define pin12 A6 //Analog input 6 of your arduino
|
||||
#define pin13 A7 //Analog input 7 of your arduino
|
||||
//#define calibration_button 13 //Pin to calibrate your sensor
|
||||
|
||||
//Declare Sensor
|
||||
@ -35,9 +40,15 @@ MQUnifiedsensor MQ6(placa, Voltage_Resolution, ADC_Bit_Resolution, pin6, type);
|
||||
MQUnifiedsensor MQ7(placa, Voltage_Resolution, ADC_Bit_Resolution, pin7, type);
|
||||
MQUnifiedsensor MQ8(placa, Voltage_Resolution, ADC_Bit_Resolution, pin8, type);
|
||||
MQUnifiedsensor MQ9(placa, Voltage_Resolution, ADC_Bit_Resolution, pin9, type);
|
||||
MQUnifiedsensor MQ131(placa, Voltage_Resolution, ADC_Bit_Resolution, pin10, type);
|
||||
MQUnifiedsensor MQ135(placa, Voltage_Resolution, ADC_Bit_Resolution, pin11, type);
|
||||
MQUnifiedsensor MQ303(placa, Voltage_Resolution, ADC_Bit_Resolution, pin12, type);
|
||||
MQUnifiedsensor MQ309(placa, Voltage_Resolution, ADC_Bit_Resolution, pin13, type);
|
||||
|
||||
int ratio[10] = {};
|
||||
int expectedValue[10] = {};
|
||||
double ratio[4] = {};
|
||||
double expectedValue[4] = {};
|
||||
double calculatedValues[4] = {};
|
||||
double error[4] = {};
|
||||
|
||||
void setup()
|
||||
{
|
||||
@ -45,73 +56,367 @@ void setup()
|
||||
Serial.begin(9600);
|
||||
//init the sensor
|
||||
MQ2.init();
|
||||
MQ2.setRegressionMethod("Exponential"); //_PPM = a*ratio^b
|
||||
MQ2.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ2.setA(574.25); MQ2.setB(-2.222); // Configurate the ecuation values to get LPG concentration
|
||||
MQ2.setR0(9.659574468);
|
||||
|
||||
MQ3.init();
|
||||
MQ3.setRegressionMethod("Exponential"); //_PPM = a*ratio^b
|
||||
MQ3.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ3.setA(4.8387); MQ3.setB(-2.68); // Configurate the ecuation values to get Benzene concentration
|
||||
MQ3.setR0(3.86018237);
|
||||
|
||||
MQ4.init();
|
||||
MQ4.setRegressionMethod("Exponential"); //_PPM = a*ratio^b
|
||||
MQ4.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ4.setA(1012.7); MQ4.setB(-2.786); // Configurate the ecuation values to get CH4 concentration
|
||||
MQ4.setR0(3.86018237);
|
||||
|
||||
MQ5.init();
|
||||
MQ5.setRegressionMethod("Exponential"); //_PPM = a*ratio^b
|
||||
MQ5.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ5.setA(1163.8); MQ5.setB(-3.874); // Configurate the ecuation values to get H2 concentration
|
||||
MQ5.setR0(71.100304);
|
||||
|
||||
MQ6.init();
|
||||
MQ6.setRegressionMethod("Exponential"); //_PPM = a*ratio^b
|
||||
MQ6.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ6.setA(2127.2); MQ6.setB(-2.526); // Configurate the ecuation values to get CH4 concentration
|
||||
MQ6.setR0(13.4285714);
|
||||
|
||||
MQ7.init();
|
||||
MQ7.setRegressionMethod("Exponential"); //_PPM = a*ratio^b
|
||||
MQ7.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ7.setA(99.042); MQ7.setB(-1.518); // Configurate the ecuation values to get CO concentration
|
||||
MQ7.setR0(4);
|
||||
|
||||
MQ8.init();
|
||||
MQ8.setRegressionMethod("Exponential"); //_PPM = a*ratio^b
|
||||
MQ8.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ8.setA(976.97); MQ8.setB(-0.688); // Configurate the ecuation values to get H2 concentration
|
||||
MQ8.setR0(1);
|
||||
|
||||
MQ9.init();
|
||||
MQ9.setRegressionMethod("Exponential"); //_PPM = a*ratio^b
|
||||
MQ9.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ9.setA(1000.5); MQ9.setB(-2.186); // Configurate the ecuation values to get LPG concentration
|
||||
MQ9.setR0(9.42857143);
|
||||
|
||||
MQ131.init();
|
||||
MQ131.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ131.setA(23.943); MQ131.setB(-1.11); // Configurate the ecuation values to get O3 concentration
|
||||
MQ131.setR0(385.40);
|
||||
|
||||
MQ135.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ135.setA(102.2); MQ135.setB(-2.473); // Configurate the ecuation values to get NH4 concentration
|
||||
MQ135.setR0(76.63);
|
||||
MQ135.init();
|
||||
|
||||
MQ303.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ303.setA(6.2144); MQ303.setB(-2.894); // Configurate the ecuation values to get Isobutano concentration
|
||||
MQ303.setR0(10);
|
||||
MQ303.init();
|
||||
|
||||
MQ309.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ309.setA(1000000); MQ309.setB(-4.01); // Configurate the ecuation values to get CO concentration
|
||||
MQ309.setR0(10);
|
||||
MQ309.init();
|
||||
|
||||
//Print in serial monitor
|
||||
Serial.print("MQ2 to MQ9 - test program");
|
||||
Serial.println("MQ2 to MQ9 - test program");
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
// Testing MQ2 LPG
|
||||
ratio = {};
|
||||
expectedValue = {};
|
||||
|
||||
Serial.println("**********************************************************************************************************************************");
|
||||
ratio[0] = 1.8; ratio[1] = 1.05; ratio[2] = 0.8; ratio[3] = 0.5;
|
||||
expectedValue[0] = 200; expectedValue[1] = 500; expectedValue[2] = 1000; expectedValue[3] = 3000;
|
||||
calculatedValues[0] = MQ2.validateEcuation(ratio[0]);
|
||||
calculatedValues[1] = MQ2.validateEcuation(ratio[1]);
|
||||
calculatedValues[2] = MQ2.validateEcuation(ratio[2]);
|
||||
calculatedValues[3] = MQ2.validateEcuation(ratio[3]);
|
||||
error[0] = calculatePercentualError(expectedValue[0], calculatedValues[0]);
|
||||
error[1] = calculatePercentualError(expectedValue[1], calculatedValues[1]);
|
||||
error[2] = calculatePercentualError(expectedValue[2], calculatedValues[2]);
|
||||
error[3] = calculatePercentualError(expectedValue[3], calculatedValues[3]);
|
||||
Serial.print("Error(%) MQ2: ");
|
||||
Serial.print(error[0]); Serial.print(" ");
|
||||
Serial.print(error[1]); Serial.print(" ");
|
||||
Serial.print(error[2]); Serial.print(" ");
|
||||
Serial.print(error[3]); Serial.print(" | Calculated(PPM): ");
|
||||
Serial.print(calculatedValues[0]); Serial.print(" ");
|
||||
Serial.print(calculatedValues[1]); Serial.print(" ");
|
||||
Serial.print(calculatedValues[2]); Serial.print(" ");
|
||||
Serial.print(calculatedValues[3]); Serial.print(" | Expected(PPM): ");
|
||||
Serial.print(expectedValue[0]); Serial.print(" ");
|
||||
Serial.print(expectedValue[1]); Serial.print(" ");
|
||||
Serial.print(expectedValue[2]); Serial.print(" ");
|
||||
Serial.print(expectedValue[3]); Serial.println(" | ");
|
||||
// Testing MQ3 Benzene
|
||||
|
||||
ratio[0] = 4; ratio[1] = 3; ratio[2] = 1; ratio[3] = 0.8;
|
||||
expectedValue[0] = 0.1; expectedValue[1] = 0.21; expectedValue[2] = 4; expectedValue[3] = 10;
|
||||
calculatedValues[0] = MQ3.validateEcuation(ratio[0]);
|
||||
calculatedValues[1] = MQ3.validateEcuation(ratio[1]);
|
||||
calculatedValues[2] = MQ3.validateEcuation(ratio[2]);
|
||||
calculatedValues[3] = MQ3.validateEcuation(ratio[3]);
|
||||
error[0] = calculatePercentualError(expectedValue[0], calculatedValues[0]);
|
||||
error[1] = calculatePercentualError(expectedValue[1], calculatedValues[1]);
|
||||
error[2] = calculatePercentualError(expectedValue[2], calculatedValues[2]);
|
||||
error[3] = calculatePercentualError(expectedValue[3], calculatedValues[3]);
|
||||
Serial.print("Error(%) MQ3: ");
|
||||
Serial.print(error[0]); Serial.print(" ");
|
||||
Serial.print(error[1]); Serial.print(" ");
|
||||
Serial.print(error[2]); Serial.print(" ");
|
||||
Serial.print(error[3]); Serial.print(" | Calculated(PPM): ");
|
||||
Serial.print(calculatedValues[0]); Serial.print(" ");
|
||||
Serial.print(calculatedValues[1]); Serial.print(" ");
|
||||
Serial.print(calculatedValues[2]); Serial.print(" ");
|
||||
Serial.print(calculatedValues[3]); Serial.print(" | Expected(PPM): ");
|
||||
Serial.print(expectedValue[0]); Serial.print(" ");
|
||||
Serial.print(expectedValue[1]); Serial.print(" ");
|
||||
Serial.print(expectedValue[2]); Serial.print(" ");
|
||||
Serial.print(expectedValue[3]); Serial.println(" | ");
|
||||
// Testing MQ4 CH4
|
||||
|
||||
ratio[0] = 1.9; ratio[1] = 1; ratio[2] = 0.6; ratio[3] = 0.42;
|
||||
expectedValue[0] = 200; expectedValue[1] = 1000; expectedValue[2] = 5000; expectedValue[3] = 10000;
|
||||
calculatedValues[0] = MQ4.validateEcuation(ratio[0]);
|
||||
calculatedValues[1] = MQ4.validateEcuation(ratio[1]);
|
||||
calculatedValues[2] = MQ4.validateEcuation(ratio[2]);
|
||||
calculatedValues[3] = MQ4.validateEcuation(ratio[3]);
|
||||
error[0] = calculatePercentualError(expectedValue[0], calculatedValues[0]);
|
||||
error[1] = calculatePercentualError(expectedValue[1], calculatedValues[1]);
|
||||
error[2] = calculatePercentualError(expectedValue[2], calculatedValues[2]);
|
||||
error[3] = calculatePercentualError(expectedValue[3], calculatedValues[3]);
|
||||
Serial.print("Error(%) MQ4: ");
|
||||
Serial.print(error[0]); Serial.print(" ");
|
||||
Serial.print(error[1]); Serial.print(" ");
|
||||
Serial.print(error[2]); Serial.print(" ");
|
||||
Serial.print(error[3]); Serial.print(" | Calculated(PPM): ");
|
||||
Serial.print(calculatedValues[0]); Serial.print(" ");
|
||||
Serial.print(calculatedValues[1]); Serial.print(" ");
|
||||
Serial.print(calculatedValues[2]); Serial.print(" ");
|
||||
Serial.print(calculatedValues[3]); Serial.print(" | Expected(PPM): ");
|
||||
Serial.print(expectedValue[0]); Serial.print(" ");
|
||||
Serial.print(expectedValue[1]); Serial.print(" ");
|
||||
Serial.print(expectedValue[2]); Serial.print(" ");
|
||||
Serial.print(expectedValue[3]); Serial.println(" | ");
|
||||
// Testing MQ5 H2
|
||||
|
||||
ratio[0] = 1.8; ratio[1] = 1.3; ratio[2] = 0.8; ratio[3] = 0.68;
|
||||
expectedValue[0] = 200; expectedValue[1] = 500; expectedValue[2] = 3000; expectedValue[3] = 10000;
|
||||
calculatedValues[0] = MQ5.validateEcuation(ratio[0]);
|
||||
calculatedValues[1] = MQ5.validateEcuation(ratio[1]);
|
||||
calculatedValues[2] = MQ5.validateEcuation(ratio[2]);
|
||||
calculatedValues[3] = MQ5.validateEcuation(ratio[3]);
|
||||
error[0] = calculatePercentualError(expectedValue[0], calculatedValues[0]);
|
||||
error[1] = calculatePercentualError(expectedValue[1], calculatedValues[1]);
|
||||
error[2] = calculatePercentualError(expectedValue[2], calculatedValues[2]);
|
||||
error[3] = calculatePercentualError(expectedValue[3], calculatedValues[3]);
|
||||
Serial.print("Error(%) MQ5: ");
|
||||
Serial.print(error[0]); Serial.print(" ");
|
||||
Serial.print(error[1]); Serial.print(" ");
|
||||
Serial.print(error[2]); Serial.print(" ");
|
||||
Serial.print(error[3]); Serial.print(" | Calculated(PPM): ");
|
||||
Serial.print(calculatedValues[0]); Serial.print(" ");
|
||||
Serial.print(calculatedValues[1]); Serial.print(" ");
|
||||
Serial.print(calculatedValues[2]); Serial.print(" ");
|
||||
Serial.print(calculatedValues[3]); Serial.print(" | Expected(PPM): ");
|
||||
Serial.print(expectedValue[0]); Serial.print(" ");
|
||||
Serial.print(expectedValue[1]); Serial.print(" ");
|
||||
Serial.print(expectedValue[2]); Serial.print(" ");
|
||||
Serial.print(expectedValue[3]); Serial.println(" | ");
|
||||
// Testing MQ6 CH4
|
||||
|
||||
ratio[0] = 2.6; ratio[1] = 1.9; ratio[2] = 1; ratio[3] = 0.8;
|
||||
expectedValue[0] = 200; expectedValue[1] = 500; expectedValue[2] = 2000; expectedValue[3] = 5000;
|
||||
calculatedValues[0] = MQ6.validateEcuation(ratio[0]);
|
||||
calculatedValues[1] = MQ6.validateEcuation(ratio[1]);
|
||||
calculatedValues[2] = MQ6.validateEcuation(ratio[2]);
|
||||
calculatedValues[3] = MQ6.validateEcuation(ratio[3]);
|
||||
error[0] = calculatePercentualError(expectedValue[0], calculatedValues[0]);
|
||||
error[1] = calculatePercentualError(expectedValue[1], calculatedValues[1]);
|
||||
error[2] = calculatePercentualError(expectedValue[2], calculatedValues[2]);
|
||||
error[3] = calculatePercentualError(expectedValue[3], calculatedValues[3]);
|
||||
Serial.print("Error(%) MQ6: ");
|
||||
Serial.print(error[0]); Serial.print(" ");
|
||||
Serial.print(error[1]); Serial.print(" ");
|
||||
Serial.print(error[2]); Serial.print(" ");
|
||||
Serial.print(error[3]); Serial.print(" | Calculated(PPM): ");
|
||||
Serial.print(calculatedValues[0]); Serial.print(" ");
|
||||
Serial.print(calculatedValues[1]); Serial.print(" ");
|
||||
Serial.print(calculatedValues[2]); Serial.print(" ");
|
||||
Serial.print(calculatedValues[3]); Serial.print(" | Expected(PPM): ");
|
||||
Serial.print(expectedValue[0]); Serial.print(" ");
|
||||
Serial.print(expectedValue[1]); Serial.print(" ");
|
||||
Serial.print(expectedValue[2]); Serial.print(" ");
|
||||
Serial.print(expectedValue[3]); Serial.println(" | ");
|
||||
// Testing MQ7 CO
|
||||
|
||||
ratio[0] = 1.8; ratio[1] = 1; ratio[2] = 0.4; ratio[3] = 0.25;
|
||||
expectedValue[0] = 50; expectedValue[1] = 100; expectedValue[2] = 400; expectedValue[3] = 1000;
|
||||
calculatedValues[0] = MQ7.validateEcuation(ratio[0]);
|
||||
calculatedValues[1] = MQ7.validateEcuation(ratio[1]);
|
||||
calculatedValues[2] = MQ7.validateEcuation(ratio[2]);
|
||||
calculatedValues[3] = MQ7.validateEcuation(ratio[3]);
|
||||
error[0] = calculatePercentualError(expectedValue[0], calculatedValues[0]);
|
||||
error[1] = calculatePercentualError(expectedValue[1], calculatedValues[1]);
|
||||
error[2] = calculatePercentualError(expectedValue[2], calculatedValues[2]);
|
||||
error[3] = calculatePercentualError(expectedValue[3], calculatedValues[3]);
|
||||
Serial.print("Error(%) MQ7: ");
|
||||
Serial.print(error[0]); Serial.print(" ");
|
||||
Serial.print(error[1]); Serial.print(" ");
|
||||
Serial.print(error[2]); Serial.print(" ");
|
||||
Serial.print(error[3]); Serial.print(" | Calculated(PPM): ");
|
||||
Serial.print(calculatedValues[0]); Serial.print(" ");
|
||||
Serial.print(calculatedValues[1]); Serial.print(" ");
|
||||
Serial.print(calculatedValues[2]); Serial.print(" ");
|
||||
Serial.print(calculatedValues[3]); Serial.print(" | Expected(PPM): ");
|
||||
Serial.print(expectedValue[0]); Serial.print(" ");
|
||||
Serial.print(expectedValue[1]); Serial.print(" ");
|
||||
Serial.print(expectedValue[2]); Serial.print(" ");
|
||||
Serial.print(expectedValue[3]); Serial.println(" | ");
|
||||
// Testing MQ8 H2
|
||||
|
||||
ratio[0] = 2.6; ratio[1] = 1; ratio[2] = 0.21; ratio[3] = 0.03;
|
||||
expectedValue[0] = 500; expectedValue[1] = 1000; expectedValue[2] = 3000; expectedValue[3] = 10000;
|
||||
calculatedValues[0] = MQ8.validateEcuation(ratio[0]);
|
||||
calculatedValues[1] = MQ8.validateEcuation(ratio[1]);
|
||||
calculatedValues[2] = MQ8.validateEcuation(ratio[2]);
|
||||
calculatedValues[3] = MQ8.validateEcuation(ratio[3]);
|
||||
error[0] = calculatePercentualError(expectedValue[0], calculatedValues[0]);
|
||||
error[1] = calculatePercentualError(expectedValue[1], calculatedValues[1]);
|
||||
error[2] = calculatePercentualError(expectedValue[2], calculatedValues[2]);
|
||||
error[3] = calculatePercentualError(expectedValue[3], calculatedValues[3]);
|
||||
Serial.print("Error(%) MQ8: ");
|
||||
Serial.print(error[0]); Serial.print(" ");
|
||||
Serial.print(error[1]); Serial.print(" ");
|
||||
Serial.print(error[2]); Serial.print(" ");
|
||||
Serial.print(error[3]); Serial.print(" | Calculated(PPM): ");
|
||||
Serial.print(calculatedValues[0]); Serial.print(" ");
|
||||
Serial.print(calculatedValues[1]); Serial.print(" ");
|
||||
Serial.print(calculatedValues[2]); Serial.print(" ");
|
||||
Serial.print(calculatedValues[3]); Serial.print(" | Expected(PPM): ");
|
||||
Serial.print(expectedValue[0]); Serial.print(" ");
|
||||
Serial.print(expectedValue[1]); Serial.print(" ");
|
||||
Serial.print(expectedValue[2]); Serial.print(" ");
|
||||
Serial.print(expectedValue[3]); Serial.println(" | ");
|
||||
// Testing MQ9 LPG
|
||||
ratio[0] = 2; ratio[1] = 1.4; ratio[2] = 0.7; ratio[3] = 0.23;
|
||||
expectedValue[0] = 200; expectedValue[1] = 500; expectedValue[2] = 2000; expectedValue[3] = 10000;
|
||||
calculatedValues[0] = MQ9.validateEcuation(ratio[0]);
|
||||
calculatedValues[1] = MQ9.validateEcuation(ratio[1]);
|
||||
calculatedValues[2] = MQ9.validateEcuation(ratio[2]);
|
||||
calculatedValues[3] = MQ9.validateEcuation(ratio[3]);
|
||||
error[0] = calculatePercentualError(expectedValue[0], calculatedValues[0]);
|
||||
error[1] = calculatePercentualError(expectedValue[1], calculatedValues[1]);
|
||||
error[2] = calculatePercentualError(expectedValue[2], calculatedValues[2]);
|
||||
error[3] = calculatePercentualError(expectedValue[3], calculatedValues[3]);
|
||||
Serial.print("Error(%) MQ9: ");
|
||||
Serial.print(error[0]); Serial.print(" ");
|
||||
Serial.print(error[1]); Serial.print(" ");
|
||||
Serial.print(error[2]); Serial.print(" ");
|
||||
Serial.print(error[3]); Serial.print(" | Calculated(PPM): ");
|
||||
Serial.print(calculatedValues[0]); Serial.print(" ");
|
||||
Serial.print(calculatedValues[1]); Serial.print(" ");
|
||||
Serial.print(calculatedValues[2]); Serial.print(" ");
|
||||
Serial.print(calculatedValues[3]); Serial.print(" | Expected(PPM): ");
|
||||
Serial.print(expectedValue[0]); Serial.print(" ");
|
||||
Serial.print(expectedValue[1]); Serial.print(" ");
|
||||
Serial.print(expectedValue[2]); Serial.print(" ");
|
||||
Serial.print(expectedValue[3]); Serial.println(" | ");
|
||||
// Testing MQ131 O3
|
||||
ratio[0] = 5; ratio[1] = 2; ratio[2] = 0.7; ratio[3] = 0.5;
|
||||
expectedValue[0] = 5; expectedValue[1] = 20; expectedValue[2] = 70; expectedValue[3] = 100;
|
||||
calculatedValues[0] = MQ131.validateEcuation(ratio[0]);
|
||||
calculatedValues[1] = MQ131.validateEcuation(ratio[1]);
|
||||
calculatedValues[2] = MQ131.validateEcuation(ratio[2]);
|
||||
calculatedValues[3] = MQ131.validateEcuation(ratio[3]);
|
||||
error[0] = calculatePercentualError(expectedValue[0], calculatedValues[0]);
|
||||
error[1] = calculatePercentualError(expectedValue[1], calculatedValues[1]);
|
||||
error[2] = calculatePercentualError(expectedValue[2], calculatedValues[2]);
|
||||
error[3] = calculatePercentualError(expectedValue[3], calculatedValues[3]);
|
||||
Serial.print("Error(%) MQ131: ");
|
||||
Serial.print(error[0]); Serial.print(" ");
|
||||
Serial.print(error[1]); Serial.print(" ");
|
||||
Serial.print(error[2]); Serial.print(" ");
|
||||
Serial.print(error[3]); Serial.print(" | Calculated(PPM): ");
|
||||
Serial.print(calculatedValues[0]); Serial.print(" ");
|
||||
Serial.print(calculatedValues[1]); Serial.print(" ");
|
||||
Serial.print(calculatedValues[2]); Serial.print(" ");
|
||||
Serial.print(calculatedValues[3]); Serial.print(" | Expected(PPM): ");
|
||||
Serial.print(expectedValue[0]); Serial.print(" ");
|
||||
Serial.print(expectedValue[1]); Serial.print(" ");
|
||||
Serial.print(expectedValue[2]); Serial.print(" ");
|
||||
Serial.print(expectedValue[3]); Serial.println(" | ");
|
||||
// Testing MQ135 NH4
|
||||
ratio[0] = 2.6; ratio[1] = 1.9; ratio[2] = 1.1; ratio[3] = 0.75;
|
||||
expectedValue[0] = 10; expectedValue[1] = 20; expectedValue[2] = 100; expectedValue[3] = 200;
|
||||
calculatedValues[0] = MQ135.validateEcuation(ratio[0]);
|
||||
calculatedValues[1] = MQ135.validateEcuation(ratio[1]);
|
||||
calculatedValues[2] = MQ135.validateEcuation(ratio[2]);
|
||||
calculatedValues[3] = MQ135.validateEcuation(ratio[3]);
|
||||
error[0] = calculatePercentualError(expectedValue[0], calculatedValues[0]);
|
||||
error[1] = calculatePercentualError(expectedValue[1], calculatedValues[1]);
|
||||
error[2] = calculatePercentualError(expectedValue[2], calculatedValues[2]);
|
||||
error[3] = calculatePercentualError(expectedValue[3], calculatedValues[3]);
|
||||
Serial.print("Error(%) MQ135: ");
|
||||
Serial.print(error[0]); Serial.print(" ");
|
||||
Serial.print(error[1]); Serial.print(" ");
|
||||
Serial.print(error[2]); Serial.print(" ");
|
||||
Serial.print(error[3]); Serial.print(" | Calculated(PPM): ");
|
||||
Serial.print(calculatedValues[0]); Serial.print(" ");
|
||||
Serial.print(calculatedValues[1]); Serial.print(" ");
|
||||
Serial.print(calculatedValues[2]); Serial.print(" ");
|
||||
Serial.print(calculatedValues[3]); Serial.print(" | Expected(PPM): ");
|
||||
Serial.print(expectedValue[0]); Serial.print(" ");
|
||||
Serial.print(expectedValue[1]); Serial.print(" ");
|
||||
Serial.print(expectedValue[2]); Serial.print(" ");
|
||||
Serial.print(expectedValue[3]); Serial.println(" | ");
|
||||
// Testing MQ303 Isobutano
|
||||
ratio[0] = 0.4; ratio[1] = 0.3; ratio[2] = 0.19; ratio[3] = 0.08;
|
||||
expectedValue[0] = 100; expectedValue[1] = 300; expectedValue[2] = 1000; expectedValue[3] = 10000;
|
||||
calculatedValues[0] = MQ303.validateEcuation(ratio[0]);
|
||||
calculatedValues[1] = MQ303.validateEcuation(ratio[1]);
|
||||
calculatedValues[2] = MQ303.validateEcuation(ratio[2]);
|
||||
calculatedValues[3] = MQ303.validateEcuation(ratio[3]);
|
||||
error[0] = calculatePercentualError(expectedValue[0], calculatedValues[0]);
|
||||
error[1] = calculatePercentualError(expectedValue[1], calculatedValues[1]);
|
||||
error[2] = calculatePercentualError(expectedValue[2], calculatedValues[2]);
|
||||
error[3] = calculatePercentualError(expectedValue[3], calculatedValues[3]);
|
||||
Serial.print("Error(%) MQ303: ");
|
||||
Serial.print(error[0]); Serial.print(" ");
|
||||
Serial.print(error[1]); Serial.print(" ");
|
||||
Serial.print(error[2]); Serial.print(" ");
|
||||
Serial.print(error[3]); Serial.print(" | Calculated(PPM): ");
|
||||
Serial.print(calculatedValues[0]); Serial.print(" ");
|
||||
Serial.print(calculatedValues[1]); Serial.print(" ");
|
||||
Serial.print(calculatedValues[2]); Serial.print(" ");
|
||||
Serial.print(calculatedValues[3]); Serial.print(" | Expected(PPM): ");
|
||||
Serial.print(expectedValue[0]); Serial.print(" ");
|
||||
Serial.print(expectedValue[1]); Serial.print(" ");
|
||||
Serial.print(expectedValue[2]); Serial.print(" ");
|
||||
Serial.print(expectedValue[3]); Serial.println(" | ");
|
||||
// Testing MQ309 CO
|
||||
ratio[0] = 6; ratio[1] = 5; ratio[2] = 4.3; ratio[3] = 4;
|
||||
expectedValue[0] = 1000; expectedValue[1] = 3000; expectedValue[2] = 5000; expectedValue[3] = 7000;
|
||||
calculatedValues[0] = MQ309.validateEcuation(ratio[0]);
|
||||
calculatedValues[1] = MQ309.validateEcuation(ratio[1]);
|
||||
calculatedValues[2] = MQ309.validateEcuation(ratio[2]);
|
||||
calculatedValues[3] = MQ309.validateEcuation(ratio[3]);
|
||||
error[0] = calculatePercentualError(expectedValue[0], calculatedValues[0]);
|
||||
error[1] = calculatePercentualError(expectedValue[1], calculatedValues[1]);
|
||||
error[2] = calculatePercentualError(expectedValue[2], calculatedValues[2]);
|
||||
error[3] = calculatePercentualError(expectedValue[3], calculatedValues[3]);
|
||||
Serial.print("Error(%) MQ309: ");
|
||||
Serial.print(error[0]); Serial.print(" ");
|
||||
Serial.print(error[1]); Serial.print(" ");
|
||||
Serial.print(error[2]); Serial.print(" ");
|
||||
Serial.print(error[3]); Serial.print(" | Calculated(PPM): ");
|
||||
Serial.print(calculatedValues[0]); Serial.print(" ");
|
||||
Serial.print(calculatedValues[1]); Serial.print(" ");
|
||||
Serial.print(calculatedValues[2]); Serial.print(" ");
|
||||
Serial.print(calculatedValues[3]); Serial.print(" | Expected(PPM): ");
|
||||
Serial.print(expectedValue[0]); Serial.print(" ");
|
||||
Serial.print(expectedValue[1]); Serial.print(" ");
|
||||
Serial.print(expectedValue[2]); Serial.print(" ");
|
||||
Serial.print(expectedValue[3]); Serial.println(" | ");
|
||||
|
||||
|
||||
Serial.println("Test finalizado, por favor verifica que no existan errores >= 0.3 -> significa que no sea mayor al 30%");
|
||||
delay(60000);
|
||||
}
|
||||
|
||||
double calculatePercentualError(double expectedValue, double calculatedValue)
|
||||
{
|
||||
// Return the diference between two measures
|
||||
return abs(calculatedValue-expectedValue)/expectedValue
|
||||
return abs(calculatedValue-expectedValue)/expectedValue;
|
||||
}
|
||||
|
@ -46,7 +46,7 @@ unsigned long contador = 0;
|
||||
void setup() {
|
||||
//Init serial port
|
||||
Serial.begin(115200);
|
||||
MQ2.setRegressionMethod("Exponential"); //_PPM = a*ratio^b
|
||||
MQ2.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ2.setA(574.25); MQ2.setB(-2.222); // Configurate the ecuation values to get LPG concentration
|
||||
//init the sensor
|
||||
MQ2.init();
|
||||
|
@ -37,7 +37,7 @@ void setup() {
|
||||
Serial.begin(9600); //Init serial port
|
||||
|
||||
//Set math model to calculate the PPM concentration and the value of constants
|
||||
MQ3.setRegressionMethod("Exponential"); //_PPM = a*ratio^b
|
||||
MQ3.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ3.setA(4.8387); MQ3.setB(-2.68); // Configurate the ecuation values to get Benzene concentration
|
||||
/*
|
||||
Exponential regression:
|
||||
|
@ -36,7 +36,7 @@ void setup() {
|
||||
Serial.begin(9600); //Init serial port
|
||||
|
||||
//Set math model to calculate the PPM concentration and the value of constants
|
||||
MQ131.setRegressionMethod("Exponential"); //_PPM = a*ratio^b
|
||||
MQ131.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ131.setA(23.943); MQ131.setB(-1.11); // Configurate the ecuation values to get O3 concentration
|
||||
|
||||
/*
|
||||
|
@ -36,7 +36,7 @@ void setup() {
|
||||
Serial.begin(9600); //Init serial port
|
||||
|
||||
//Set math model to calculate the PPM concentration and the value of constants
|
||||
MQ135.setRegressionMethod("Exponential"); //_PPM = a*ratio^b
|
||||
MQ135.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ135.setA(102.2); MQ135.setB(-2.473); // Configurate the ecuation values to get NH4 concentration
|
||||
|
||||
/*
|
||||
|
@ -36,7 +36,7 @@ void setup() {
|
||||
Serial.begin(9600); //Init serial port
|
||||
|
||||
//Set math model to calculate the PPM concentration and the value of constants
|
||||
MQ2.setRegressionMethod("Exponential"); //_PPM = a*ratio^b
|
||||
MQ2.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ2.setA(574.25); MQ2.setB(-2.222); // Configurate the ecuation values to get LPG concentration
|
||||
/*
|
||||
Exponential regression:
|
||||
|
@ -36,7 +36,7 @@ void setup() {
|
||||
Serial.begin(9600); //Init serial port
|
||||
|
||||
//Set math model to calculate the PPM concentration and the value of constants
|
||||
MQ3.setRegressionMethod("Exponential"); //_PPM = a*ratio^b
|
||||
MQ3.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ3.setA(4.8387); MQ3.setB(-2.68); // Configurate the ecuation values to get Benzene concentration
|
||||
/*
|
||||
Exponential regression:
|
||||
|
@ -36,7 +36,7 @@ void setup() {
|
||||
Serial.begin(9600); //Init serial port
|
||||
|
||||
//Set math model to calculate the PPM concentration and the value of constants
|
||||
MQ303.setRegressionMethod("Exponential"); //_PPM = a*ratio^b
|
||||
MQ303.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ303.setA(6.2144); MQ303.setB(-2.894); // Configurate the ecuation values to get Isobutano concentration
|
||||
|
||||
/*
|
||||
|
@ -36,7 +36,7 @@ void setup() {
|
||||
Serial.begin(9600); //Init serial port
|
||||
|
||||
//Set math model to calculate the PPM concentration and the value of constants
|
||||
MQ309.setRegressionMethod("Exponential"); //_PPM = a*ratio^b
|
||||
MQ309.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ309.setA(1000000); MQ309.setB(-4.01); // Configurate the ecuation values to get CO concentration
|
||||
|
||||
/*
|
||||
|
@ -36,7 +36,7 @@ void setup() {
|
||||
Serial.begin(9600); //Init serial port
|
||||
|
||||
//Set math model to calculate the PPM concentration and the value of constants
|
||||
MQ4.setRegressionMethod("Exponential"); //_PPM = a*ratio^b
|
||||
MQ4.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ4.setA(1012.7); MQ4.setB(-2.786); // Configurate the ecuation values to get CH4 concentration
|
||||
/*
|
||||
Exponential regression:
|
||||
|
@ -36,7 +36,7 @@ void setup() {
|
||||
Serial.begin(9600); //Init serial port
|
||||
|
||||
//Set math model to calculate the PPM concentration and the value of constants
|
||||
MQ5.setRegressionMethod("Exponential"); //_PPM = a*ratio^b
|
||||
MQ5.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ5.setA(1163.8); MQ5.setB(-3.874); // Configurate the ecuation values to get H2 concentration
|
||||
/*
|
||||
Exponential regression:
|
||||
|
@ -36,7 +36,7 @@ void setup() {
|
||||
Serial.begin(9600); //Init serial port
|
||||
|
||||
//Set math model to calculate the PPM concentration and the value of constants
|
||||
MQ6.setRegressionMethod("Exponential"); //_PPM = a*ratio^b
|
||||
MQ6.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ6.setA(2127.2); MQ6.setB(-2.526); // Configurate the ecuation values to get CH4 concentration
|
||||
/*
|
||||
Exponential regression:
|
||||
|
@ -36,7 +36,7 @@ void setup() {
|
||||
Serial.begin(9600); //Init serial port
|
||||
|
||||
//Set math model to calculate the PPM concentration and the value of constants
|
||||
MQ7.setRegressionMethod("Exponential"); //_PPM = a*ratio^b
|
||||
MQ7.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ7.setA(99.042); MQ7.setB(-1.518); // Configurate the ecuation values to get CO concentration
|
||||
|
||||
/*
|
||||
|
@ -36,7 +36,7 @@ void setup() {
|
||||
Serial.begin(9600); //Init serial port
|
||||
|
||||
//Set math model to calculate the PPM concentration and the value of constants
|
||||
MQ8.setRegressionMethod("Exponential"); //_PPM = a*ratio^b
|
||||
MQ8.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ8.setA(976.97); MQ8.setB(-0.688); // Configurate the ecuation values to get H2 concentration
|
||||
|
||||
/*
|
||||
|
@ -36,7 +36,7 @@ void setup() {
|
||||
Serial.begin(9600); //Init serial port
|
||||
|
||||
//Set math model to calculate the PPM concentration and the value of constants
|
||||
MQ9.setRegressionMethod("Exponential"); //_PPM = a*ratio^b
|
||||
MQ9.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ9.setA(1000.5); MQ9.setB(-2.186); // Configurate the ecuation values to get LPG concentration
|
||||
|
||||
/*
|
||||
|
@ -93,42 +93,42 @@ void setup() {
|
||||
//init the sensor
|
||||
|
||||
MQ2.init();
|
||||
MQ2.setRegressionMethod("Exponential"); //_PPM = a*ratio^b
|
||||
MQ2.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ2.setA(574.25); MQ2.setB(-2.222); // Configurate the ecuation values to get LPG concentration
|
||||
MQ2.setR0(9.659574468);
|
||||
|
||||
MQ3.init();
|
||||
MQ3.setRegressionMethod("Exponential"); //_PPM = a*ratio^b
|
||||
MQ3.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ3.setA(4.8387); MQ3.setB(-2.68); // Configurate the ecuation values to get Benzene concentration
|
||||
MQ3.setR0(3.86018237);
|
||||
|
||||
MQ4.init();
|
||||
MQ4.setRegressionMethod("Exponential"); //_PPM = a*ratio^b
|
||||
MQ4.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ4.setA(1012.7); MQ4.setB(-2.786); // Configurate the ecuation values to get CH4 concentration
|
||||
MQ4.setR0(3.86018237);
|
||||
|
||||
MQ5.init();
|
||||
MQ5.setRegressionMethod("Exponential"); //_PPM = a*ratio^b
|
||||
MQ5.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ5.setA(1163.8); MQ5.setB(-3.874); // Configurate the ecuation values to get H2 concentration
|
||||
MQ5.setR0(71.100304);
|
||||
|
||||
MQ6.init();
|
||||
MQ6.setRegressionMethod("Exponential"); //_PPM = a*ratio^b
|
||||
MQ6.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ6.setA(2127.2); MQ6.setB(-2.526); // Configurate the ecuation values to get CH4 concentration
|
||||
MQ6.setR0(13.4285714);
|
||||
|
||||
MQ7.init();
|
||||
MQ7.setRegressionMethod("Exponential"); //_PPM = a*ratio^b
|
||||
MQ7.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ7.setA(99.042); MQ7.setB(-1.518); // Configurate the ecuation values to get CO concentration
|
||||
MQ7.setR0(4);
|
||||
|
||||
MQ8.init();
|
||||
MQ8.setRegressionMethod("Exponential"); //_PPM = a*ratio^b
|
||||
MQ8.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ8.setA(976.97); MQ8.setB(-0.688); // Configurate the ecuation values to get H2 concentration
|
||||
MQ8.setR0(1);
|
||||
|
||||
MQ9.init();
|
||||
MQ9.setRegressionMethod("Exponential"); //_PPM = a*ratio^b
|
||||
MQ9.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ9.setA(1000.5); MQ9.setB(-2.186); // Configurate the ecuation values to get LPG concentration
|
||||
MQ9.setR0(9.42857143);
|
||||
|
||||
|
@ -49,42 +49,42 @@ void setup() {
|
||||
Serial.begin(9600);
|
||||
//init the sensor
|
||||
MQ2.init();
|
||||
MQ2.setRegressionMethod("Exponential"); //_PPM = a*ratio^b
|
||||
MQ2.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ2.setA(574.25); MQ2.setB(-2.222); // Configurate the ecuation values to get LPG concentration
|
||||
MQ2.setR0(9.659574468);
|
||||
|
||||
MQ3.init();
|
||||
MQ3.setRegressionMethod("Exponential"); //_PPM = a*ratio^b
|
||||
MQ3.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ3.setA(4.8387); MQ3.setB(-2.68); // Configurate the ecuation values to get Benzene concentration
|
||||
MQ3.setR0(3.86018237);
|
||||
|
||||
MQ4.init();
|
||||
MQ4.setRegressionMethod("Exponential"); //_PPM = a*ratio^b
|
||||
MQ4.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ4.setA(1012.7); MQ4.setB(-2.786); // Configurate the ecuation values to get CH4 concentration
|
||||
MQ4.setR0(3.86018237);
|
||||
|
||||
MQ5.init();
|
||||
MQ5.setRegressionMethod("Exponential"); //_PPM = a*ratio^b
|
||||
MQ5.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ5.setA(1163.8); MQ5.setB(-3.874); // Configurate the ecuation values to get H2 concentration
|
||||
MQ5.setR0(71.100304);
|
||||
|
||||
MQ6.init();
|
||||
MQ6.setRegressionMethod("Exponential"); //_PPM = a*ratio^b
|
||||
MQ6.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ6.setA(2127.2); MQ6.setB(-2.526); // Configurate the ecuation values to get CH4 concentration
|
||||
MQ6.setR0(13.4285714);
|
||||
|
||||
MQ7.init();
|
||||
MQ7.setRegressionMethod("Exponential"); //_PPM = a*ratio^b
|
||||
MQ7.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ7.setA(99.042); MQ7.setB(-1.518); // Configurate the ecuation values to get CO concentration
|
||||
MQ7.setR0(4);
|
||||
|
||||
MQ8.init();
|
||||
MQ8.setRegressionMethod("Exponential"); //_PPM = a*ratio^b
|
||||
MQ8.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ8.setA(976.97); MQ8.setB(-0.688); // Configurate the ecuation values to get H2 concentration
|
||||
MQ8.setR0(1);
|
||||
|
||||
MQ9.init();
|
||||
MQ9.setRegressionMethod("Exponential"); //_PPM = a*ratio^b
|
||||
MQ9.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ9.setA(1000.5); MQ9.setB(-2.186); // Configurate the ecuation values to get LPG concentration
|
||||
MQ9.setR0(9.42857143);
|
||||
//Print in serial monitor
|
||||
|
@ -31,7 +31,7 @@ void setup() {
|
||||
//Init serial port
|
||||
Serial.begin(115200);
|
||||
//Set math model to calculate the PPM concentration and the value of constants
|
||||
MQ4.setRegressionMethod("Exponential"); //_PPM = a*ratio^b
|
||||
MQ4.setRegressionMethod(1); //_PPM = a*ratio^b
|
||||
MQ4.setA(30000000); MQ4.setB(-2.786); // Configurate the ecuation values to get CH4 concentration
|
||||
/*
|
||||
Exponential regression:
|
||||
|
@ -1,9 +1,11 @@
|
||||
#include "MQUnifiedsensor.h"
|
||||
|
||||
MQUnifiedsensor::MQUnifiedsensor(String Placa, double Voltage_Resolution, int ADC_Bit_Resolution, int pin, String type) {
|
||||
MQUnifiedsensor::MQUnifiedsensor(String Placa, float Voltage_Resolution, int ADC_Bit_Resolution, int pin, String type) {
|
||||
this->_pin = pin;
|
||||
this->_type = type; //MQ-2, MQ-3 ... MQ-309A
|
||||
this->_placa = Placa;
|
||||
Placa.toCharArray(this->_placa, 20);
|
||||
type.toCharArray(this->_type, 6);
|
||||
//this->_type = type; //MQ-2, MQ-3 ... MQ-309A
|
||||
//this->_placa = Placa;
|
||||
this-> _VOLT_RESOLUTION = Voltage_Resolution;
|
||||
this-> _ADC_Bit_Resolution = ADC_Bit_Resolution;
|
||||
}
|
||||
@ -11,33 +13,48 @@ void MQUnifiedsensor::init()
|
||||
{
|
||||
pinMode(_pin, INPUT);
|
||||
}
|
||||
void MQUnifiedsensor::setA(double a) {
|
||||
void MQUnifiedsensor::setA(float a) {
|
||||
this->_a = a;
|
||||
}
|
||||
void MQUnifiedsensor::setB(double b) {
|
||||
void MQUnifiedsensor::setB(float b) {
|
||||
this->_b = b;
|
||||
}
|
||||
void MQUnifiedsensor::setR0(double R0) {
|
||||
void MQUnifiedsensor::setR0(float R0) {
|
||||
this->_R0 = R0;
|
||||
}
|
||||
void MQUnifiedsensor::setRL(double RL) {
|
||||
void MQUnifiedsensor::setRL(float RL) {
|
||||
this->_RL = RL;
|
||||
}
|
||||
void MQUnifiedsensor::setVoltResolution(double voltage_resolution)
|
||||
void MQUnifiedsensor::setVoltResolution(float voltage_resolution)
|
||||
{
|
||||
_VOLT_RESOLUTION = voltage_resolution;
|
||||
}
|
||||
void MQUnifiedsensor::setRegressionMethod(String regressionMethod)
|
||||
void MQUnifiedsensor::setRegressionMethod(int regressionMethod)
|
||||
{
|
||||
//this->_regressionMethod = regressionMethod;
|
||||
this->_regressionMethod = regressionMethod;
|
||||
}
|
||||
|
||||
double MQUnifiedsensor::getR0() {
|
||||
float MQUnifiedsensor::getR0() {
|
||||
return _R0;
|
||||
}
|
||||
double MQUnifiedsensor::getRL() {
|
||||
float MQUnifiedsensor::getRL() {
|
||||
return _RL;
|
||||
}
|
||||
float MQUnifiedsensor::getVoltResolution()
|
||||
{
|
||||
return _VOLT_RESOLUTION;
|
||||
}
|
||||
String MQUnifiedsensor::getRegressionMethod()
|
||||
{
|
||||
if(_regressionMethod == 1) return "Exponential";
|
||||
else return "Linear";
|
||||
}
|
||||
float MQUnifiedsensor::getA() {
|
||||
return _a;
|
||||
}
|
||||
float MQUnifiedsensor::getB() {
|
||||
return _b;
|
||||
}
|
||||
void MQUnifiedsensor::serialDebug(bool onSetup)
|
||||
{
|
||||
if(onSetup)
|
||||
@ -52,33 +69,33 @@ void MQUnifiedsensor::serialDebug(bool onSetup)
|
||||
Serial.println("Authors: Miguel A. Califa U - Yersson R. Carrillo A - Ghiordy F. Contreras C");
|
||||
Serial.println("Contributors: Andres A. Martinez - Juan A. Rodríguez - Mario A. Rodríguez O ");
|
||||
|
||||
Serial.println("Sensor: " + _type);
|
||||
Serial.print("Sensor: "); Serial.println(_type);
|
||||
Serial.print("Supply voltage: "); Serial.print(_VOLT_RESOLUTION); Serial.println(" VDC");
|
||||
Serial.print("ADC Resolution: "); Serial.print(_ADC_Bit_Resolution); Serial.println(" Bits");
|
||||
Serial.print("R0: "); Serial.print(_R0); Serial.println(" KΩ");
|
||||
Serial.print("RL: "); Serial.print(_RL); Serial.println(" KΩ");
|
||||
|
||||
Serial.print("Model: "); if(_regressionMethod == "Exponential") Serial.println("Exponential"); else Serial.println("Linear");
|
||||
Serial.print(_type + " -> " + "a: "); Serial.print(_a); Serial.print(" | b: "); Serial.println(_b);
|
||||
Serial.print(_type); Serial.print(" -> a: "); Serial.print(_a); Serial.print(" | b: "); Serial.println(_b);
|
||||
|
||||
Serial.println("Development board: " + _placa);
|
||||
Serial.print("Development board: "); Serial.println(_placa);
|
||||
}
|
||||
else
|
||||
{
|
||||
if(!_firstFlag)
|
||||
{
|
||||
Serial.println("| ********************************************************************" + _type + "*********************************************************************|");
|
||||
Serial.print("| ********************************************************************"); Serial.print(_type); Serial.println("*********************************************************************|");
|
||||
Serial.println("|ADC_In | Equation_V_ADC | Voltage_ADC | Equation_RS | Resistance_RS | EQ_Ratio | Ratio (RS/R0) | Equation_PPM | PPM |");
|
||||
_firstFlag = true; //Headers are printed
|
||||
}
|
||||
else
|
||||
{
|
||||
String eq = "";
|
||||
if(_regressionMethod == "Linear") eq = "ratio*a + b";
|
||||
if(_regressionMethod == "Exponential") eq = "a*ratio^b";
|
||||
Serial.print("|"); Serial.print(_adc); Serial.print("| v = ADC*"); Serial.print(_VOLT_RESOLUTION); Serial.print("/"); Serial.print(pow(2, _ADC_Bit_Resolution)); Serial.print(" | "); Serial.print(_sensor_volt);
|
||||
Serial.print(" | RS = ((" ); Serial.print(_VOLT_RESOLUTION ); Serial.print("*RL)/Voltage) - RL| "); Serial.print(_RS_Calc); Serial.print(" | Ratio = RS/R0| ");
|
||||
Serial.print(_ratio); Serial.print( " | " + eq + " | "); Serial.print(_PPM); Serial.println(" |");
|
||||
Serial.print(_ratio); Serial.print( " | ");
|
||||
if(_regressionMethod != 1) Serial.println("ratio*a + b");
|
||||
else Serial.println("a*ratio^b");
|
||||
Serial.print(" | "); Serial.print(_PPM); Serial.println(" |");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -86,11 +103,11 @@ void MQUnifiedsensor::update()
|
||||
{
|
||||
_sensor_volt = this->getVoltage();
|
||||
}
|
||||
float MQUnifiedsensor::calculatePPM(double ratio)
|
||||
float MQUnifiedsensor::validateEcuation(float ratioInput)
|
||||
{
|
||||
//Usage of this function: Unit test on ALgorithmTester example;
|
||||
if(_regressionMethod == "Exponential") _PPM= _a*pow(ratio, _b);
|
||||
if(_regressionMethod == "Linear") _PPM= _a*ratio + _b;
|
||||
if(_regressionMethod != 1) _PPM= _a*pow(ratioInput, _b);
|
||||
else _PPM= _a*ratioInput + _b;
|
||||
return _PPM;
|
||||
}
|
||||
float MQUnifiedsensor::readSensor()
|
||||
@ -100,8 +117,8 @@ float MQUnifiedsensor::readSensor()
|
||||
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 = 0; //No negative values accepted or upper datasheet recomendation.
|
||||
if(_regressionMethod == "Exponential") _PPM= _a*pow(_ratio, _b);
|
||||
if(_regressionMethod == "Linear") _PPM= _a*_ratio + _b;
|
||||
if(_regressionMethod != 1) _PPM= _a*pow(_ratio, _b);
|
||||
else _PPM= _a*_ratio + _b;
|
||||
if(_PPM < 0) _PPM = 0; //No negative values accepted or upper datasheet recomendation.
|
||||
//if(_PPM > 10000) _PPM = 99999999; //No negative values accepted or upper datasheet recomendation.
|
||||
return _PPM;
|
||||
@ -127,11 +144,11 @@ float MQUnifiedsensor::calibrate(float ratioInCleanAir) {
|
||||
if(R0 < 0) R0 = 0; //No negative values accepted.
|
||||
return R0;
|
||||
}
|
||||
double MQUnifiedsensor::getVoltage(int read) {
|
||||
double voltage;
|
||||
float MQUnifiedsensor::getVoltage(int read) {
|
||||
float voltage;
|
||||
if(read)
|
||||
{
|
||||
double avg = 0.0;
|
||||
float avg = 0.0;
|
||||
for (int i = 0; i < retries; i ++) {
|
||||
_adc = analogRead(this->_pin);
|
||||
avg += _adc;
|
||||
@ -145,7 +162,7 @@ double MQUnifiedsensor::getVoltage(int read) {
|
||||
}
|
||||
return voltage;
|
||||
}
|
||||
double MQUnifiedsensor::stringToDouble(String & str)
|
||||
float MQUnifiedsensor::stringTofloat(String & str)
|
||||
{
|
||||
return atof( str.c_str() );
|
||||
}
|
@ -13,30 +13,34 @@
|
||||
class MQUnifiedsensor
|
||||
{
|
||||
public:
|
||||
MQUnifiedsensor(String Placa = "Arduino", double Voltage_Resolution = 5, int ADC_Bit_Resolution = 10, int pin = 1, String type = "CUSTOM MQ");
|
||||
MQUnifiedsensor(String Placa = "Arduino", float Voltage_Resolution = 5, int ADC_Bit_Resolution = 10, int pin = 1, String type = "CUSTOM MQ");
|
||||
|
||||
//Functions to set values
|
||||
void init();
|
||||
void update();
|
||||
void setR0(double R0 = 10);
|
||||
void setRL(double RL = 10);
|
||||
void setA(double a);
|
||||
void setB(double b);
|
||||
void setRegressionMethod(String regressionMethod);
|
||||
void setVoltResolution(double voltage_resolution = 5);
|
||||
void setR0(float R0 = 10);
|
||||
void setRL(float RL = 10);
|
||||
void setA(float a);
|
||||
void setB(float b);
|
||||
void setRegressionMethod(int regressionMethod);
|
||||
void setVoltResolution(float voltage_resolution = 5);
|
||||
void serialDebug(bool onSetup = false); //Show on serial port information about sensor
|
||||
|
||||
//user functions
|
||||
float calibrate(float ratioInCleanAir);
|
||||
float readSensor();
|
||||
float calculatePPM(double ratio = 0);
|
||||
float validateEcuation(float ratioInput = 0);
|
||||
|
||||
//get function for info
|
||||
double getR0();
|
||||
double getRL();
|
||||
double getVoltage(int read = true);
|
||||
float getA();
|
||||
float getB();
|
||||
float getR0();
|
||||
float getRL();
|
||||
float getVoltResolution();
|
||||
String getRegressionMethod();
|
||||
float getVoltage(int read = true);
|
||||
|
||||
double stringToDouble(String & str);
|
||||
float stringTofloat(String & str);
|
||||
|
||||
private:
|
||||
/************************Private vars************************************/
|
||||
@ -45,13 +49,13 @@ class MQUnifiedsensor
|
||||
byte _VOLT_RESOLUTION = 5.0; // if 3.3v use 3.3
|
||||
byte _RL = 10; //Value in KiloOhms
|
||||
byte _ADC_Bit_Resolution = 10;
|
||||
byte _regressionMethod = 1; // 1 -> Exponential || 2 -> Linear
|
||||
|
||||
double _adc, _a, _b, _sensor_volt;
|
||||
float _adc, _a, _b, _sensor_volt;
|
||||
float _R0, RS_air, _ratio, _PPM, _RS_Calc;
|
||||
|
||||
String _type;
|
||||
String _placa;
|
||||
String _regressionMethod;
|
||||
char _type[6];
|
||||
char _placa[20];
|
||||
};
|
||||
|
||||
#endif //MQUnifiedsensor_H
|
||||
|
Loading…
x
Reference in New Issue
Block a user