#include <RunningStats.h>
#include <Filters.h> //Easy library to do the calculations
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
float testFrequency = 50; // test signal frequency (Hz)
int Sensor = 0; //Sensor analog input, here it's A0
int relay = 9; //Define output pin for relay
float intercept = 0.7; // To be adjusted based on calibration testing
float slope = 0.04; // To be adjusted based on calibration testing
float current_Volts; // Voltage
unsigned long printPeriod = 1000; //Refresh rate
unsigned long previousMillis = 0;
void setup()
{
lcd.init();
lcd.backlight();
pinMode(relay, OUTPUT);
lcd.print("Voltage:");
delay(1000);
}
void loop()
{
RunningStats stats; // Easy life lines, actual calculation of the RMS requires a load of coding
while ( true )
{
Sensor = analogRead(A0); // Read the analog in value:
stats.update(Sensor); // Log to stats function
if ((unsigned long)(millis() - previousMillis) >= printPeriod)
{
previousMillis = millis(); // Update time every second
current_Volts = intercept + slope * stats.std(); // Calibartions for offset and amplitude
current_Volts = current_Volts * (40.3231); // Further calibrations for the amplitude
lcd.setCursor(9, 0);
lcd.print(current_Volts);
lcd.print("V");
}
// Case 1 Under Voltage
if ( (current_Volts > 0) && (current_Volts < 150) )
{
lcd.setCursor(0, 1);
lcd.print("Under Voltage");
digitalWrite(relay, LOW);
}
// Case 2 Normal Rated Voltage
if ( (current_Volts >= 150) && (current_Volts <= 260) )
{
lcd.setCursor(0, 1);
lcd.print("Normal Voltage");
digitalWrite(relay, HIGH);
}
// Case 3 Over Voltage
if ( current_Volts > 260 )
{
lcd.setCursor(0, 1);
lcd.print("Over Voltage");
digitalWrite(relay, LOW);
}
}
}
{"html5":"htmlmixed","css":"css","javascript":"javascript","php":"php","python":"python","ruby":"ruby","lua":"text\/x-lua","bash":"text\/x-sh","go":"go","c":"text\/x-csrc","cpp":"text\/x-c++src","diff":"diff","latex":"stex","sql":"sql","xml":"xml","apl":"apl","asterisk":"asterisk","c_loadrunner":"text\/x-csrc","c_mac":"text\/x-csrc","coffeescript":"text\/x-coffeescript","csharp":"text\/x-csharp","d":"d","ecmascript":"javascript","erlang":"erlang","groovy":"text\/x-groovy","haskell":"text\/x-haskell","haxe":"text\/x-haxe","html4strict":"htmlmixed","java":"text\/x-java","java5":"text\/x-java","jquery":"javascript","mirc":"mirc","mysql":"sql","ocaml":"text\/x-ocaml","pascal":"text\/x-pascal","perl":"perl","perl6":"perl","plsql":"sql","properties":"text\/x-properties","q":"text\/x-q","scala":"scala","scheme":"text\/x-scheme","tcl":"text\/x-tcl","vb":"text\/x-vb","verilog":"text\/x-verilog","yaml":"text\/x-yaml","z80":"text\/x-z80"}