here is the complete code.
// EmonLibrary examples openenergymonitor.org, Licence GNU GPL V3
#include "EmonLib.h" // Include Emon Library
EnergyMonitor emon1; // Create an instance
int i = 0;
float tenvals = 0.0;
float minval = 1000;
float maxval = 0.0;
int ledPin = 13; // select the pin for the LED
float minAC = 200.00;
float maxAC = 250.00;
float LowAC = 198.00;
float AC_Volts =0.0;
// Relays Defining//
const int KE_NO = 5; // white
const int KE_NC = 6; // Red
const int Gen_NO = 3; //Orange
const int Gen_NC = 4; //yellow
const int KE_Relay = 9; //Green
const int Gen_Relay = 10; //Orange
const int Gen_OFF = 11; // Yellow
int Var_Gen1 = 0;
int Var_KE1 = 0;
int Var_Gen2 = 0;
int Var_KE2 = 0;
// Time Defining//
const long oneSecond = 500;
const long One_Min = oneSecond * 60;
const long Half_Min = oneSecond * 30;
void setup()
{
Serial.begin(115200);
emon1.voltage(A2, 176, 1.7); // Voltage: input pin, calibration, phase_shift
// emon1.current(1, 111.1); // Current: input pin, calibration.
pinMode(ledPin, OUTPUT);
pinMode(KE_NO, INPUT);
pinMode(Gen_NO, INPUT);
pinMode(KE_NC, INPUT);
pinMode(Gen_NC, INPUT);
pinMode(KE_Relay, OUTPUT);
pinMode(Gen_Relay, OUTPUT);
pinMode(Gen_OFF, OUTPUT);
}
void loop()
{
emon1.calcVI(20,2000); // Calculate all. No.of half wavelengths (crossings), time-out
// emon1.serialprint(); // Print out all variables (realpower, apparent power, Vrms, Irms, power factor)
// float realPower = emon1.realPower; //extract Real Power into variable
// float apparentPower = emon1.apparentPower; //extract Apparent Power into variable
// float powerFActor = emon1.powerFactor; //extract Power Factor into Variable
// float supplyVoltage = emon1.Vrms; //extract Vrms into Variable
// float Irms = emon1.Irms; //extract Irms into Variable
float Vrms = (emon1.Vrms - 3);
if (Vrms < 0) { Vrms = 0.0; }
tenvals += Vrms;
if (minval > Vrms) { minval = Vrms; }
if (maxval < Vrms) { maxval = Vrms; }
AC_Volts = tenvals/10 ;
i++;
if (i == 10)
{
Serial.print("Avg: ");
Serial.print(tenvals/10);
Serial.print(" (");
Serial.print(Vrms);
Serial.print(") Min: ");
Serial.print(minval);
Serial.print(" Line_AC: ");
Serial.println(AC_Volts);
Serial.print(" Max: ");
Serial.println(maxval);
i = 0;
tenvals = 0.0;
minval = 1000.0;
maxval = 0.0;
}
delay(100);
//int val; // The value coming from the sensor
// float Avgfloat = tenvals/10;
// The mapped value
// val = analogRead(Analog_IN); // read the voltage on the pot (val ranges from 0 to 1023)
// AC_Volts = map(val, 0, 1023, 0, 250); // percent will range from 0 to 100.
int Var_Gen1 = digitalRead(Gen_NO); // Gen Contactor is Energized
int Var_KE1 = digitalRead(KE_NO);
// ---------------- GEN POWER WITH LOW GRID VOLTAGE ---------//
if (LowAC >= AC_Volts and Var_Gen1 == HIGH and Var_KE1 == LOW)
{
digitalWrite (Gen_Relay, LOW); // Gen Contactor De-Energized 10
digitalWrite(ledPin, HIGH); // turn the ledPin on
delay(200);
digitalWrite(ledPin, LOW); // turn the ledPin on
delay(200);
}
// ----------------- GEN POWER WITH NORMAL GRID POWER----------//
else if (AC_Volts >= minAC and AC_Volts <= maxAC and Var_Gen1 == HIGH and Var_KE1 == LOW)
{
delay(One_Min);
digitalWrite (Gen_Relay, HIGH); // Gen Contactor De-Energized 10
delay(2000);
}
//---------------------- GEN CONTROL END----------------------//
int Var_Gen2 = digitalRead(Gen_NO); // Confirms Gen Contactor is De-Energized
int Var_KE2 = digitalRead(KE_NO);
//----------------- GRID POWER CONTROL WITH NORMAL GRID VOLTAGE------------------//
if (AC_Volts >= minAC and AC_Volts <= maxAC and Var_KE2 == LOW and Var_Gen2 == LOW )
{
delay(Half_Min);
digitalWrite(KE_Relay, HIGH); // Turn ON the KE Contactor 9
// delay(2000);
}
if (LowAC >= AC_Volts and Var_KE2 == HIGH )
{
digitalWrite (KE_Relay, LOW); // if Volatge are lower then the specified Turn OFF KE
digitalWrite(ledPin, HIGH); // turn the ledPin on
delay(200);
digitalWrite(ledPin, LOW); // turn the ledPin on
delay(200);
}
//--------------------GRID POWER CONTROL END--------------//
}