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