// Pin arrays
const int callButton = 2;
const int ackButton = 3;
const int testButton = 4;
const int outputLed = 5;
const int outputBuzz = 6;

// Busy flag
boolean ledon = false; // led on
boolean buzzon = false; // buzzer on
boolean called = false; // call button pressed

void setup() {
  // Init pins mode
  pinMode(callButton, INPUT_PULLUP);
  pinMode(ackButton, INPUT_PULLUP);
  pinMode(testButton, INPUT_PULLUP);
  pinMode(outputLed, OUTPUT);
  // pinMode(outputBuzz, OUTPUT);
  Serial.begin(9600); // Initialize serial communication
  Serial.println("OGS Emergency Call Button v2.2 H.O.I. starting...");
}

void loop() {
  // call button pressed
  if (digitalRead(callButton) == LOW) {
    Serial.println("Call Button pressed");
    called = true;

    ledon = true;
    buzzon = true;
  }

  // test button pressed
  if (digitalRead(testButton) == LOW) {
    Serial.println("Test Button pressed");
    digitalWrite(outputLed, HIGH);
    // digitalWrite(outputBuzz, HIGH);
    tone(outputBuzz, 440, 200);
  } else {
    digitalWrite(outputLed, LOW);
    // digitalWrite(outputBuzz, LOW);
    noTone(outputBuzz);
  }

  // acknowlage button press to clear buzz only
  if (digitalRead(ackButton) == LOW) {
    Serial.println("Acknowlage Button pressed");
    // digitalWrite(outputBuzz, LOW);
    noTone(outputBuzz);
    buzzon = false;
  }

  // call button reset
  if ((called == true) && (digitalRead(callButton) == HIGH)) {
    Serial.println("Call Button reset to normal");
    called = false;
    digitalWrite(outputLed, LOW);
    ledon = false;
    //digitalWrite(outputBuzz, LOW);
    noTone(outputBuzz);
    buzzon = false;
  }  

  // keep Led or Buzz if call button pressed
  if (called == true) {
    if (ledon == true) {
      digitalWrite(outputLed, HIGH);
    }
    if (buzzon == true) {
    // digitalWrite(outputBuzz, HIGH);
    tone(outputBuzz, 440, 200);
    }

  }

}