ebk-unhb-co2ampel/ebk_co2ampel/ebk_co2ampel.ino

73 lines
1.4 KiB
Arduino
Raw Normal View History

2020-10-31 01:20:32 +01:00
#include <Arduino.h>
2020-10-31 12:02:27 +01:00
#include "MHZ19.h"
#include "SSD1306Wire.h"
#define RX_PIN 16
#define TX_PIN 17
#define BAUDRATE 9600
2020-10-31 01:20:32 +01:00
2020-10-31 12:02:27 +01:00
MHZ19 myMHZ19;
2020-10-31 01:20:32 +01:00
2020-10-31 12:02:27 +01:00
HardwareSerial mySerial(1);
SSD1306Wire display(0x3c, 21, 22);
2020-10-31 01:20:32 +01:00
unsigned long getDataTimer = 0;
int lastvals[120];
2020-10-31 12:02:27 +01:00
int dheight;
2020-10-31 01:20:32 +01:00
void setup()
{
2020-10-31 12:02:27 +01:00
Serial.begin(9600);
mySerial.begin(BAUDRATE, SERIAL_8N1, RX_PIN, TX_PIN);
myMHZ19.begin(mySerial);
2020-10-31 01:20:32 +01:00
display.init();
display.setContrast(255);
delay(1000);
display.clear();
2020-10-31 12:02:27 +01:00
dheight = display.getHeight();
2020-10-31 01:20:32 +01:00
2020-10-31 12:02:27 +01:00
myMHZ19.autoCalibration();
for (int x; x <= 119; x = x + 1) {
lastvals[x] = -1;
}
2020-10-31 01:20:32 +01:00
2020-10-31 12:02:27 +01:00
}
2020-10-31 01:20:32 +01:00
2020-10-31 12:02:27 +01:00
int calc_vpos_for_co2(int co2val, int display_height) {
return display_height - int((float(display_height) / 3000) * co2val);
2020-10-31 01:20:32 +01:00
}
void loop()
{
2020-10-31 12:02:27 +01:00
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);
}
}
2020-10-31 01:20:32 +01:00
2020-10-31 12:02:27 +01:00
display.setLogBuffer(5, 30);
display.println(CO2);
display.drawLogBuffer(0, 0);
display.display();
Serial.print("CO2 (ppm): ");
Serial.println(CO2);
getDataTimer = millis();
}
2020-10-31 01:20:32 +01:00
}