#include <SoftwareSerial.h>
+#include <LiquidCrystal.h>
#include "MHZ19.h"
const int rx_pin = 2; // connect to TX on MHZ19
const int tx_pin = 3; // connect to RX on MHZ19
+const int led_green_pin = 4;
+const int led_yellow_pin = 5;
+const int led_red_pin = 6;
+
+const int buzzer_pin = 13;
+
+const int co2_warning_thr = 1000;
+const int co2_alarm_thr = 1500;
+
+const unsigned long warmuptime = 60000; // 1 min.
+const unsigned long cooldowntime = 60000; // 1 min.
MHZ19 mhz19 = MHZ19(rx_pin,tx_pin);
+LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
+
+unsigned long time;
+unsigned long lastwarntime = 0;
+int co2 = 0;
+int temp = 0;
+int lastppm = 0;
+void buzz(unsigned long pause, int count) {
+ for (int i = 0; i <= count; i++) {
+ digitalWrite(buzzer_pin, HIGH);
+ delay(pause);
+ digitalWrite(buzzer_pin, LOW);
+ if (i < count - 1) delay(pause);
+ }
+}
void setup()
{
- Serial.begin(115200);
- mhz19.begin(rx_pin, tx_pin);
+ Serial.begin(115200);
+ Serial.println("Time,CO2,Temp");
+
+ // MH-Z19
+ mhz19.begin(rx_pin, tx_pin);
+
+ // LCD
+ lcd.begin(16, 2);
+ lcd.setCursor(0, 0);
+ lcd.print("Airing Butler");
+ lcd.setCursor(0, 1);
+ lcd.print("says hello!");
+
+ // LEDs
+ pinMode(led_green_pin, OUTPUT);
+ pinMode(led_yellow_pin, OUTPUT);
+ pinMode(led_red_pin, OUTPUT);
+ digitalWrite(led_green_pin, HIGH);
+ delay(500);
+ digitalWrite(led_green_pin, LOW);
+ digitalWrite(led_yellow_pin, HIGH);
+ delay(500);
+ digitalWrite(led_yellow_pin, LOW);
+ digitalWrite(led_red_pin, HIGH);
+ delay(500);
+ digitalWrite(led_red_pin, LOW);
+
+ // buzzer
+ pinMode(buzzer_pin, OUTPUT);
+ buzz(200, 1);
+
+ lcd.clear();
}
void loop()
{
- measurement_t m = mhz19.getMeasurement();
-
- Serial.print("co2: ");
- Serial.println(m.co2_ppm);
- Serial.print("temp: ");
- Serial.println(m.temperature);
-
- delay(500);
-}
+ time = millis();
+ measurement_t m = mhz19.getMeasurement();
+ co2 = m.co2_ppm;
+ temp = m.temperature;
+
+ Serial.print(time);
+ Serial.print(",");
+ Serial.print(co2);
+ Serial.print(",");
+ Serial.println(temp);
+ // LEDs
+ digitalWrite(led_red_pin, co2 >= co2_alarm_thr);
+ digitalWrite(led_yellow_pin, co2 >= co2_warning_thr && co2 < co2_alarm_thr);
+ digitalWrite(led_green_pin, co2 < co2_warning_thr);
+
+ // LCD
+ if (time < warmuptime && (co2 == -1 || co2 == 410)) {
+ lcd.begin(16, 2);
+ lcd.setCursor(0, 0);
+ lcd.print("Airing Butler");
+ lcd.setCursor(0, 1);
+ lcd.print("is warming up...");
+ } else {
+ lcd.begin(16 ,2);
+ lcd.setCursor(0, 0);
+ lcd.print("CO2: ");
+ lcd.print(co2);
+ lcd.print(" ppm ");
+ lcd.setCursor(0, 1);
+ lcd.print("Temp: ");
+ lcd.print(temp);
+ lcd.print(" C +/- ");
+ }
+
+ // buzzer
+ if (co2 >= co2_alarm_thr && lastppm < co2_alarm_thr && time - lastwarntime > cooldowntime) {
+ buzz(100, 3);
+ lastwarntime = time;
+ }
+ if (co2 >= co2_warning_thr && lastppm < co2_warning_thr && time - lastwarntime > cooldowntime) {
+ buzz(500, 1);
+ lastwarntime = time;
+ }
+
+ lastppm = co2;
+ delay(5000);
+}