From 70fbaa911524d1c2fb070d890144eaea786cf3d8 Mon Sep 17 00:00:00 2001 From: miguel5612 Date: Sat, 28 Mar 2020 12:58:25 -0500 Subject: [PATCH] Added Linear ecuation on test algorithm --- examples/AlgorithmTester/AlgorithmTester.ino | 7 +++++++ examples/MQ-4-LINEAR/MQ-4-LINEAR.ino | 2 +- src/MQUnifiedsensor.cpp | 6 +++--- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/examples/AlgorithmTester/AlgorithmTester.ino b/examples/AlgorithmTester/AlgorithmTester.ino index 3686989..f632d9c 100644 --- a/examples/AlgorithmTester/AlgorithmTester.ino +++ b/examples/AlgorithmTester/AlgorithmTester.ino @@ -99,6 +99,13 @@ void loop() expectedValue[0] = 1000; expectedValue[1] = 3000; expectedValue[2] = 5000; expectedValue[3] = 7000; testSensor("MQ-309", 1000000, -4.01); + // Testing linear equation for MQ-4 + mySensor.setRegressionMethod(0); //_PPM = pow(10, (log10(ratio)-b)/a) + ratio[0] = 2.5; ratio[1] = 1.5; ratio[2] = 0.9; ratio[3] = 0.65; + expectedValue[0] = 200; expectedValue[1] = 1000; expectedValue[2] = 5000; expectedValue[3] = 10000; + testSensor("MQ-4", -0.318, 1.133); + + while(1); } diff --git a/examples/MQ-4-LINEAR/MQ-4-LINEAR.ino b/examples/MQ-4-LINEAR/MQ-4-LINEAR.ino index aee7669..3e3e704 100644 --- a/examples/MQ-4-LINEAR/MQ-4-LINEAR.ino +++ b/examples/MQ-4-LINEAR/MQ-4-LINEAR.ino @@ -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 - MQ4.setRegressionMethod(0); //_PPM = a*ratio + b + MQ4.setRegressionMethod(0); //_PPM = pow(10, (log10(ratio)-b)/a) diff --git a/src/MQUnifiedsensor.cpp b/src/MQUnifiedsensor.cpp index 47d07f2..265ec8e 100644 --- a/src/MQUnifiedsensor.cpp +++ b/src/MQUnifiedsensor.cpp @@ -112,9 +112,9 @@ float MQUnifiedsensor::validateEcuation(float ratioInput) if(_regressionMethod == 1) _PPM= _a*pow(ratioInput, _b); else { - // https://jayconsystems.com/blog/understanding-a-gas-sensor - double ppm_log = (log10(_ratio)-_b)/_a; //Get ppm value in linear scale according to the the ratio value - _PPM = pow(10, ppm_log); //Convert ppm value to log scale + // https://jayconsystems.com/blog/understanding-a-gas-sensor + double ppm_log = (log10(ratioInput)-_b)/_a; //Get ppm value in linear scale according to the the ratio value + _PPM = pow(10, ppm_log); //Convert ppm value to log scale } //Serial.println("Regression Method: "); Serial.println(_regressionMethod); //Serial.println("Result: "); Serial.println(_PPM);