// Define pins for sensor, relay, and button
#define previousButtonPin 3 // Previous relay button
#define buzzerPin 12 // Buzzer pin
#define relaysCount 4 // Total number of relays
int relayPins[relaysCount] = {11, 10, 9, 8}; // Relay pins
#define bottomSensorPower 12 // Power pin for the bottom sensor
#define bottomSensorPin A0 // Analog pin for the bottom sensor
#define topSensorPower 13 // Power pin for the top sensor
#define topSensorPin A1 // Analog pin for the top sensor
#define nextButtonPin 2 // Next relay button
// Variables for relay control
int currentRelay = 0; // Index of the current relay
unsigned long relayTimer = 0; // Timer to track relay duration
unsigned long relayDuration = 10000; // 10 seconds in milliseconds
// Variables for water sensor levels
int bottomSensorValue = 0;
int topSensorValue = 0;
// Flags for relay operation
bool isTwoRelaysActive = false; // Tracks if two relays should be open
bool isFirstRun = true; // Tracks the first run for immediate activation
void setup() {
// Initialize sensor pins
pinMode(bottomSensorPower, OUTPUT);
pinMode(topSensorPower, OUTPUT);
digitalWrite(bottomSensorPower, LOW);
digitalWrite(topSensorPower, LOW);
// Initialize relay pins
for (int i = 0; i < relaysCount; i++) {
pinMode(relayPins[i], OUTPUT);
digitalWrite(relayPins[i], LOW); // Ensure all relays are OFF
}
// Initialize buzzer pins
pinMode(buzzerPin, OUTPUT);
digitalWrite(buzzerPin, LOW);
pinMode(nextButtonPin, INPUT_PULLUP);
pinMode(previousButtonPin, INPUT_PULLUP);
// Start serial communication
Serial.begin(9600);
}
void loop() {
// Read sensor values
bottomSensorValue = 0;
// topSensorValue = readSensor(topSensorPower, topSensorPin);
// Print sensor readings
// Serial.print("Bottom Sensor: ");
// Serial.println(bottomSensorValue);
// Serial.print("Top Sensor: ");
// Serial.println(topSensorValue);
// Check water level and control relays
if (bottomSensorValue == 0) {
// Below bottom sensor
soundAlarm();
isTwoRelaysActive = false; // Activate only one relay in this case
operateRelays(1);
}
else if (topSensorValue > 500) {
// Above top sensor
isTwoRelaysActive = true; // Activate two relays when water level is high
operateRelays(2);
}
else {
// Between bottom and top sensors
isTwoRelaysActive = false; // Activate only one relay
operateRelays(1);
}
// Handle button presses
handleButtons();
}
// Function to read sensor values
int readSensor(int sensorPower, int sensorPin) {
digitalWrite(sensorPower, HIGH); // Turn the sensor ON
delay(10); // Wait for stable reading
int value = analogRead(sensorPin);// Read the analog value
digitalWrite(sensorPower, LOW); // Turn the sensor OFF
return value; // Return the sensor reading
}
// Function to sound alarm
void soundAlarm() {
tone(buzzerPin, 1000); // Sound buzzer at 1000 Hz
delay(500);
noTone(buzzerPin); // Turn buzzer off
delay(500);
}
// Function to operate relays
void operateRelays(int numberOfRelays) {
// Check if it's the first run or if enough time has passed to activate relays
if (isFirstRun || millis() - relayTimer >= relayDuration) {
// Turn off all relays first
for (int i = 0; i < relaysCount; i++) {
digitalWrite(relayPins[i], LOW);
}
relayDuration = 10000;
// Open the required number of relays
for (int i = 0; i < numberOfRelays; i++) {
int relayIndex = (currentRelay + i) % relaysCount;
digitalWrite(relayPins[relayIndex], HIGH);
Serial.println(relayIndex);
}
// Update timer and relay index
relayTimer = millis(); // Set relay timer to current time
currentRelay = (currentRelay + numberOfRelays) % relaysCount;
// Disable first-run flag after the initial activation
isFirstRun = false;
}
}
void handleButtons() {
// Check for "Next" button press
if (digitalRead(nextButtonPin) == LOW) {
delay(200); // Debounce delay
Serial.println("nextbutton");
// Check if millis() is less than relayDuration
if (millis() - relayTimer < relayDuration) {
relayDuration = 0; // Set relayDuration to 0
}
// Determine how many relays to activate
int numberOfActiveRelays = isTwoRelaysActive ? 2 : 1;
// Reset the relay timer to immediately switch the relay
relayTimer = 0; // Reset timer
operateRelays(numberOfActiveRelays); // Operate the next relays
}
// Check for "Previous" button press
if (digitalRead(previousButtonPin) == LOW) {
delay(200); // Debounce delay
Serial.println("previousbutton");
// Determine how many relays to activate
int numberOfActiveRelays = isTwoRelaysActive ? 2 : 1;
// Calculate the new currentRelay index for "Previous" operation
if (numberOfActiveRelays == 1)
{
// Subtract 2 if a single relay is active
currentRelay = (currentRelay - 2 + relaysCount) % relaysCount;
} else {
// Subtract 4 if two relays are active
currentRelay = (currentRelay - 4 + relaysCount) % relaysCount;
}
// Reset the timer to immediately move to the previous relay
relayTimer = 0; // Reset timer
operateRelays(numberOfActiveRelays); // Operate the previous relays
}
}