factor out buzzer calls into a function
[toast/airingbutler.git] / airingbutler.ino
index c93f4ba89dad7496c546c027c24957dd18119b7a..075db34f29970b1bca75080305623b21f4dd0c32 100644 (file)
@@ -18,8 +18,18 @@ MHZ19 mhz19 = MHZ19(rx_pin,tx_pin);
 LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
 
 unsigned long time;
+int co2 = 0;
+int temp = 0;
 int lastppm = 0;
 
+void buzz(unsigned long pause, int count) {
+       for (int i = 0; i <= count; i++) {
+               digitalWrite(buzzer_pin, HIGH);
+               delay(pause);
+               digitalWrite(buzzer_pin, LOW);
+               if (i < count - 1) delay(pause);
+       }
+}
 
 void setup()
 {
@@ -30,17 +40,16 @@ void setup()
        mhz19.begin(rx_pin, tx_pin);
 
        // LCD
-       lcd.begin(16,2);
-       lcd.setCursor(0,0);
+       lcd.begin(16, 2);
+       lcd.setCursor(0, 0);
        lcd.print("Airing Butler");
-       lcd.setCursor(0,1);
+       lcd.setCursor(0, 1);
        lcd.print("says hello!");
 
-       // LEDs + buzzer
+       // LEDs
        pinMode(led_green_pin, OUTPUT);
        pinMode(led_yellow_pin, OUTPUT);
        pinMode(led_red_pin, OUTPUT);
-       pinMode(buzzer_pin, OUTPUT);
        digitalWrite(led_green_pin, HIGH);
        delay(500);
        digitalWrite(led_green_pin, LOW);
@@ -50,9 +59,10 @@ void setup()
        digitalWrite(led_red_pin, HIGH);
        delay(500);
        digitalWrite(led_red_pin, LOW);
-       digitalWrite(buzzer_pin, HIGH);
-       delay(200);
-       digitalWrite(buzzer_pin, LOW);
+
+       // buzzer
+       pinMode(buzzer_pin, OUTPUT);
+       buzz(200, 1);
 
        lcd.clear();
 }
@@ -62,42 +72,36 @@ void loop()
 {
        time = millis();
        measurement_t m = mhz19.getMeasurement();
+       co2 = m.co2_ppm;
+       temp = m.temperature;
 
        Serial.print(time);
        Serial.print(",");
-       Serial.print(m.co2_ppm);
+       Serial.print(co2);
        Serial.print(",");
-       Serial.println(m.temperature);
+       Serial.println(temp);
 
-       digitalWrite(led_red_pin, m.co2_ppm >= co2_alarm_thr);
-       digitalWrite(led_yellow_pin, m.co2_ppm >= co2_warning_thr && m.co2_ppm < co2_alarm_thr);
-       digitalWrite(led_green_pin, m.co2_ppm < co2_warning_thr);
+       digitalWrite(led_red_pin, co2 >= co2_alarm_thr);
+       digitalWrite(led_yellow_pin, co2 >= co2_warning_thr && co2 < co2_alarm_thr);
+       digitalWrite(led_green_pin, co2 < co2_warning_thr);
 
-       if (m.co2_ppm >= co2_alarm_thr && lastppm < co2_alarm_thr) {
-               digitalWrite(buzzer_pin, HIGH);
-               delay(100);
-               digitalWrite(buzzer_pin, LOW);
-               delay(100);
-               digitalWrite(buzzer_pin, HIGH);
-               delay(100);
-               digitalWrite(buzzer_pin, LOW);
-               delay(100);
-               digitalWrite(buzzer_pin, HIGH);
-               delay(100);
-               digitalWrite(buzzer_pin, LOW);
+       if (co2 >= co2_alarm_thr && lastppm < co2_alarm_thr) {
+               buzz(100, 3);
        }
 
-       if (m.co2_ppm >= co2_warning_thr && lastppm < co2_warning_thr) {
-               digitalWrite(buzzer_pin, HIGH);
-               delay(500);
-               digitalWrite(buzzer_pin, LOW);
+       if (co2 >= co2_warning_thr && lastppm < co2_warning_thr) {
+               buzz(500, 1);
        }
 
        lcd.setCursor(0, 0);
        lcd.print("CO2: ");
-       lcd.print(m.co2_ppm);
-       lcd.print(" ppm         ");
-
-       lastppm = m.co2_ppm;
+       lcd.print(co2);
+       lcd.print(" ppm    ");
+       lcd.setCursor(0, 1);
+       lcd.print("Temp: ");
+       lcd.print(temp);
+       lcd.print(" C +/-  ");
+
+       lastppm = co2;
        delay(5000);
 }