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,89 +1,72 @@
|
|||
#include <Arduino.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
|
||||
#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();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue