factor out buzzer calls into a function
[toast/airingbutler.git] / airingbutler.ino
index ef2d87421d45800ffb29734f96feb494b40add13..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()
 {
@@ -38,11 +46,10 @@ void setup()
        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,23 +86,11 @@ 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);