#define buzzer 9
const uint8_t maxDetector = 2;
const uint8_t waterDetectors[] = {A0, A1}; // Define Analog Input Pins
const uint8_t alarmLamps[] = {2, 3}; // Define Alarm Lamp Pins
int detectorValues [maxDetector]; // Define Read Value Buffers for future use
bool warning = false;
bool alarm = false;
bool currWarning = false;
bool currAlarm = false;
String statusMessage = "";
unsigned long prevTime = millis();
unsigned long interval = 1000;
void setup() {
Serial.begin(9600);
pinMode(buzzer, OUTPUT);
for (uint8_t index = 0; index < maxDetector; index++) {
pinMode(alarmLamps[index], OUTPUT);
}
}
void loop() {
warning = false;
alarm = false;
statusMessage = "";
for (uint8_t index = 0; index < maxDetector; index++) {
detectorValues[index] = analogRead(waterDetectors[index]); // Read Water Level
if (detectorValues[index] >= 300) { // Water Alarm Level
currAlarm = true;
digitalWrite(alarmLamps[index], HIGH);
} else if (detectorValues[index] >= 250) { // Water Warning Level
currWarning = true;
} else {
currAlarm = false;
currWarning = false;
digitalWrite(alarmLamps[index], LOW);
}
warning |= currWarning; // Common Warning
alarm |= currAlarm; // Common Alarm
if (index > 0) {
statusMessage = statusMessage + ", " + + "Detector" + String(index + 1) + " Value:= " + String(detectorValues[index]);
} else {
statusMessage = "Detector1 Value:= " + String(detectorValues[index]);
}
}
if (alarm) { // Alarm
tone(buzzer, 5000);
}
else if (warning) { // Warning
tone(buzzer, 1000, 5);
}
else { // Normal
noTone(buzzer);
}
if (millis() - prevTime >= interval) {
prevTime = millis();
Serial.println(statusMessage);
}
}