From 48cbcd7d344d990b90fa230cb5ee3e3c7a6ae71e Mon Sep 17 00:00:00 2001 From: nilo Date: Sat, 31 Oct 2020 12:02:27 +0100 Subject: [PATCH] code cleanup --- ebk_co2ampel/ebk_co2ampel.ino | 113 +++++++++++++++------------------- 1 file changed, 48 insertions(+), 65 deletions(-) diff --git a/ebk_co2ampel/ebk_co2ampel.ino b/ebk_co2ampel/ebk_co2ampel.ino index 6393ea7..a286c23 100644 --- a/ebk_co2ampel/ebk_co2ampel.ino +++ b/ebk_co2ampel/ebk_co2ampel.ino @@ -1,89 +1,72 @@ #include -#include "MHZ19.h" +#include "MHZ19.h" +#include "SSD1306Wire.h" +#define RX_PIN 16 +#define TX_PIN 17 +#define BAUDRATE 9600 - #include // Only needed for Arduino 1.6.5 and earlier -#include "SSD1306Wire.h" // legacy include: `#include "SSD1306.h"` -#define RX_PIN 16 // Rx pin which the MHZ19 Tx pin is attached to -#define TX_PIN 17 // Tx pin which the MHZ19 Rx pin is attached to -#define BAUDRATE 9600 // Device to MH-Z19 Serial baudrate (should not be changed) +MHZ19 myMHZ19; -MHZ19 myMHZ19; // Constructor for library - -HardwareSerial mySerial(1); // (ESP32 Example) create device to MH-Z19 serial - SSD1306Wire display(0x3c, 21, 22); +HardwareSerial mySerial(1); +SSD1306Wire display(0x3c, 21, 22); unsigned long getDataTimer = 0; int lastvals[120]; +int dheight; void setup() { - Serial.begin(9600); // Device to serial monitor feedback - - mySerial.begin(BAUDRATE, SERIAL_8N1, RX_PIN, TX_PIN); // (ESP32 Example) device to MH-Z19 serial start - myMHZ19.begin(mySerial); // *Serial(Stream) refence must be passed to library begin(). + Serial.begin(9600); + mySerial.begin(BAUDRATE, SERIAL_8N1, RX_PIN, TX_PIN); + myMHZ19.begin(mySerial); display.init(); - - // display.flipScreenVertically(); - display.setContrast(255); delay(1000); display.clear(); + dheight = display.getHeight(); - myMHZ19.autoCalibration(); // Turn auto calibration ON (OFF autoCalibration(false)) + myMHZ19.autoCalibration(); + for (int x; x <= 119; x = x + 1) { + lastvals[x] = -1; + } + +} - for (int x; x<=119; x=x+1) { - lastvals[x] = -1; - } - +int calc_vpos_for_co2(int co2val, int display_height) { + return display_height - int((float(display_height) / 3000) * co2val); } void loop() { - if (millis() - getDataTimer >= 2000) - { - int CO2; - - /* note: getCO2() default is command "CO2 Unlimited". This returns the correct CO2 reading even - if below background CO2 levels or above range (useful to validate sensor). You can use the - usual documented command with getCO2(false) */ - CO2 = myMHZ19.getCO2(); // Request CO2 (as ppm) - - for (int x=1; x<=119; x=x+1) { - lastvals[x-1] = lastvals[x]; - } - - lastvals[119] = CO2; - - - display.clear(); - - for (int h=1; h<120; h=h+1) { - int curval = lastvals[h]; - if (curval > 0) { - int vpos = display.getHeight() - int((float(display.getHeight()) / 3000) * lastvals[h]); - int vpos_1 = display.getHeight() - int((float(display.getHeight()) / 3000) * lastvals[h-1]); - - display.drawLine(h-1, vpos_1, h, vpos); - - } - } - - int8_t Temp; - Temp = myMHZ19.getTemperature(); // Request Temperature (as Celsius) - - display.setLogBuffer(5, 30); - display.println(CO2); - display.drawLogBuffer(0, 0); - display.display(); - Serial.print("CO2 (ppm): "); - Serial.println(CO2); - Serial.println(); - Serial.print("Temperature (C): "); - Serial.println(Temp); - - getDataTimer = millis(); + if (millis() - getDataTimer >= 5000) + { + int CO2; + CO2 = myMHZ19.getCO2(); + for (int x = 1; x <= 119; x = x + 1) { + lastvals[x - 1] = lastvals[x]; } + lastvals[119] = CO2; + display.clear(); + for (int h = 1; h < 120; h = h + 1) { + int curval = lastvals[h]; + if (curval > 0) { + int vpos = calc_vpos_for_co2(lastvals[h], dheight); + int vpos_last = calc_vpos_for_co2(lastvals[h - 1], dheight); + display.drawLine(h - 1, vpos_last, h, vpos); + + } + } + + + display.setLogBuffer(5, 30); + display.println(CO2); + display.drawLogBuffer(0, 0); + display.display(); + Serial.print("CO2 (ppm): "); + Serial.println(CO2); + getDataTimer = millis(); + } }