Hi There,
I am running into an issue when I use Filters.h when running my script. When the script starts, all it runs is L1G(); , and doesn't go to the other functions L2G();,SolarBattWind();. Is there something I am missing when it comes to using the void loop? I have tried using other methods of cleaning the data, but I can't for the voltage sensors.
I am lost on this.
Thanks for any help.
#include "Wire.h"
#include "Filters.h"
const int sensorIn = 8;
const int sensorIn1 = 9;
int mVperAmp = 13.3;
int ACSoffset = 2500; // See offsets below
double Voltage = 0;
double VRMS = 0;
double AmpsRMS = 0;
double Voltage1 = 0;
double VRMS1 = 0;
double AmpsRMS1 = 0;
////////////////////////////////////////////////////////////////////////////////// AC Voltage Setup
float testFrequency = 60; // test signal frequency (Hz)
float windowLength = 22.0/testFrequency; // how long to average the signal, for statistist //How long before refreshing sample
int Sensor = 0; //L1 G
int Sensor1 = 0; //L2 G
float intercept = -0.04;
float slope = 0.0401;
float current_Volts; // Voltage
float current_Volts1; // Voltage
unsigned long printPeriod = 500; //Refresh rate
unsigned long previousMillis = 0;
/////////////////////////////////////////////////////////////////////////////////DC Current Setup
int RawValue2= 0;
double Voltage2 = 0;
double Amps2 = 0;
int RawValue3= 0;
double Voltage3 = 0;
double Amps3 = 0;
int readValue; //value read from the sensor
int maxValue = 0; // store max value here
int minValue = 1024; // store min value here
int readValue1; //value read from the sensor
int maxValue1 = 0; // store max value here
int minValue1 = 1024; // store min value here
//Scale Factors
//50A bi-directional = 40
//50A uni-directional = 60
//100A bi-directional = 20
//100A uni-directional = 40
//150A bi-directional = 13.3
//150A uni-directioal = 26.7
int RawValue4= 0;
double Voltage4 = 0;
double Amps4 = 0;
int RawValue5= 0;
double Voltage5 = 0;
double Amps5 = 0;
///////////////////////////////////////////////////////////////////////////// DC Voltage Setup
int analogInput = A0; //Change if not connected to pin A0
float vout = 0.0; //do not change
float vin = 0.0; //do not change
float R1 = 30000.0; //onboard resistor 1 value
float R2 = 7500.0; //onboard resistor 2 value
int svalue = 0; //do not change
//////////////////////////////////////////////////////////////////////////
void setup() {
Serial.begin( 115200 ); // start the serial port
}
void L1G() {
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Voltage
RunningStatistics inputStats;
inputStats.setWindowSecs( windowLength );
while( true ) {
Sensor = analogRead(Sensor); // read the analog in value:
inputStats.input(Sensor); // log to Stats function
if((unsigned long)(millis() - previousMillis) >= printPeriod) {
previousMillis = millis(); // update time every second
Serial.print( "\n" );
current_Volts = intercept + slope * inputStats.sigma(); //Calibartions for offset and amplitude
current_Volts= current_Volts*(69.95); //Further calibrations for the amplitude
Serial.print( "L1 Grid Voltage: " );
Serial.print( current_Volts );
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Voltage = getVPP();
VRMS = (Voltage/2.0) *0.707;
AmpsRMS = (VRMS * 1000)/mVperAmp;
Serial.print(AmpsRMS);
Serial.println("L1 G Current:");
Serial.print("L1 G Watts: ");
Serial.println(.95* AmpsRMS * current_Volts);
}
}
}
float getVPP() //////// L1 Current
{
float result;
int readValue; //value read from the sensor
int maxValue = 0; // store max value here
int minValue = 1024; // store min value here
uint32_t start_time = millis();
while((millis()-start_time) < 1000) //sample for 1 Sec
{
readValue = analogRead(8);
// see if you have a new maxValue
if (readValue > maxValue)
{
/*record the maximum sensor value*/
maxValue = readValue;
}
if (readValue < minValue)
{
/*record the maximum sensor value*/
minValue = readValue;
}
}
// Subtract min from max
result = ((maxValue - minValue) * 5.0)/1024.0;
return result;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void L2G() {
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Voltage
RunningStatistics inputStats1;
inputStats1.setWindowSecs( windowLength );
while( true ) {
Sensor1 = analogRead(Sensor1); // read the analog in value:
inputStats1.input(Sensor1); // log to Stats function
if((unsigned long)(millis() - previousMillis) >= printPeriod) {
previousMillis = millis(); // update time every second
Serial.print( "\n" );
current_Volts1 = intercept + slope * inputStats1.sigma(); //Calibartions for offset and amplitude
current_Volts1= current_Volts1*(69.95); //Further calibrations for the amplitude
Serial.print( "L2 G Voltage: " );
Serial.print( current_Volts1 );
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Voltage1 = getVPP1();
VRMS1 = (Voltage/2.0) *0.707;
AmpsRMS1 = (VRMS1 * 1000)/mVperAmp;
Serial.print(AmpsRMS1);
Serial.println("L2 G Current:");
Serial.print("L2 G Watts: ");
Serial.println(.95* AmpsRMS1 * current_Volts1);
}
}
}
float getVPP1() //////// L2 Current
{
float result1;
int readValue1; //value read from the sensor
int maxValue1 = 0; // store max value here
int minValue1 = 1024; // store min value here
uint32_t start_time = millis();
while((millis()-start_time) < 1000) //sample for 1 Sec
{
readValue = analogRead(9);
// see if you have a new maxValue
if (readValue1 > maxValue1)
{
/*record the maximum sensor value*/
maxValue1 = readValue1;
}
if (readValue1 < minValue1)
{
/*record the maximum sensor value*/
minValue1 = readValue1;
}
}
// Subtract min from max
result1 = ((maxValue1 - minValue1) * 5.0)/1024.0;
return result1;
}
void SolarBattWind(){
RawValue3 = analogRead(A4); //Solar
Voltage3 = (RawValue3 / 1023.0) * 5000; // Gets you mV
Amps3 = ((Voltage3 - ACSoffset) / mVperAmp);
RawValue4 = analogRead(A5); //Wind
Voltage4 = (RawValue4 / 1023.0) * 5000; // Gets you mV
Amps4 = ((Voltage4 - ACSoffset) / mVperAmp);
svalue = analogRead(analogInput); //this reads the value from the sensor //Battery Voltage
vout = (svalue * 5.0) / 1024.0;
vin = vout / (R2/(R1+R2));
Serial.print(vin,2); // prints the voltage
Serial.println("Battery Voltage: ");
Serial.print("Solar Current: "); // shows the voltage measured
Serial.println(Amps3,3);
Serial.print("Wind Current: "); // shows the voltage measured
Serial.println(Amps4,3);
Serial.print("Wind Watts: ");
Serial.println(24 * Amps4,3);
Serial.print("Solar Watts: ");
Serial.println(24 * Amps3,3);
}
void loop() {
L1G();
L2G();
SolarBattWind();
delay(250); //delay loop
}