Using multiple current sensors with supplied code for Arduino.

Thread Starter

kuera

Joined Aug 17, 2012
29
I'm using an ACS712 (20amp I think, doesn't matter at the moment) with a code I found. The code works as intended and I get it to run with a single sensor unit all fine and dandy. I am however trying to monitor a three-phase motor's current draw to make sure there isn't any problems with it in a long-term run.
Code:
#include <Filters.h>                      //This library does a massive work check it's .cpp file

#define ACS_Pin A2                        //Sensor data pin on A0 analog input

float ACS_Value;                              //Here we keep the raw data valuess
float testFrequency = 50;                    // test signal frequency (Hz)
float windowLength = 40.0/testFrequency;     // how long to average the signal, for statistist



float intercept = -0.1; // to be adjusted based on calibration testing
float slope = 0.0752; // to be adjusted based on calibration testing
                      //Please check the ACS712 Tutorial video by SurtrTech to see how to get them because it depends on your sensor, or look below


float Amps_TRMS; // estimated actual current in amps

unsigned long printPeriod = 1000; // in milliseconds
// Track time in milliseconds since last reading 
unsigned long previousMillis = 0;

void setup() {
  Serial.begin( 9600 );    // Start the serial port
  pinMode(ACS_Pin,INPUT);  //Define the pin mode
}

void loop() {
  RunningStatistics inputStats;                 // create statistics to look at the raw test signal
  inputStats.setWindowSecs( windowLength );     //Set the window length
 while( true ) {   
    ACS_Value = analogRead(ACS_Pin);  // read the analog in value:
    inputStats.input(ACS_Value);  // log to Stats function
        
    if((unsigned long)(millis() - previousMillis) >= printPeriod) { //every second we do the calculation
      previousMillis = millis();   // update time
      
      Amps_TRMS = intercept + slope * inputStats.sigma();

      Serial.print( "\t Amps: " ); 
      Serial.print( Amps_TRMS );
            Serial.print( "\t Sigma: " ); 
      Serial.println( inputStats.sigma());

    }
  }
 
}
I've tried to add other inputs by adding more running statistics and AO inputs and adding input statistics but I'm having no luck.
Also I'm rather new to the software side so any enlightenment would be highly appreciated.
So several questions pop into my head when I see this.
1.) Why is there a while(true) command in the void loop function. I thought while true was a void loop (or so I've seen in my limited understanding from Pi pico and arduino)
2.) Can this be used to actually measure multiple sensors and if so where would I mallet the code into it?
3.) Not a question but I have hooked up a cute nokia screen to the system for a display and that works.

Thanks in advance.
 

Ya’akov

Joined Jan 27, 2019
5,998
You need to investigate state machines. There are specific tutorials for using them with Arduino. If oyu search YouTube for “Arduino state machine” you will find several options.

This one has good content though I must admit I don’t like the style of the presenter. Still it is correctly presented.

 

djsfantasi

Joined Apr 11, 2010
8,390
Why is there a while(true) command in the void loop function.
What do YOU mean by a “void loop”? The statement defines an infinite loop, that repeatedly executes the code between the matching braces, starting with the open brace after the “while (true)”.

So, your sketch repeats the following code over and over…
Code:
    ACS_Value = analogRead(ACS_Pin);  // read the analog in value:
    inputStats.input(ACS_Value);  // log to Stats function
       
    if((unsigned long)(millis() - previousMillis) >= printPeriod) { //every second we do the calculation
      previousMillis = millis();   // update time
     
      Amps_TRMS = intercept + slope * inputStats.sigma();

      Serial.print( "\t Amps: " );
      Serial.print( Amps_TRMS );
            Serial.print( "\t Sigma: " );
      Serial.println( inputStats.sigma());
 

Thread Starter

kuera

Joined Aug 17, 2012
29
What do YOU mean by a “void loop”? The statement defines an infinite loop, that repeatedly executes the code between the matching braces, starting with the open brace after the “while (true)”.

So, your sketch repeats the following code over and over…
Code:
    ACS_Value = analogRead(ACS_Pin);  // read the analog in value:
    inputStats.input(ACS_Value);  // log to Stats function
      
    if((unsigned long)(millis() - previousMillis) >= printPeriod) { //every second we do the calculation
      previousMillis = millis();   // update time
    
      Amps_TRMS = intercept + slope * inputStats.sigma();

      Serial.print( "\t Amps: " );
      Serial.print( Amps_TRMS );
            Serial.print( "\t Sigma: " );
      Serial.println( inputStats.sigma());
If it's repeating what's in the "while (true)" over and over, wouldn't it be easier to just put it under the void loop function since they serve the same purpose? I just ask this since arduino seems to use void loop for repeating code and micropyhton does the same with while (true). This code seems to have a while true inside a void loop, which seems redundant. I'm just trying to figure out why it's done in this way.
 

djsfantasi

Joined Apr 11, 2010
8,390
You have a good point.

It usually comes down to issues of scope. There may be situations where you may want to initialize data structures that cannot be done in the setup function. Or one may not understand all methods of variable definition. Or someone may not understand such scope issues or the execution of the loop() function. Some of my early sketches uses this “while (true)” pattern because of my lack of understanding of this latter point at the time.
 

Thread Starter

kuera

Joined Aug 17, 2012
29
You have a good point.

It usually comes down to issues of scope. There may be situations where you may want to initialize data structures that cannot be done in the setup function. Or one may not understand all methods of variable definition. Or someone may not understand such scope issues or the execution of the loop() function. Some of my early sketches uses this “while (true)” pattern because of my lack of understanding of this latter point at the time.
I think it has something to do with the variable definition you speak of because if I remove the while statement and stick it only in the loop function.
Code:
void loop() {
  RunningStatistics inputStats;                 // create statistics to look at the raw test signal
  inputStats.setWindowSecs( windowLength );     //Set the window length
 while( true ) {   
    ACS_Value = analogRead(ACS_Pin);  // read the analog in value:
    inputStats.input(ACS_Value);  // log to Stats function
it complains about about the inputStats being undefined. I've played around with adding more inputStats and readings and such to the sketch and it compiles properly without screaming blue murder at me but I haven't had the chance to do a physical test. Life doing its thing and all.
 
Top