TroykaMQ/BaseMQ.cpp

114 lines
2.3 KiB
C++
Raw Normal View History

2016-04-29 17:42:44 +03:00
#include "BaseMQ.h"
2016-08-26 14:43:01 +03:00
BaseMQ::BaseMQ(uint8_t pin) :
_pin (pin) {
2016-04-29 17:42:44 +03:00
}
2016-08-26 14:43:01 +03:00
BaseMQ::BaseMQ(uint8_t pin, uint8_t pinHeater) :
_pin (pin),
_pinHeater (pinHeater)
{
2016-04-29 17:42:44 +03:00
pinMode(_pinHeater, OUTPUT);
}
2016-05-12 17:53:29 +03:00
// калибровка датчика
2016-04-29 17:42:44 +03:00
void BaseMQ::calibrate(float ro) {
_ro = ro;
_stateCalibrate = true;
}
2016-05-12 17:53:29 +03:00
// калибровка датчика
2016-04-29 17:42:44 +03:00
void BaseMQ::calibrate() {
float ro = 0;
for (int i = 0; i < MQ_SAMPLE_TIMES; i++) {
ro += calculateResistance(analogRead(_pin));
delay(MQ_SAMPLE_INTERVAL);
}
ro = ro/MQ_SAMPLE_TIMES;
ro = ro/getRoInCleanAir();
2016-08-26 14:06:39 +03:00
calibrate (ro);
2016-04-29 17:42:44 +03:00
}
void BaseMQ::heaterPwrHigh() {
digitalWrite(_pinHeater, HIGH);
_heater = true;
_prMillis = millis();
}
void BaseMQ::heaterPwrLow() {
analogWrite(_pinHeater, 75);
_heater = true;
_cooler = true;
_prMillis = millis();
}
void BaseMQ::heaterPwrOff() {
digitalWrite(_pinHeater, LOW);
_heater = false;
}
2016-05-12 17:53:29 +03:00
// сопротивление датчика
2016-08-26 14:43:01 +03:00
float BaseMQ::calculateResistance(int rawAdc) const {
2016-04-29 17:42:44 +03:00
float vrl = rawAdc*(5.0 / 1023);
float rsAir = (5.0 - vrl)/vrl*getRL();
return rsAir;
}
// считывание датчика
2016-08-26 14:43:01 +03:00
float BaseMQ::readRs() const {
2016-04-29 17:42:44 +03:00
float rs = 0;
for (int i = 0; i < MQ_SAMPLE_TIMES; i++) {
rs += calculateResistance(analogRead(_pin));
delay(MQ_SAMPLE_INTERVAL);
}
rs = rs/MQ_SAMPLE_TIMES;
return rs;
}
2016-08-26 14:43:01 +03:00
unsigned long BaseMQ::readScaled(float a, float b) const {
2016-08-26 14:06:39 +03:00
float ratio = readRatio();
2016-08-26 14:09:24 +03:00
return exp((log(ratio)-b)/a);
2016-04-29 17:42:44 +03:00
}
2016-08-26 14:43:01 +03:00
float BaseMQ::readRatio() const {
2016-08-26 14:06:39 +03:00
return readRs()/getRo();
2016-04-29 17:42:44 +03:00
}
2016-08-26 14:43:01 +03:00
bool BaseMQ::heatingCompleted() const {
2016-05-12 17:53:29 +03:00
if ((_heater) && (!_cooler) && (millis() - _prMillis > 60000))
2016-04-29 17:42:44 +03:00
return true;
else
return false;
}
2016-08-26 14:43:01 +03:00
bool BaseMQ::coolanceCompleted() const {
2016-05-12 17:53:29 +03:00
if ((_heater) && (_cooler) && (millis() - _prMillis > 90000))
2016-04-29 17:42:44 +03:00
return true;
else
return false;
}
void BaseMQ::cycleHeat() {
_heater = false;
_cooler = false;
heaterPwrHigh();
#ifdef MQDEBUG
2016-04-29 17:42:44 +03:00
Serial.println("Heated sensor");
#endif //MQDEBUG
2016-04-29 17:42:44 +03:00
}
bool BaseMQ::atHeatCycleEnd() {
if (heatingCompleted()) {
heaterPwrLow();
#ifdef MQDEBUG
2016-04-29 17:42:44 +03:00
Serial.println("Cool sensor");
#endif //MQDEBUG
2016-04-29 17:42:44 +03:00
return false;
} else if (coolanceCompleted()) {
heaterPwrOff();
return true;
} else {
return false;
}
}