// ================= Configuration Variables =================
const int buzzerPin = 27;
const int pumpRelayPin = 16;
const unsigned long pumpOnTimeMs = 20000; // 20 seconds
const unsigned long beepIntervalMs = 300; // beep delay
// ================= Internal State Variables =================
bool pumpActive = false;
unsigned long pumpStartTime = 0;
unsigned long lastBeepTime = 0;
bool buzzerState = false;
// ================= Function Prototypes =================
void handlePumpCycle();
void handleBuzzerBeep();
/*
* Function: handlePumpCycle
* I/O: Controls pump relay (output)
* Working: Turns ON pump for fixed duration, then turns OFF
*/
void handlePumpCycle() {
// Start pump if not active
if (!pumpActive) {
digitalWrite(pumpRelayPin, HIGH);
pumpActive = true;
pumpStartTime = millis();
}
// Turn OFF after 20 seconds
if (pumpActive && (millis() - pumpStartTime >= pumpOnTimeMs)) {
digitalWrite(pumpRelayPin, LOW);
pumpActive = false;
}
}
/*
* Function: handleBuzzerBeep
* I/O: Controls buzzer (output)
* Working: Creates beep-beep sound while pump is ON
*/
void handleBuzzerBeep() {
// Error handling: if pump is OFF, ensure buzzer OFF
if (!pumpActive) {
digitalWrite(buzzerPin, LOW);
buzzerState = false;
return;
}
// Toggle buzzer for beep effect
if (millis() - lastBeepTime >= beepIntervalMs) {
lastBeepTime = millis();
buzzerState = !buzzerState;
digitalWrite(buzzerPin, buzzerState ? HIGH : LOW);
}
}
void setup() {
pinMode(buzzerPin, OUTPUT);
pinMode(pumpRelayPin, OUTPUT);
// Ensure everything OFF at start
digitalWrite(pumpRelayPin, LOW);
digitalWrite(buzzerPin, LOW);
}
void loop() {
handlePumpCycle();
handleBuzzerBeep();
}