diff --git a/BaseMQ.cpp b/BaseMQ.cpp index c1a5ad5..ee09891 100644 --- a/BaseMQ.cpp +++ b/BaseMQ.cpp @@ -1,14 +1,14 @@ #include "BaseMQ.h" -BaseMQ::BaseMQ(uint8_t pin) { - _pin = pin; +BaseMQ::BaseMQ(uint8_t pin) : + _pin (pin) { } -BaseMQ::BaseMQ(uint8_t pin, uint8_t pinHeater) { - _pin = pin; - _pinHeater = pinHeater; +BaseMQ::BaseMQ(uint8_t pin, uint8_t pinHeater) : + _pin (pin), + _pinHeater (pinHeater) +{ pinMode(_pinHeater, OUTPUT); - _prMillis = 0; } // калибровка датчика @@ -26,8 +26,7 @@ void BaseMQ::calibrate() { } ro = ro/MQ_SAMPLE_TIMES; ro = ro/getRoInCleanAir(); - _ro = ro; - _stateCalibrate = true; + calibrate (ro); } void BaseMQ::heaterPwrHigh() { @@ -49,14 +48,14 @@ void BaseMQ::heaterPwrOff() { } // сопротивление датчика -float BaseMQ::calculateResistance(int rawAdc) { +float BaseMQ::calculateResistance(int rawAdc) const { float vrl = rawAdc*(5.0 / 1023); float rsAir = (5.0 - vrl)/vrl*getRL(); return rsAir; } // считывание датчика -float BaseMQ::readRs() { +float BaseMQ::readRs() const { float rs = 0; for (int i = 0; i < MQ_SAMPLE_TIMES; i++) { rs += calculateResistance(analogRead(_pin)); @@ -66,26 +65,23 @@ float BaseMQ::readRs() { return rs; } -float BaseMQ::getRo() { - return _ro; +unsigned long BaseMQ::readScaled(float a, float b) const { + float ratio = readRatio(); + return exp((log(ratio)-b)/a); } -bool BaseMQ::isCalibrated() { - return _stateCalibrate; +float BaseMQ::readRatio() const { + return readRs()/getRo(); } -float BaseMQ::readRatio() { - return readRs()/_ro; -} - -bool BaseMQ::heatingCompleted() { +bool BaseMQ::heatingCompleted() const { if ((_heater) && (!_cooler) && (millis() - _prMillis > 60000)) return true; else return false; } -bool BaseMQ::coolanceCompleted() { +bool BaseMQ::coolanceCompleted() const { if ((_heater) && (_cooler) && (millis() - _prMillis > 90000)) return true; else diff --git a/BaseMQ.h b/BaseMQ.h index 6aaf1bc..d80cab1 100644 --- a/BaseMQ.h +++ b/BaseMQ.h @@ -16,24 +16,32 @@ public: void heaterPwrLow(); void heaterPwrOff(); void cycleHeat(); - bool heatingCompleted(); - bool coolanceCompleted(); - bool isCalibrated(); bool atHeatCycleEnd(); - float getRo(); - float readRatio(); + bool heatingCompleted() const; + bool coolanceCompleted() const; + float readRatio() const; + inline bool isCalibrated() const { + return _stateCalibrate; + }; + inline float getRo() const { + return _ro; + }; + protected: + unsigned long readScaled(float a, float b) const; + virtual float getRoInCleanAir() const = 0; + virtual int getRL() const = 0; + +private: bool _heater = false; bool _cooler = false; bool _stateCalibrate = false; - unsigned long _prMillis; - float _ro; + unsigned long _prMillis = 0; + float _ro = 1.0f; uint8_t _pin; uint8_t _pinHeater; - float readRs(); - float calculateResistance(int rawAdc); - virtual float getRoInCleanAir() const = 0; - virtual int getRL() const = 0; + float readRs() const; + float calculateResistance(int rawAdc) const; }; -#endif \ No newline at end of file +#endif diff --git a/MQ2.cpp b/MQ2.cpp index 6c0a29d..0b897de 100644 --- a/MQ2.cpp +++ b/MQ2.cpp @@ -9,22 +9,17 @@ MQ2::MQ2(uint8_t pin, uint8_t pinHeater) } unsigned long MQ2::readLPG() { - return readPpm(-0.45, 2.95); + return readScaled(-0.45, 2.95); } unsigned long MQ2::readMethane() { - return readPpm(-0.38, 3.21); + return readScaled(-0.38, 3.21); } unsigned long MQ2::readSmoke() { - return readPpm(-0.42, 3.54); + return readScaled(-0.42, 3.54); } unsigned long MQ2::readHydrogen() { - return readPpm(-0.48, 3.32); -} - -int MQ2::readPpm(float a, float b) { - float ratio = readRs()/_ro; - return pow(M_E, (log(ratio)-b)/a); + return readScaled(-0.48, 3.32); } diff --git a/MQ2.h b/MQ2.h index a563c10..6f1e2bb 100644 --- a/MQ2.h +++ b/MQ2.h @@ -12,11 +12,10 @@ public: unsigned long readSmoke(); unsigned long readHydrogen(); private: - int readPpm(float a, float b); // Резистор установленный на плату (кОм) virtual int getRL() const { return 5; } // коефициент чистого воздуха virtual float getRoInCleanAir() const { return 9.83; } }; -#endif // MQ2_H_ \ No newline at end of file +#endif // MQ2_H_ diff --git a/MQ3.cpp b/MQ3.cpp index 2d4e321..71053b0 100644 --- a/MQ3.cpp +++ b/MQ3.cpp @@ -9,14 +9,9 @@ MQ3::MQ3(uint8_t pin, uint8_t pinHeater) } float MQ3::readAlcoholMgL() { - return readMgL(-0.66, -0.62); + return readScaled(-0.66, -0.62); } float MQ3::readAlcoholPpm() { - return readMgL(-0.66, -0.62)*2.2; -} - -float MQ3::readMgL(float a, float b) { - float ratio = readRs()/_ro; - return pow(M_E, (log(ratio)-b)/a); + return readScaled(-0.66, -0.62)*2.2; } diff --git a/MQ3.h b/MQ3.h index e796480..7bfc1fc 100644 --- a/MQ3.h +++ b/MQ3.h @@ -10,11 +10,10 @@ public: float readAlcoholMgL(); float readAlcoholPpm(); private: - float readMgL(float a, float b); // Резистор установленный на плату (кОм) virtual int getRL() const { return 200; } // коефициент чистого воздуха virtual float getRoInCleanAir() const { return 60; } }; -#endif // MQ3_H_ \ No newline at end of file +#endif // MQ3_H_ diff --git a/MQ4.cpp b/MQ4.cpp index f377862..c63496e 100644 --- a/MQ4.cpp +++ b/MQ4.cpp @@ -9,10 +9,5 @@ MQ4::MQ4(uint8_t pin, uint8_t pinHeater) } unsigned long MQ4::readMethane() { - return readPpm(-0.36, 2.54); -} - -int MQ4::readPpm(float a, float b) { - float ratio = readRs()/_ro; - return pow(M_E, (log(ratio)-b)/a); + return readScaled(-0.36, 2.54); } diff --git a/MQ4.h b/MQ4.h index a8b1f38..1555fd2 100644 --- a/MQ4.h +++ b/MQ4.h @@ -9,11 +9,10 @@ public: MQ4(uint8_t pin, uint8_t pinHeater); unsigned long readMethane(); private: - int readPpm(float a, float b); // Резистор установленный на плату (кОм) virtual int getRL() const { return 20; } // коефициент чистого воздуха virtual float getRoInCleanAir() const { return 4.4; } }; -#endif // MQ4_H_ \ No newline at end of file +#endif // MQ4_H_ diff --git a/MQ5.cpp b/MQ5.cpp index a462094..2ab0b1e 100644 --- a/MQ5.cpp +++ b/MQ5.cpp @@ -9,14 +9,9 @@ MQ5::MQ5(uint8_t pin, uint8_t pinHeater) } unsigned long MQ5::readLPG() { - return readPpm(-0.39, 1.73); + return readScaled(-0.39, 1.73); } unsigned long MQ5::readMethane() { - return readPpm(-0.38, 1.97); -} - -int MQ5::readPpm(float a, float b) { - float ratio = readRs()/_ro; - return pow(M_E, (log(ratio)-b)/a); + return readScaled(-0.38, 1.97); } diff --git a/MQ5.h b/MQ5.h index a2e3eda..30accb8 100644 --- a/MQ5.h +++ b/MQ5.h @@ -10,11 +10,10 @@ public: unsigned long readLPG(); unsigned long readMethane(); private: - int readPpm(float a, float b); // Резистор установленный на плату (кОм) virtual int getRL() const { return 20; } // коефициент чистого воздуха virtual float getRoInCleanAir() const { return 6.5; } }; -#endif // MQ5_H_ \ No newline at end of file +#endif // MQ5_H_ diff --git a/MQ6.cpp b/MQ6.cpp index 05843fc..d60cb26 100644 --- a/MQ6.cpp +++ b/MQ6.cpp @@ -9,10 +9,5 @@ MQ6::MQ6(uint8_t pin, uint8_t pinHeater) } unsigned long MQ6::readLPG() { - return readPpm(-0.42, 2.91); -} - -int MQ6::readPpm(float a, float b) { - float ratio = readRs()/_ro; - return pow(M_E, (log(ratio)-b)/a); + return readScaled(-0.42, 2.91); } diff --git a/MQ6.h b/MQ6.h index c474480..fb14bc6 100644 --- a/MQ6.h +++ b/MQ6.h @@ -9,11 +9,10 @@ public: MQ6(uint8_t pin, uint8_t pinHeater); unsigned long readLPG(); private: - int readPpm(float a, float b); // Резистор установленный на плату (кОм) virtual int getRL() const { return 20; } // коефициент чистого воздуха virtual float getRoInCleanAir() const { return 10; } }; -#endif // MQ6_H_ \ No newline at end of file +#endif // MQ6_H_ diff --git a/MQ7.cpp b/MQ7.cpp index dcb8563..abbc023 100644 --- a/MQ7.cpp +++ b/MQ7.cpp @@ -9,10 +9,5 @@ MQ7::MQ7(uint8_t pin, uint8_t pinHeater) } unsigned long MQ7::readCarbonMonoxide() { - return readPpm(-0.77, 3.38); -} - -int MQ7::readPpm(float a, float b) { - float ratio = readRs()/_ro; - return pow(M_E, (log(ratio)-b)/a); + return readScaled(-0.77, 3.38); } diff --git a/MQ7.h b/MQ7.h index be281eb..cbda104 100644 --- a/MQ7.h +++ b/MQ7.h @@ -9,11 +9,10 @@ public: MQ7(uint8_t pin, uint8_t pinHeater); unsigned long readCarbonMonoxide(); private: - int readPpm(float a, float b); // Резистор установленный на плату (кОм) virtual int getRL() const { return 10; } // коефициент чистого воздуха virtual float getRoInCleanAir() const { return 27; } }; -#endif // MQ7_H_ \ No newline at end of file +#endif // MQ7_H_ diff --git a/MQ8.cpp b/MQ8.cpp index c2897d1..762e032 100644 --- a/MQ8.cpp +++ b/MQ8.cpp @@ -9,10 +9,5 @@ MQ8::MQ8(uint8_t pin, uint8_t pinHeater) } unsigned long MQ8::readHydrogen() { - return readPpm(-1.52, 10.49); -} - -int MQ8::readPpm(float a, float b) { - float ratio = readRs()/_ro; - return pow(M_E, (log(ratio)-b)/a); + return readScaled(-1.52, 10.49); } diff --git a/MQ8.h b/MQ8.h index ceb18ea..fae336c 100644 --- a/MQ8.h +++ b/MQ8.h @@ -9,11 +9,10 @@ public: MQ8(uint8_t pin, uint8_t pinHeater); unsigned long readHydrogen(); private: - int readPpm(float a, float b); // Резистор установленный на плату (кОм) virtual int getRL() const { return 10; } // коефициент чистого воздуха virtual float getRoInCleanAir() const { return 70; } }; -#endif // MQ8_H_ \ No newline at end of file +#endif // MQ8_H_ diff --git a/MQ9.cpp b/MQ9.cpp index 95dd875..efc80ca 100644 --- a/MQ9.cpp +++ b/MQ9.cpp @@ -9,18 +9,13 @@ MQ9::MQ9(uint8_t pin, uint8_t pinHeater) } unsigned long MQ9::readLPG() { - return readPpm(-0.48, 3.33); + return readScaled(-0.48, 3.33); } unsigned long MQ9::readMethane() { - return readPpm(-0.38, 3.21); + return readScaled(-0.38, 3.21); } unsigned long MQ9::readCarbonMonoxide() { - return readPpm(-0.48, 3.10); -} - -int MQ9::readPpm(float a, float b) { - float ratio = readRs()/_ro; - return pow(M_E, (log(ratio)-b)/a); + return readScaled(-0.48, 3.10); } diff --git a/MQ9.h b/MQ9.h index 4a4b28f..b375598 100644 --- a/MQ9.h +++ b/MQ9.h @@ -11,11 +11,10 @@ public: unsigned long readMethane(); unsigned long readCarbonMonoxide(); private: - int readPpm(float a, float b); // Резистор установленный на плату (кОм) virtual int getRL() const { return 10; } // коефициент чистого воздуха virtual float getRoInCleanAir() const { return 9.8; } }; -#endif // MQ9_H_ \ No newline at end of file +#endif // MQ9_H_