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