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