-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws2-test.ino
71 lines (53 loc) · 1.59 KB
/
ws2-test.ino
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
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h> // library for I2C/SPI temperture and humidity sensor - BMP280
#include <BH1750.h> // library for I2C light intensiy sensor - BH1750
#define mhz19pin D3 // PWM pin input for CO2 sensor - MH-Z19
#define preheattime 90 // (seconds) pre-heat startup time for MH-Z19
#define preheatstatus 0 // 1 - enable preheat ; 0 - disable preheat
Adafruit_BMP280 bmp280;
BH1750 bh1750;
long lastMillis = 0;
void setup() {
Serial.begin(9600);
Serial.println(F("Weather Station v.2 test"));
pinMode(mhz19pin, INPUT);
bh1750.begin();
bmp280.begin();
#if preheatstatus
Serial.println(F("Preheating..."));
for (int8_t i = 0; i < preheattime; i++) {
uint8_t timeremain = preheattime - i;
Serial.println(timeremain);
delay(1000);
}
#endif
}
void loop() {
long nowMillis = millis();
if (nowMillis - lastMillis > 1000) { // update value every 1 second
lastMillis = nowMillis;
uint16_t lux = bh1750.readLightLevel();
uint16_t co2ppm = readCO2PWM();
Serial.print(bmp280.readTemperature());
Serial.println(" *C");
Serial.print(bmp280.readPressure());
Serial.println(" Pa");
Serial.print(lux);
Serial.println(" lux");
Serial.print(co2ppm);
Serial.println(" ppm");
Serial.println(F("----------------"));
}
}
// read CO2
int readCO2PWM() {
unsigned long th, tl, ppm_pwm = 0;
do {
th = pulseIn(mhz19pin, HIGH, 1004000) / 1000;
tl = 1004 - th;
ppm_pwm = 5000 * (th - 2) / (th + tl - 4);
}
while (th == 0);
return ppm_pwm;
}