factor out buzzer calls into a function
[toast/airingbutler.git] / airingbutler.ino
index 68cd95d7338cc37ccde355ade7f056607728242d..075db34f29970b1bca75080305623b21f4dd0c32 100644 (file)
@@ -22,6 +22,14 @@ 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()
 {
@@ -32,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);
@@ -52,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();
 }
@@ -78,29 +86,21 @@ void loop()
        digitalWrite(led_green_pin, co2 < co2_warning_thr);
 
        if (co2 >= 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);
+               buzz(100, 3);
        }
 
        if (co2 >= co2_warning_thr && lastppm < co2_warning_thr) {
-               digitalWrite(buzzer_pin, HIGH);
-               delay(500);
-               digitalWrite(buzzer_pin, LOW);
+               buzz(500, 1);
        }
 
        lcd.setCursor(0, 0);
        lcd.print("CO2: ");
        lcd.print(co2);
-       lcd.print(" ppm         ");
+       lcd.print(" ppm    ");
+       lcd.setCursor(0, 1);
+       lcd.print("Temp: ");
+       lcd.print(temp);
+       lcd.print(" C +/-  ");
 
        lastppm = co2;
        delay(5000);