Compare commits

..

2 commits

Author SHA1 Message Date
GrafZ3pp3lin
0fa26e334d fix loading + htu21 sensor 2021-07-21 22:20:03 +02:00
GrafZ3pp3lin
beb41310c1 update 2021-07-10 09:32:32 +02:00
2 changed files with 141 additions and 33 deletions

View file

@ -8,12 +8,11 @@
; Please visit documentation for the other options and examples ; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html ; https://docs.platformio.org/page/projectconf.html
; nanoatmega328new
[env:arduino] [env:arduino]
platform = atmelavr platform = atmelavr
board = uno board = nanoatmega328new
framework = arduino framework = arduino
lib_deps = lib_deps =
; adafruit/Adafruit BME280 Library @ ^2.1.2 ; adafruit/Adafruit BME280 Library @ ^2.1.2
; miguel5612/MQUnifiedsensor @ ^2.0.1
sparkfun/SparkFun HTU21D Humidity and Temperature Sensor Breakout @ ^1.1.3 sparkfun/SparkFun HTU21D Humidity and Temperature Sensor Breakout @ ^1.1.3

View file

@ -2,7 +2,7 @@
#include <MQ135.h> #include <MQ135.h>
#include <SparkFunHTU21D.h> #include <SparkFunHTU21D.h>
#define PIN_MQ135 A0 #define PIN_MQ135 A7
#define PIN_NOISE 2 #define PIN_NOISE 2
#define PIN_LED_GREEN 3 #define PIN_LED_GREEN 3
#define PIN_LED_GREEN2 4 #define PIN_LED_GREEN2 4
@ -11,52 +11,161 @@
#define PIN_LED_RED 7 #define PIN_LED_RED 7
#define PIN_LED_RED2 8 #define PIN_LED_RED2 8
void printValues(float ppm, float temp, float humidity);
const int maxCount = 10;
int count = 0;
bool noise = false;
bool noiseActive = false;
// loading
bool loading = true;
int firstRead;
const int loadingDelay = 200;
int loadingCount = 30000 / loadingDelay;
MQ135 co2_sensor(PIN_MQ135); MQ135 co2_sensor(PIN_MQ135);
HTU21D envSensor; HTU21D envSensor;
void setup() { void setup() {
pinMode(PIN_LED_GREEN, OUTPUT);
pinMode(PIN_LED_GREEN2, OUTPUT);
pinMode(PIN_LED_YELLOW, OUTPUT);
pinMode(PIN_LED_YELLOW2, OUTPUT);
pinMode(PIN_LED_RED, OUTPUT);
pinMode(PIN_LED_RED2, OUTPUT);
pinMode(PIN_NOISE, OUTPUT);
Serial.begin(9600); Serial.begin(9600);
co2_sensor.setR0(200); co2_sensor.setR0(200);
// if (!bme.begin(0x76)) {
// Serial.println(F("Could not find a valid BME280 sensor, check wiring!"));
// }
envSensor.begin(); envSensor.begin();
} }
void loop() { void loop() {
float temp = envSensor.readTemperature(); if (loading) {
float humidity = envSensor.readHumidity(); if (loadingCount <= 0) {
loading = false;
count = -1;
return;
}
float r0c = co2_sensor.getCorrectedR0(temp, humidity); int temp = analogRead(PIN_MQ135);
float in = analogRead(PIN_MQ135); if (temp > firstRead) {
double v = co2_sensor.getVoltage(); firstRead = temp;
double r = co2_sensor.getResistance(); }
float r0 = co2_sensor.getR0(); else if (temp < (firstRead - 100) && loadingCount > 10) {
loadingCount = 10;
}
double co2 = co2_sensor.getCO2(); digitalWrite(PIN_LED_GREEN, 0);
double co2c = co2_sensor.getCorrectedCO2(temp, humidity); digitalWrite(PIN_LED_GREEN2, 0);
digitalWrite(PIN_LED_YELLOW, 0);
digitalWrite(PIN_LED_YELLOW2, 0);
digitalWrite(PIN_LED_RED, 0);
digitalWrite(PIN_LED_RED2, 0);
Serial.print("Analog Read: "); switch (count)
Serial.println(in); {
Serial.print("Voltage: "); case 1:
Serial.println(v); digitalWrite(PIN_LED_RED2, 1);
Serial.print("Resistance: "); break;
Serial.println(r); case 2:
digitalWrite(PIN_LED_RED, 1);
digitalWrite(PIN_LED_RED2, 1);
break;
case 3:
digitalWrite(PIN_LED_RED, 1);
digitalWrite(PIN_LED_YELLOW2, 1);
break;
case 4:
digitalWrite(PIN_LED_YELLOW2, 1);
digitalWrite(PIN_LED_YELLOW, 1);
break;
case 5:
digitalWrite(PIN_LED_YELLOW, 1);
digitalWrite(PIN_LED_GREEN2, 1);
break;
case 6:
digitalWrite(PIN_LED_GREEN2, 1);
digitalWrite(PIN_LED_GREEN, 1);
break;
case 7:
digitalWrite(PIN_LED_GREEN, 1);
break;
case 10:
count = 0;
break;
default:
break;
}
Serial.print("R0: "); loadingCount--;
Serial.println(r0); count++;
delay(loadingDelay);
return;
}
if (count >= maxCount || count < 0) {
float temp = envSensor.readTemperature();
float humidity = envSensor.readHumidity();
float ppm = co2_sensor.getCorrectedCO2(temp, humidity);
count = 0;
noiseActive = false;
digitalWrite(PIN_LED_GREEN, 0);
digitalWrite(PIN_LED_GREEN2, 0);
digitalWrite(PIN_LED_YELLOW, 0);
digitalWrite(PIN_LED_YELLOW2, 0);
digitalWrite(PIN_LED_RED, 0);
digitalWrite(PIN_LED_RED2, 0);
if (ppm < 900) {
digitalWrite(PIN_LED_GREEN, 1);
}
if (ppm > 750 && ppm < 1100) {
digitalWrite(PIN_LED_GREEN2, 1);
}
if (ppm > 900 && ppm < 1900) {
digitalWrite(PIN_LED_YELLOW, 1);
}
if (ppm > 1500 && ppm < 2200) {
digitalWrite(PIN_LED_YELLOW2, 1);
}
if (ppm > 2000 && ppm < 2800) {
digitalWrite(PIN_LED_RED, 1);
}
if (ppm > 2500) {
digitalWrite(PIN_LED_RED2, 1);
}
if (ppm > 3000) {
noiseActive = true;
}
printValues(ppm, temp, humidity);
}
if (noiseActive) {
noise = !noise;
digitalWrite(PIN_NOISE, noise);
}
else {
digitalWrite(PIN_NOISE, 0);
}
delay(100);
count++;
}
void printValues(float ppm, float temp, float humidity) {
Serial.print("ppm: ");
Serial.println(ppm);
Serial.print("Temperature: "); Serial.print("Temperature: ");
Serial.println(temp); Serial.println(temp);
Serial.print("Humidity: "); Serial.print("Humidity: ");
Serial.println(humidity); Serial.println(humidity);
Serial.print("R0 Corrected: ");
Serial.println(r0c);
Serial.print("CO2: ");
Serial.println(co2);
Serial.print("CO2 Corrected: ");
Serial.println(co2c);
Serial.println("-----------------------------");
delay(1000);
} }