#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 4); // Adjust columns and rows as needed
const int phase1Pin = A0;
const int phase2Pin = A1;
const int phase3Pin = A2;
const int solarPowerPin = A3;
const int relay1Pin = 8;
const int relay2Pin = 10;
const int relay3Pin = 4;
const int solarRelayPin = 7;
const int generatorSwitchRelayPin = 6; // Added generator switch relay pin
const int generatorRelayPin = 5; // Added generator relay pin
void setup() {
lcd.begin(16, 4); // Adjust rows as needed
lcd.print("God is my Light");
delay(2000);
// Configure relay pins as outputs
pinMode(relay1Pin, OUTPUT);
pinMode(relay2Pin, OUTPUT);
pinMode(relay3Pin, OUTPUT);
pinMode(solarRelayPin, OUTPUT);
pinMode(generatorSwitchRelayPin, OUTPUT); // Added generator switch relay pin
pinMode(generatorRelayPin, OUTPUT); // Added generator relay pin
lcd.clear();
}
void loop() {
int phase1Value = analogRead(phase1Pin);
int phase2Value = analogRead(phase2Pin);
int phase3Value = analogRead(phase3Pin);
int solarPowerValue = analogRead(solarPowerPin);
// Adjust these thresholds based on your sensor characteristics
const int threshold = 990;
// Determine which phase sensor is on (if any)
int activePhase = 0;
if (phase1Value > threshold)
activePhase = 1;
if (phase2Value > threshold)
activePhase = 2;
if (phase3Value > threshold)
activePhase = 3;
// Relay control based on phase status hierarchy
switch (activePhase) {
case 1:
digitalWrite(relay1Pin, HIGH);
digitalWrite(relay2Pin, LOW);
digitalWrite(relay3Pin, LOW);
break;
case 2:
digitalWrite(relay1Pin, LOW);
digitalWrite(relay2Pin, HIGH);
digitalWrite(relay3Pin, LOW);
break;
case 3:
digitalWrite(relay1Pin, LOW);
digitalWrite(relay2Pin, LOW);
digitalWrite(relay3Pin, HIGH);
break;
default:
digitalWrite(relay1Pin, LOW);
digitalWrite(relay2Pin, LOW);
digitalWrite(relay3Pin, LOW);
}
// Solar relay control
digitalWrite(solarRelayPin, (activePhase == 0 && solarPowerValue > threshold) ? HIGH : LOW);
// Generator switch relay control
digitalWrite(generatorSwitchRelayPin, (activePhase == 0 && solarPowerValue <= threshold) ? HIGH : LOW);
// Generator relay control
digitalWrite(generatorRelayPin, (activePhase == 0 && solarPowerValue <= threshold) ? HIGH : LOW);
// Display power status on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Power Status:");
// Determine the active power source
if (activePhase != 0) {
lcd.setCursor(0, 1);
lcd.print("Phase ");
lcd.print(activePhase);
} else if (solarPowerValue > threshold) {
lcd.setCursor(0, 1);
lcd.print("Solar Power");
} else {
lcd.setCursor(0, 1);
lcd.print("Gen On");
}
delay(700); // Adjust delay based on your requirements
}