forked from github/ebk_co2ampel
code cleanup
This commit is contained in:
parent
6569a14ebf
commit
48cbcd7d34
1 changed files with 48 additions and 65 deletions
|
@ -1,37 +1,31 @@
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include "MHZ19.h"
|
#include "MHZ19.h"
|
||||||
|
#include "SSD1306Wire.h"
|
||||||
|
#define RX_PIN 16
|
||||||
|
#define TX_PIN 17
|
||||||
|
#define BAUDRATE 9600
|
||||||
|
|
||||||
#include <Wire.h> // Only needed for Arduino 1.6.5 and earlier
|
MHZ19 myMHZ19;
|
||||||
#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; // Constructor for library
|
HardwareSerial mySerial(1);
|
||||||
|
|
||||||
HardwareSerial mySerial(1); // (ESP32 Example) create device to MH-Z19 serial
|
|
||||||
SSD1306Wire display(0x3c, 21, 22);
|
SSD1306Wire display(0x3c, 21, 22);
|
||||||
|
|
||||||
unsigned long getDataTimer = 0;
|
unsigned long getDataTimer = 0;
|
||||||
int lastvals[120];
|
int lastvals[120];
|
||||||
|
int dheight;
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
Serial.begin(9600); // Device to serial monitor feedback
|
Serial.begin(9600);
|
||||||
|
mySerial.begin(BAUDRATE, SERIAL_8N1, RX_PIN, TX_PIN);
|
||||||
mySerial.begin(BAUDRATE, SERIAL_8N1, RX_PIN, TX_PIN); // (ESP32 Example) device to MH-Z19 serial start
|
myMHZ19.begin(mySerial);
|
||||||
myMHZ19.begin(mySerial); // *Serial(Stream) refence must be passed to library begin().
|
|
||||||
display.init();
|
display.init();
|
||||||
|
|
||||||
// display.flipScreenVertically();
|
|
||||||
|
|
||||||
display.setContrast(255);
|
display.setContrast(255);
|
||||||
delay(1000);
|
delay(1000);
|
||||||
display.clear();
|
display.clear();
|
||||||
|
dheight = display.getHeight();
|
||||||
|
|
||||||
myMHZ19.autoCalibration(); // Turn auto calibration ON (OFF autoCalibration(false))
|
myMHZ19.autoCalibration();
|
||||||
|
|
||||||
|
|
||||||
for (int x; x <= 119; x = x + 1) {
|
for (int x; x <= 119; x = x + 1) {
|
||||||
lastvals[x] = -1;
|
lastvals[x] = -1;
|
||||||
}
|
}
|
||||||
|
@ -39,40 +33,33 @@ void setup()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int calc_vpos_for_co2(int co2val, int display_height) {
|
||||||
|
return display_height - int((float(display_height) / 3000) * co2val);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
if (millis() - getDataTimer >= 2000)
|
if (millis() - getDataTimer >= 5000)
|
||||||
{
|
{
|
||||||
int CO2;
|
int CO2;
|
||||||
|
CO2 = myMHZ19.getCO2();
|
||||||
/* 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) {
|
for (int x = 1; x <= 119; x = x + 1) {
|
||||||
lastvals[x - 1] = lastvals[x];
|
lastvals[x - 1] = lastvals[x];
|
||||||
}
|
}
|
||||||
|
|
||||||
lastvals[119] = CO2;
|
lastvals[119] = CO2;
|
||||||
|
|
||||||
|
|
||||||
display.clear();
|
display.clear();
|
||||||
|
|
||||||
for (int h = 1; h < 120; h = h + 1) {
|
for (int h = 1; h < 120; h = h + 1) {
|
||||||
int curval = lastvals[h];
|
int curval = lastvals[h];
|
||||||
if (curval > 0) {
|
if (curval > 0) {
|
||||||
int vpos = display.getHeight() - int((float(display.getHeight()) / 3000) * lastvals[h]);
|
int vpos = calc_vpos_for_co2(lastvals[h], dheight);
|
||||||
int vpos_1 = display.getHeight() - int((float(display.getHeight()) / 3000) * lastvals[h-1]);
|
int vpos_last = calc_vpos_for_co2(lastvals[h - 1], dheight);
|
||||||
|
display.drawLine(h - 1, vpos_last, h, vpos);
|
||||||
display.drawLine(h-1, vpos_1, h, vpos);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int8_t Temp;
|
|
||||||
Temp = myMHZ19.getTemperature(); // Request Temperature (as Celsius)
|
|
||||||
|
|
||||||
display.setLogBuffer(5, 30);
|
display.setLogBuffer(5, 30);
|
||||||
display.println(CO2);
|
display.println(CO2);
|
||||||
|
@ -80,10 +67,6 @@ void loop()
|
||||||
display.display();
|
display.display();
|
||||||
Serial.print("CO2 (ppm): ");
|
Serial.print("CO2 (ppm): ");
|
||||||
Serial.println(CO2);
|
Serial.println(CO2);
|
||||||
Serial.println();
|
|
||||||
Serial.print("Temperature (C): ");
|
|
||||||
Serial.println(Temp);
|
|
||||||
|
|
||||||
getDataTimer = millis();
|
getDataTimer = millis();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue