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