fix loading + htu21 sensor

This commit is contained in:
GrafZ3pp3lin 2021-07-21 22:20:03 +02:00
parent beb41310c1
commit 0fa26e334d
2 changed files with 84 additions and 70 deletions

View file

@ -13,5 +13,6 @@ platform = atmelavr
board = nanoatmega328new 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 ; miguel5612/MQUnifiedsensor @ ^2.0.1
sparkfun/SparkFun HTU21D Humidity and Temperature Sensor Breakout @ ^1.1.3

View file

@ -1,6 +1,6 @@
#include <Arduino.h> #include <Arduino.h>
#include <MQ135.h> #include <MQ135.h>
#include <Adafruit_BME280.h> #include <SparkFunHTU21D.h>
#define PIN_MQ135 A7 #define PIN_MQ135 A7
#define PIN_NOISE 2 #define PIN_NOISE 2
@ -11,18 +11,22 @@
#define PIN_LED_RED 7 #define PIN_LED_RED 7
#define PIN_LED_RED2 8 #define PIN_LED_RED2 8
void printValues(); void printValues(float ppm, float temp, float humidity);
const int maxCount = 10; const int maxCount = 10;
int count = 0; int count = 0;
bool noise = false; bool noise = false;
bool noiseActive = false; bool noiseActive = false;
bool loading = false;
float ppm; // loading
bool loading = true;
int firstRead;
const int loadingDelay = 200;
int loadingCount = 30000 / loadingDelay;
MQ135 co2_sensor(PIN_MQ135); MQ135 co2_sensor(PIN_MQ135);
Adafruit_BME280 bme; HTU21D envSensor;
void setup() { void setup() {
pinMode(PIN_LED_GREEN, OUTPUT); pinMode(PIN_LED_GREEN, OUTPUT);
@ -33,68 +37,83 @@ void setup() {
pinMode(PIN_LED_RED2, OUTPUT); pinMode(PIN_LED_RED2, OUTPUT);
pinMode(PIN_NOISE, OUTPUT); pinMode(PIN_NOISE, OUTPUT);
Serial.begin(9600); Serial.begin(9600);
co2_sensor.setR0(130); co2_sensor.setR0(200);
// if (!bme.begin(0x76)) { // if (!bme.begin(0x76)) {
// Serial.println(F("Could not find a valid BME280 sensor, check wiring!")); // Serial.println(F("Could not find a valid BME280 sensor, check wiring!"));
// } // }
envSensor.begin();
} }
void loop() { void loop() {
// if (loading) { if (loading) {
// digitalWrite(PIN_LED_GREEN, 0); if (loadingCount <= 0) {
// digitalWrite(PIN_LED_GREEN2, 0); loading = false;
// digitalWrite(PIN_LED_YELLOW, 0); count = -1;
// digitalWrite(PIN_LED_YELLOW2, 0); return;
// digitalWrite(PIN_LED_RED, 0); }
// digitalWrite(PIN_LED_RED2, 0);
// switch (count) int temp = analogRead(PIN_MQ135);
// { if (temp > firstRead) {
// case 1: firstRead = temp;
// digitalWrite(PIN_LED_RED2, 1); }
// break; else if (temp < (firstRead - 100) && loadingCount > 10) {
// case 2: loadingCount = 10;
// digitalWrite(PIN_LED_RED, 1); }
// digitalWrite(PIN_LED_RED2, 1);
// break; digitalWrite(PIN_LED_GREEN, 0);
// case 3: digitalWrite(PIN_LED_GREEN2, 0);
// digitalWrite(PIN_LED_RED, 1); digitalWrite(PIN_LED_YELLOW, 0);
// digitalWrite(PIN_LED_YELLOW2, 1); digitalWrite(PIN_LED_YELLOW2, 0);
// break; digitalWrite(PIN_LED_RED, 0);
// case 4: digitalWrite(PIN_LED_RED2, 0);
// digitalWrite(PIN_LED_YELLOW2, 1);
// digitalWrite(PIN_LED_YELLOW, 1); switch (count)
// break; {
// case 5: case 1:
// digitalWrite(PIN_LED_YELLOW, 1); digitalWrite(PIN_LED_RED2, 1);
// digitalWrite(PIN_LED_GREEN2, 1); break;
// break; case 2:
// case 6: digitalWrite(PIN_LED_RED, 1);
// digitalWrite(PIN_LED_GREEN2, 1); digitalWrite(PIN_LED_RED2, 1);
// digitalWrite(PIN_LED_GREEN, 1); break;
// break; case 3:
// case 7: digitalWrite(PIN_LED_RED, 1);
// digitalWrite(PIN_LED_GREEN, 1); digitalWrite(PIN_LED_YELLOW2, 1);
// break; break;
// case 10: case 4:
// count = 0; digitalWrite(PIN_LED_YELLOW2, 1);
// break; 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: default:
// break; break;
// } }
// count++; loadingCount--;
// delay(200); count++;
// return; delay(loadingDelay);
// } return;
}
if (count >= maxCount || count < 0) { if (count >= maxCount || count < 0) {
// float temp = bme.readTemperature(); float temp = envSensor.readTemperature();
// float humidity = bme.readHumidity(); float humidity = envSensor.readHumidity();
// ppm = co2_sensor.getCorrectedCO2(temp, humidity); float ppm = co2_sensor.getCorrectedCO2(temp, humidity);
ppm = co2_sensor.getCO2();
count = 0; count = 0;
noiseActive = false; noiseActive = false;
@ -127,32 +146,26 @@ void loop() {
noiseActive = true; noiseActive = true;
} }
printValues(); printValues(ppm, temp, humidity);
} }
if (noiseActive) { if (noiseActive) {
noise = !noise; noise = !noise;
// digitalWrite(PIN_NOISE, noise); digitalWrite(PIN_NOISE, noise);
} }
else { else {
// digitalWrite(PIN_NOISE, 0); digitalWrite(PIN_NOISE, 0);
} }
delay(100); delay(100);
count++; count++;
} }
void printValues() { void printValues(float ppm, float temp, float humidity) {
// float temp = bme.readTemperature();
// float humidity = bme.readHumidity();
// float ppm = co2_sensor.getCO2();
// float cppm = co2_sensor.getCorrectedCO2(temp, humidity);
Serial.print("ppm: "); Serial.print("ppm: ");
Serial.println(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("corrected ppm: ");
// Serial.println(cppm);
} }