optimizations
This commit is contained in:
parent
360211d2c2
commit
ce1a6273ee
3 changed files with 38 additions and 19 deletions
|
@ -4,21 +4,25 @@ MQ135::MQ135(uint8_t pin) {
|
|||
_pin = pin;
|
||||
}
|
||||
|
||||
double MQ135::getRS() {
|
||||
double voltage = getVoltage();
|
||||
double RS = (double)((double)(VIn * RL) / voltage) - RL;
|
||||
return RS;
|
||||
double MQ135::getVoltage() {
|
||||
return (double)analogRead(_pin) * VStep;
|
||||
}
|
||||
|
||||
double MQ135::getVoltage() {
|
||||
int value = analogRead(_pin);
|
||||
double voltage = (double)(value * VIn) / (double)(Resolution - 1);
|
||||
return voltage;
|
||||
double MQ135::getResistance() {
|
||||
double voltage = getVoltage();
|
||||
double rs = ((VIn * RL) / voltage) - RL;
|
||||
if (rs < 0) {
|
||||
rs = 0;
|
||||
}
|
||||
return rs;
|
||||
}
|
||||
|
||||
double MQ135::getPPM(float a, float b) {
|
||||
double ratio = getRS() / R0;
|
||||
double ratio = getResistance() / R0;
|
||||
double ppm = a * pow(ratio, b);
|
||||
if (ppm < 0) {
|
||||
ppm = 0;
|
||||
}
|
||||
return ppm;
|
||||
}
|
||||
|
||||
|
@ -47,10 +51,15 @@ double MQ135::getTolueno() {
|
|||
}
|
||||
|
||||
float MQ135::getR0() {
|
||||
double r0 = getRS() / 3.6;
|
||||
double r0 = getResistance() / 3.6;
|
||||
return r0;
|
||||
}
|
||||
|
||||
double MQ135::getR0By(float ppm, float a, float b) {
|
||||
double tmp = (log10(ppm / a) / b) - log10(RL);
|
||||
return pow(10, tmp);
|
||||
}
|
||||
|
||||
void MQ135::setR0(float r0) {
|
||||
R0 = r0;
|
||||
}
|
||||
|
|
|
@ -9,23 +9,28 @@
|
|||
|
||||
#define ATMOCO2 397.13
|
||||
|
||||
const double VStep = (double)VIn / (Resolution - 1);
|
||||
|
||||
class MQ135 {
|
||||
private:
|
||||
uint8_t _pin;
|
||||
float R0;
|
||||
double getPPM(float a, float b);
|
||||
|
||||
public:
|
||||
MQ135(uint8_t pin);
|
||||
float getR0();
|
||||
double getR0By(float ppm, float a, float b);
|
||||
void setR0(float r0);
|
||||
|
||||
double getVoltage();
|
||||
double getResistance();
|
||||
double getPPM(float a, float b);
|
||||
|
||||
double getAcetona();
|
||||
double getAlcohol();
|
||||
double getCO();
|
||||
double getCO2();
|
||||
double getNH4();
|
||||
double getTolueno();
|
||||
float getR0();
|
||||
void setR0(float r0);
|
||||
double getRS();
|
||||
double getVoltage();
|
||||
};
|
||||
#endif
|
||||
|
|
13
src/main.cpp
13
src/main.cpp
|
@ -1,5 +1,5 @@
|
|||
#include <Arduino.h>
|
||||
#include <MQ135New.h>
|
||||
#include "MQ135New.h"
|
||||
|
||||
#define PIN_MQ135 A0
|
||||
#define PIN_LED_GREEN DD2
|
||||
|
@ -22,12 +22,17 @@ void printValues(float ppm, float temp, float humidity);
|
|||
|
||||
void loop() {
|
||||
float value = co2_sensor.getCO2();
|
||||
Serial.print("co2: ");
|
||||
Serial.println(value);
|
||||
Serial.print("rs: ");
|
||||
Serial.println(co2_sensor.getRS());
|
||||
Serial.print("resistance: ");
|
||||
Serial.println(co2_sensor.getResistance());
|
||||
Serial.print("voltage: ");
|
||||
Serial.println(co2_sensor.getVoltage());
|
||||
delay(500);
|
||||
Serial.print("r01: ");
|
||||
Serial.println(co2_sensor.getR0());
|
||||
Serial.print("r02: ");
|
||||
Serial.println(co2_sensor.getR0By(1, 110.47, -2.862));
|
||||
delay(2000);
|
||||
// if (count >= maxCount || count < 0) {
|
||||
// float temp = bme.readTemperature();
|
||||
// float humidity = bme.readHumidity();
|
||||
|
|
Loading…
Add table
Reference in a new issue