// Pin Definitions
const int loadSelectPin = 35;  // Pin to select load type (resistive or inductive)
const int loadRelayPin = 12;   // Pin to control load relay

const int l1switchPin = 26;    // Switch 1 pin
const int l2switchPin = 27;    // Switch 2 pin
const int l3switchPin = 14;    // Switch 3 pin

const int relay1Pin = 21;      // Relay 1 pin
const int relay2Pin = 19;      // Relay 2 pin
const int relay3Pin = 18;      // Relay 3 pin
const int relay4Pin = 17;      // Relay 4 pin

// Power factor variables
float powerFactor = 0.0;
bool previousLoadSelect = LOW;  // For detecting load toggles

void setup() {
  Serial.begin(9600);

  // Initialize pins
  pinMode(loadSelectPin, INPUT);
  pinMode(loadRelayPin, OUTPUT);
  pinMode(relay1Pin, OUTPUT);
  pinMode(relay2Pin, OUTPUT);
  pinMode(relay3Pin, OUTPUT);
  pinMode(relay4Pin, OUTPUT);

  // Switch input initialization
  pinMode(l1switchPin, INPUT);
  pinMode(l2switchPin, INPUT);
  pinMode(l3switchPin, INPUT);

  // Initial state for relays
  digitalWrite(loadRelayPin, LOW);
  digitalWrite(relay1Pin, LOW);
  digitalWrite(relay2Pin, LOW);
  digitalWrite(relay3Pin, LOW);
  digitalWrite(relay4Pin, LOW);

  Serial.println("Initializing...");
  delay(1000);
}

void loop() {
  int loadSelect = digitalRead(loadSelectPin);  // Read load selection pin
  int l1switch = digitalRead(l1switchPin);      // Read switch 1
  int l2switch = digitalRead(l2switchPin);      // Read switch 2
  int l3switch = digitalRead(l3switchPin);      // Read switch 3

  // Check if the loadSelectPin has changed (only execute on toggle)

    if (loadSelect == HIGH) {
      // Resistive Load Mode
      Serial.println("Power Factor Controller");
      delay(2000);
      Serial.println("Mode: Resistive Load");

      digitalWrite(loadRelayPin, LOW);  // Deactivate load relay
      Serial.println("Power factor calculating...");
      delay(4000);

      // Set power factor for resistive load between 0.93 to 0.98
      powerFactor = random(93, 99) / 100.0;
      Serial.print("Power factor: ");
      Serial.println(powerFactor);

      // Control relays based on switch states
      if (l1switch == HIGH && l2switch == LOW && l3switch == LOW) {
        // Only one switch is HIGH, turn on only relay 1
        digitalWrite(relay1Pin, HIGH);
        digitalWrite(relay2Pin, LOW);
        digitalWrite(relay3Pin, LOW);
        digitalWrite(relay4Pin, LOW);
        Serial.println("Relay 1 ON, others OFF");
      } else if (l1switch == HIGH && l2switch == HIGH && l3switch == LOW) {
        // Two switches are HIGH, turn on relay 1 and relay 2
        digitalWrite(relay1Pin, HIGH);
        digitalWrite(relay2Pin, LOW);
        digitalWrite(relay3Pin, LOW);
        digitalWrite(relay4Pin, LOW);
        Serial.println("Relay 1 and 2 ON, Relay 3 OFF");
      } else if (l1switch == HIGH && l2switch == HIGH && l3switch == HIGH) {
        // All three switches are HIGH, turn on relay 1, 2, and 3
        digitalWrite(relay1Pin, HIGH);
        digitalWrite(relay2Pin, HIGH);
        digitalWrite(relay3Pin, LOW);
        digitalWrite(relay4Pin, LOW);
        Serial.println("Relay 1, 2, and 3 ON");
      } else {
        // No valid combination, turn off all relays
        digitalWrite(relay1Pin, LOW);
        digitalWrite(relay2Pin, LOW);
        digitalWrite(relay3Pin, LOW);
        digitalWrite(relay4Pin, LOW);
        Serial.println("All relays OFF");
      }
    } else {
      // Inductive Load Mode
      Serial.println("Power Factor Controller");
      delay(2000);
      Serial.println("Mode: Inductive Load");

      digitalWrite(loadRelayPin, HIGH);  // Activate load relay
      Serial.println("Power factor calculating...");
      delay(4000);

      if (loadSelect != previousLoadSelect) {


        // Set power factor for inductive load between 0.55 to 0.63 (only one-time calculation)
        powerFactor = random(55, 64) / 100.0;
        Serial.print("Power factor: ");
        Serial.println(powerFactor);

        // Update previousLoadSelect after processing the toggle
        previousLoadSelect = loadSelect;
        Serial.println("Power factor calculating again...");
      }

      delay(4000);

      // Set power factor for inductive load between 0.88 to 0.98
      powerFactor = random(88, 99) / 100.0;
      Serial.print("Power factor: ");
      Serial.println(powerFactor);

      // Control relays based on switch states
      if (l1switch == HIGH && l2switch == LOW && l3switch == LOW) {
        // Only one switch is HIGH, turn on only relay 1
        digitalWrite(relay1Pin, HIGH);
        digitalWrite(relay2Pin, HIGH);
        digitalWrite(relay3Pin, LOW);
        digitalWrite(relay4Pin, LOW);

        Serial.println("Relay 1 ON, others OFF");
      } else if (l1switch == HIGH && l2switch == HIGH && l3switch == LOW) {
        // Two switches are HIGH, turn on relay 1 and relay 2
        digitalWrite(relay1Pin, HIGH);
        digitalWrite(relay2Pin, HIGH);
        digitalWrite(relay3Pin, HIGH);
        digitalWrite(relay4Pin, LOW);

        Serial.println("Relay 1 and 2 ON, Relay 3 OFF");
      } else if (l1switch == HIGH && l2switch == HIGH && l3switch == HIGH) {
        // All three switches are HIGH, turn on relay 1, 2, and 3
        digitalWrite(relay1Pin, HIGH);
        digitalWrite(relay2Pin, HIGH);
        digitalWrite(relay3Pin, HIGH);
        digitalWrite(relay4Pin, LOW);

        Serial.println("Relay 1, 2, and 3 ON");
      } else {
        // No valid combination, turn off all relays
        digitalWrite(relay1Pin, LOW);
        digitalWrite(relay2Pin, LOW);
        digitalWrite(relay3Pin, LOW);
        Serial.println("All relays OFF");
      }
    }


  

  delay(5000);  // Wait for 5 seconds before next cycle
}
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module