#include <Arduino.h>

// Define pin numbers for ESP32
const int floatSwitch1Pin = 25;
const int floatSwitch2Pin = 26;
const int floatSwitch3Pin = 33;
const int floatSwitch4Pin = 32;
const int manualOverridePin = 5;
const int relay1Pin = 7;
const int relay2Pin = 8;
const int indicator1Pin = 22;
const int indicator2Pin = 23;
const int alarmPin = 11;
const int screenPin = 12;

// Other constants
const int pumpRuntime = 5000;  // Adjust based on your requirements (in milliseconds)

// Variables
bool manualOverride = false;
int activePump = 1;

void setup() {
  Serial.begin(9600); // Initialize serial communication
  Serial.println("ESP32 Pump Control System");

  // Set up input and output pins
  pinMode(floatSwitch1Pin, INPUT);
  pinMode(floatSwitch2Pin, INPUT);
  pinMode(floatSwitch3Pin, INPUT);
  pinMode(floatSwitch4Pin, INPUT);
  pinMode(manualOverridePin, INPUT);
  pinMode(relay1Pin, OUTPUT);
  pinMode(relay2Pin, OUTPUT);
  pinMode(indicator1Pin, OUTPUT);
  pinMode(indicator2Pin, OUTPUT);
  pinMode(alarmPin, OUTPUT);
  pinMode(screenPin, OUTPUT);
}

void loop() {
  // Read the state of the manual override switch
  manualOverride = digitalRead(manualOverridePin);

  // If manual override is not activated, proceed with automatic control
  if (!manualOverride) {
    if (digitalRead(floatSwitch1Pin) == HIGH && digitalRead(floatSwitch2Pin) == HIGH) {
      // Float1 is up and Float2 is up
      if (activePump == 1) {
        TurnOnPump1();
      } else {
        TurnOnPump2();
      }
    } else if (digitalRead(floatSwitch1Pin) == LOW) {
      // Float1 is down
      TurnOffPumps();
    } else if (digitalRead(floatSwitch2Pin) == LOW) {
      //Float2 is down, Switch to next pump
      SwitchActivePump();
    } else if (digitalRead(floatSwitch3Pin) == HIGH) {
      // Float3 is up, indicating too much water, turn on both pumps
      TurnOnBothPumps();
    } else if (digitalRead(floatSwitch4Pin) == HIGH) {
      // Float4 is up, indicating dangerous water levels, trigger alarm
      TriggerAlarm();
      // You may add additional actions as needed when dangerous levels are detected
    } else {
      // No specific conditions met, turn off pumps and reset the alarm
      TurnOffPumps();
      ResetAlarm();
    }
  } else {
    // Manual override is activated, allow manual control
    ManualControl();
  }

  // Add additional logic for displaying information on the screen if needed

  delay(1500); // Add a short delay to avoid excessive loop execution
}

// Function to turn on Pump 1 and update indicators
void TurnOnPump1() {
  digitalWrite(relay1Pin, HIGH);
  digitalWrite(relay2Pin, LOW);
  digitalWrite(indicator1Pin, HIGH);
  digitalWrite(indicator2Pin, LOW);
  
  Serial.println("Pump 1 turned on");
}

// Function to turn on Pump 2 and update indicators
void TurnOnPump2() {
  digitalWrite(relay1Pin, LOW);
  digitalWrite(relay2Pin, HIGH);
  digitalWrite(indicator1Pin, LOW);
  digitalWrite(indicator2Pin, HIGH);
  
  Serial.println("Pump 2 turned on");
}

// Function to turn on both pumps and update indicators
void TurnOnBothPumps() {
  digitalWrite(relay1Pin, HIGH);
  digitalWrite(relay2Pin, HIGH);
  digitalWrite(indicator1Pin, HIGH);
  digitalWrite(indicator2Pin, HIGH);

  Serial.println("Both pumps turned on");
}

// Function to wait for the predetermined pump runtime
void WaitPumpRuntime() {
  delay(pumpRuntime);
}

// Function to turn off both pumps and update indicators
void TurnOffPumps() {
  digitalWrite(relay1Pin, LOW);
  digitalWrite(relay2Pin, LOW);
  digitalWrite(indicator1Pin, LOW);
  digitalWrite(indicator2Pin, LOW);

  Serial.println("Both pumps turned off");
}

// Function to switch the active pump
void SwitchActivePump() {
  activePump = (activePump == 1) ? 2 : 1;

  Serial.print("Switched to Pump ");
  Serial.println(activePump);
}

// Function to handle manual control
void ManualControl() {
  // Read additional inputs or user commands for manual control
  // Update pump and indicator states accordingly
  // Display relevant information on the screen
  // Allow the user to manually control the pumps

  Serial.println("Manual control is active");
}

// Function to trigger an alarm
void TriggerAlarm() {
  digitalWrite(alarmPin, HIGH);

  Serial.println("Alarm triggered");
}

// Function to reset the alarm
void ResetAlarm() {
  digitalWrite(alarmPin, LOW);

  Serial.println("Alarm reset");
}
Loading
esp32-devkit-c-v4
sw1:1
sw1:2
sw1:3
sw2:1
sw2:2
sw2:3
sw3:1
sw3:2
sw3:3
sw4:1
sw4:2
sw4:3
led1:A
led1:C
led2:A
led2:C
sw5:1
sw5:2
sw5:3