-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmh-z19_co2_meter.ino
More file actions
82 lines (73 loc) · 1.72 KB
/
mh-z19_co2_meter.ino
File metadata and controls
82 lines (73 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
#define pwmPin 7
int preheatSec = 120;
int prevVal = LOW;
long th, tl, h, l, ppm = 0;
void PWM_ISR() {
long tt = millis();
int val = digitalRead(pwmPin);
if (val == HIGH) {
if (val != prevVal) {
h = tt;
tl = h - l;
prevVal = val;
}
} else {
if (val != prevVal) {
l = tt;
th = l - h;
prevVal = val;
ppm = 2000 * (th - 2) / (th + tl - 4);
}
}
}
void setup() {
Serial.begin(115200);
pinMode(pwmPin, INPUT);
attachInterrupt(digitalPinToInterrupt(pwmPin), PWM_ISR, CHANGE);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x64)
display.setTextColor(WHITE);
}
void displayPreheating(int secLeft) {
display.setTextSize(2);
display.println("PREHEATING");
display.setTextSize(1);
display.println();
display.setTextSize(5);
display.print(" ");
display.print(secLeft);
display.display();
}
void displayPPM(long ppm) {
display.setTextSize(2);
display.println("CO2 PPM");
display.setTextSize(1);
display.println();
display.setTextSize(5);
if (ppm < 1000) {
display.print(" ");
}
display.print(ppm);
display.display();
Serial.println(ppm);
}
void loop() {
display.clearDisplay();
display.setCursor(0,0);
if (preheatSec > 0) {
displayPreheating(preheatSec);
preheatSec--;
}
else {
displayPPM(ppm);
}
delay(1000);
}