Sketch only running on First Function in void Loop

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
}

It’s going to run this until true becomes false, which it won’t, so it sits in L1G for ever.

void L1G() {
  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Voltage 
  RunningStatistics inputStats;           
  inputStats.setWindowSecs( windowLength );
   
  while( true )  // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<,
  {

sayHovis:
It’s going to run this until true becomes false, which it won’t, so it sits in L1G for ever.

void L1G() {

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Voltage
 RunningStatistics inputStats;          
 inputStats.setWindowSecs( windowLength );
 
 while( true )  // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<,
 {

Hi there, thanks for your reply. I removed that statement and it did go through the rest of them, but the voltage wasn’t the correct value. One thing I did for my other script, is remove that statement in there, and all I get is the first Voltage again, but at the right value. but after 10 times of printing it, it goes down to 3 from 122.

Attached is the sketch I am running.

#include <Filters.h> //Easy library to do the calculations



float testFrequency = 60;                     // test signal frequency (Hz)
float windowLength = 40.0/testFrequency;     // how long to average the signal, for statistist

int Sensor = 0; //Sensor analog input, here it's A0
int Sensor1 = 1; //Sensor analog input, here it's A0

float intercept = -0.04; // to be adjusted based on calibration testing
float slope = 0.0405; // to be adjusted based on calibration testing
float current_Volts; // Voltage
float current_Volts1; // Voltage


unsigned long printPeriod = 1000; //Refresh rate
unsigned long previousMillis = 0;


void setup() {
  Serial.begin( 115200 );    // start the serial port


}

void One() {
  
  RunningStatistics inputStats;                //Easy life lines, actual calculation of the RMS requires a load of coding
  inputStats.setWindowSecs( windowLength );
   
 
    Sensor = analogRead(A0);  // 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*(40.3231);                //Further calibrations for the amplitude
      
      Serial.print( "\tVoltage: " );
      Serial.print( current_Volts ); //Calculation and Value display is done the rest is if you're using an OLED display
      

    }
  }



void Two() {
  
  RunningStatistics inputStats;                //Easy life lines, actual calculation of the RMS requires a load of coding
  inputStats.setWindowSecs( windowLength );
   
  
    Sensor1 = analogRead(A1);  // read the analog in value:
    inputStats.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 * inputStats.sigma(); //Calibartions for offset and amplitude
      current_Volts1= current_Volts1*(40.3231);                //Further calibrations for the amplitude
      
      Serial.print( "\tVoltage: 12" );
      Serial.print( current_Volts1 ); //Calculation and Value display is done the rest is if you're using an OLED display
      

    }
  }




void loop() {
  One();
  Two();
  
}

Just to note that only running 1 of the sketch, instead of 2, it works fine. I need to be able to get both of those to run in the same sketch that I posted just now.

Been looking around some more. Break statement is probably the best here?

How could I insert a break statement after it comes out with a result from “current_Volts”, that way it moves on to the next and loops?

Thanks

inventrex3601:
I need to be able to get both of those to run in the same sketch that I posted just now.

Both of what?

Your two functions are both using previousMillis to control whether they print any results. Each function needs its own version of previousMillis, or it'll be the luck of the draw as to which actually prints something.