]> ToastFreeware Gitweb - toast/airingbutler.git/blob - airingbutler.ino
b4579e6fe78e2a46ec31fb07fdd016c51364a722
[toast/airingbutler.git] / airingbutler.ino
1 #include <SoftwareSerial.h>
2 #include <LiquidCrystal.h>
3 #include "MHZ19.h"
4
5 const int rx_pin = 2; // connect to TX on MHZ19
6 const int tx_pin = 3; // connect to RX on MHZ19
7
8 const int led_green_pin  = 4;
9 const int led_yellow_pin = 5;
10 const int led_red_pin    = 6;
11
12 const int buzzer_pin = 13;
13
14 const int co2_warning_thr = 1000;
15 const int co2_alarm_thr   = 1500;
16
17 const unsigned long warmuptime = 60000; // 1 min.
18
19 MHZ19 mhz19 = MHZ19(rx_pin,tx_pin);
20 LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
21
22 unsigned long time;
23 int co2 = 0;
24 int temp = 0;
25 int lastppm = 0;
26
27 void buzz(unsigned long pause, int count) {
28         for (int i = 0; i <= count; i++) {
29                 digitalWrite(buzzer_pin, HIGH);
30                 delay(pause);
31                 digitalWrite(buzzer_pin, LOW);
32                 if (i < count - 1) delay(pause);
33         }
34 }
35
36 void setup()
37 {
38         Serial.begin(115200);
39         Serial.println("Time,CO2,Temp");
40
41         // MH-Z19
42         mhz19.begin(rx_pin, tx_pin);
43
44         // LCD
45         lcd.begin(16, 2);
46         lcd.setCursor(0, 0);
47         lcd.print("Airing Butler");
48         lcd.setCursor(0, 1);
49         lcd.print("says hello!");
50
51         // LEDs
52         pinMode(led_green_pin, OUTPUT);
53         pinMode(led_yellow_pin, OUTPUT);
54         pinMode(led_red_pin, OUTPUT);
55         digitalWrite(led_green_pin, HIGH);
56         delay(500);
57         digitalWrite(led_green_pin, LOW);
58         digitalWrite(led_yellow_pin, HIGH);
59         delay(500);
60         digitalWrite(led_yellow_pin, LOW);
61         digitalWrite(led_red_pin, HIGH);
62         delay(500);
63         digitalWrite(led_red_pin, LOW);
64
65         // buzzer
66         pinMode(buzzer_pin, OUTPUT);
67         buzz(200, 1);
68
69         lcd.clear();
70 }
71
72
73 void loop()
74 {
75         time = millis();
76         measurement_t m = mhz19.getMeasurement();
77         co2 = m.co2_ppm;
78         temp = m.temperature;
79
80         Serial.print(time);
81         Serial.print(",");
82         Serial.print(co2);
83         Serial.print(",");
84         Serial.println(temp);
85
86         digitalWrite(led_red_pin, co2 >= co2_alarm_thr);
87         digitalWrite(led_yellow_pin, co2 >= co2_warning_thr && co2 < co2_alarm_thr);
88         digitalWrite(led_green_pin, co2 < co2_warning_thr);
89
90         if (co2 >= co2_alarm_thr && lastppm < co2_alarm_thr) {
91                 buzz(100, 3);
92         }
93
94         if (co2 >= co2_warning_thr && lastppm < co2_warning_thr) {
95                 buzz(500, 1);
96         }
97
98         lcd.setCursor(0, 0);
99         lcd.print("CO2: ");
100         lcd.print(co2);
101         if (time < warmuptime && (co2 == -1 || co2 == 410)) {
102                 lcd.print (" (W)");
103         }
104         lcd.print(" ppm    ");
105         lcd.setCursor(0, 1);
106         lcd.print("Temp: ");
107         lcd.print(temp);
108         lcd.print(" C +/-  ");
109
110         lastppm = co2;
111         delay(5000);
112 }