Smoothing Irms

 // EmonLibrary examples openenergymonitor.org, Licence GNU GPL V3
#include <LiquidCrystal.h>
LiquidCrystal lcd(PA0, PA1, PA2, PA3, PA4, PA5);


#include "EmonLib.h"                   // Include Emon Library
EnergyMonitor emon1;                   // Create an instance
const int numReadings = 10;


int readings[numReadings];      // the readings from the analog input
int readIndex = 0;              // the index of the current reading
int total = 0;                  // the running total
int average = 0;                // the average
int Irms;
void setup()
{
  for (int thisReading = 0; thisReading < numReadings; thisReading++)
  {
    readings[thisReading] = 0;
  }
  lcd.begin(16, 2);
  emon1.current(PA7, 2.8);             // Current: input pin, calibration.
}


void loop()


{
  double Irms = emon1.calcIrms(1480);  // Calculate Irms only


  // subtract the last reading:
  total = total - readings[readIndex];
  // read from the sensor:


  // readings[readIndex] = analogRead(PA7);
  readings[readIndex] = Irms;


  // add the reading to the total:
  total = total + readings[readIndex];
  // advance to the next position in the array:
  readIndex = readIndex + 1;


  // if we're at the end of the array...
  if (readIndex >= numReadings) {
    // ...wrap around to the beginning:
    readIndex = 0;
  }
 average = total / numReadings;
 
  Serial.println(average);
  delay(1); 
        
  lcd.setCursor(0, 1);
  lcd.print("U=");
  lcd.print(Irms);


  lcd.setCursor(0, 0);
  lcd.print("A=");
  lcd.print(average);
}

Hi
The program above it is a AC meter, I need to smooth the readings to get stable numbers on LCD, to do that I need to average Irms , when I using traditional arduino smoothing the reading non LCD
lcd.print(average);is only integer - 0,1,2 ,3.
How to fix it?

int average = 0;

lcd.print(average);
is only integer - 0,1,2 ,3.

No surprise there.

Start by declaring average and total as floats

I changed to;

float total = 0;                  // the running total
float average = 0;                // the average

Results are the same.

Let's see your new code

// EmonLibrary examples openenergymonitor.org, Licence GNU GPL V3
#include <LiquidCrystal.h>
LiquidCrystal lcd(PA0, PA1, PA2, PA3, PA4, PA5);


#include "EmonLib.h"                   // Include Emon Library
EnergyMonitor emon1;                   // Create an instance
const int numReadings = 10;


int readings[numReadings];      // the readings from the analog input
int readIndex = 0;              // the index of the current reading
//int total = 0;                  // the running total
//int average = 0;                // the average
/////////////////////////////
float total = 0;                  // the running total
float average = 0;                // the average
/////////////////////////
int Irms;
void setup()
{
  for (int thisReading = 0; thisReading < numReadings; thisReading++)
  {
    readings[thisReading] = 0;
  }
  lcd.begin(16, 2);
  emon1.current(PA7, 2.8);             // Current: input pin, calibration.
}


void loop()


{
  double Irms = emon1.calcIrms(1480);  // Calculate Irms only


  // subtract the last reading:
  total = total - readings[readIndex];
  // read from the sensor:


  // readings[readIndex] = analogRead(PA7);
  readings[readIndex] = Irms;


  // add the reading to the total:
  total = total + readings[readIndex];
  // advance to the next position in the array:
  readIndex = readIndex + 1;


  // if we're at the end of the array...
  if (readIndex >= numReadings) {
    // ...wrap around to the beginning:
    readIndex = 0;
  }
 average = total / numReadings;
 
  Serial.println(average);
  delay(1); 
        
  lcd.setCursor(0, 1);
  lcd.print("U=");
  lcd.print(Irms);


  lcd.setCursor(0, 0);
  lcd.print("A=");
  lcd.print(average);
}
int readings[numReadings];      // the readings from the analog input

void loop()
{
  double Irms = emon1.calcIrms(1480);  // Calculate Irms only
  readings[readIndex] = Irms;

Your readings are all 'double' so your array to store them should probably be 'double', not 'int'.

// EmonLibrary examples openenergymonitor.org, Licence GNU GPL V3
#include <LiquidCrystal.h>
LiquidCrystal lcd(PA0, PA1, PA2, PA3, PA4, PA5);


#include "EmonLib.h"                   // Include Emon Library
EnergyMonitor emon1;                   // Create an instance
const int numReadings = 10;


int readings[numReadings];      // the readings from the analog input
int readIndex = 0;              // the index of the current reading




//////////////////////////////////////////////////////////////////////
//double readings[numReadings];      // not good - compiling error
//double readIndex = 0;              //not good - compiling error
//////////////////////////////////////////////////////////////////////


//////////////////
 double total ;
 double average = 0;                // the average
////////////////////
//float total ;                  // the running total
//float average ;                // the average
/////////////////
int Irms;
void setup()
{
  for (int thisReading = 0; thisReading < numReadings; thisReading++)
  {
    readings[thisReading] = 0;
  }
  lcd.begin(16, 2);
  emon1.current(PA7, 2.8);             // Current: input pin, calibration.
}


void loop()


{
  double Irms = emon1.calcIrms(1480);  // Calculate Irms only


  total = total - readings[readIndex];

 //////////////////////////////
  readings[readIndex] = Irms;
  //////////////////////////////

total = total + readings[readIndex];
 readIndex = readIndex + 1;
 if (readIndex >= numReadings) {
 
    readIndex = 0;
  }
 average = total / numReadings;
 
  Serial.println(average);
  delay(1); 
        
  lcd.setCursor(0, 1);
  lcd.print("U=");
  lcd.print(Irms);


  lcd.setCursor(0, 0);
  lcd.print("A=");
  lcd.print(average);
}

No changes in results

int readings[numReadings];      // the readings from the analog input
  double Irms = emon1.calcIrms(1480);  // Calculate Irms only
  total = total - readings[readIndex];
  //////////////////////////////
  readings[readIndex] = Irms;

readings is an array of ints but Irms is a double

This line is commented out

//double readings[numReadings];      // not good - compiling error

What error did you get ?

UKHeliBob:

int readings[numReadings];      // the readings from the analog input
  double Irms = emon1.calcIrms(1480);  // Calculate Irms only

total = total - readings[readIndex];
  //////////////////////////////
  readings[readIndex] = Irms;




readings is an array of ints but Irms is a double

This line is commented out


//double readings[numReadings];      // not good - compiling error



What error did you get ?

exit status 1
conflicting declaration 'double readings [10]'

exit status 1
conflicting declaration 'double readings [10]'

How many times did you declare readings in the code ?

Did you have this as well ?

int readings[numReadings];      // the readings from the analog input

UKHeliBob:

int readings[numReadings];      // the readings from the analog input

The reading for averaging is not from analog input pin but Irms.
The signal is on analog input pin - "EmonLib.h"- calculate the amplitude = Irms. I am trying to average Irms not analog input, when I do that (smoothing analog pin ) the average is 2.5V which is the bias on the pin.

This is last code and error

// EmonLibrary examples openenergymonitor.org, Licence GNU GPL V3
#include <LiquidCrystal.h>
LiquidCrystal lcd(PA0, PA1, PA2, PA3, PA4, PA5);


#include "EmonLib.h"                   // Include Emon Library
EnergyMonitor emon1;                   // Create an instance
const int numReadings = 10;


//int readings[numReadings];      // the readings from the analog input
//int readIndex = 0;              // the index of the current reading




//////////////////////////////////////////////////////////////////////

double readings[numReadings];      // not good - compiling error
double readIndex = 0;              //not good - compiling error
//////////////////////////////////////////////////////////////////////


//////////////////
 double total ;
 double average = 0;                // the average
////////////////////
//float total ;                  // the running total
//float average ;                // the average
/////////////////
int Irms;
void setup()
{
  for (int thisReading = 0; thisReading < numReadings; thisReading++)
  {
    readings[thisReading] = 0;
  }
  lcd.begin(16, 2);
  emon1.current(PA7, 2.8);             // Current: input pin, calibration.
}


void loop()


{
  double Irms = emon1.calcIrms(1480);  // Calculate Irms only


  total = total - readings[readIndex];

 //////////////////////////////
  readings[readIndex] = Irms;
  //////////////////////////////

total = total + readings[readIndex];
 readIndex = readIndex + 1;
 if (readIndex >= numReadings) {
 
    readIndex = 0;
  }
 average = total / numReadings;
 
  Serial.println(average);
  delay(1); 
        
  lcd.setCursor(0, 1);
  lcd.print("U=");
  lcd.print(Irms);


  lcd.setCursor(0, 0);
  lcd.print("A=");
  lcd.print(average);
}

AC_smoothing:50:37: error: invalid types 'double [10][double]' for array subscript

total = total - readings[readIndex];

^

AC_smoothing:53:21: error: invalid types 'double [10][double]' for array subscript

readings[readIndex] = Irms;

^

AC_smoothing:56:35: error: invalid types 'double [10][double]' for array subscript

total = total + readings[readIndex];

^

exit status 1
invalid types 'double [10][double]' for array subscript

double readIndex = 0;

The index to an array must be an integer as there will always be 0, 1, 2, 3 etc element in the array. There can never be, for instance, 1.5 elements in the array

Declare readIndex as an integer

So what solution can be, averaging 10 reading of the data = Irms ?
Applying smoothing function to DC voltmeter it is working Ok, I have average in not integer numbers.

We can’t see your code

This change was a mistake:

//int readIndex = 0;              // the index of the current reading
double readIndex = 0;              //not good - compiling error

TheMemberFormerlyKnownAsAWOL:
We can't see your code

Please look post #1, I have to start from beginning.

But you’ve changed that code to make the array and total to be both of type “double”, right?

So post that code.

// EmonLibrary examples openenergymonitor.org, Licence GNU GPL V3
#include <LiquidCrystal.h>
LiquidCrystal lcd(PA0, PA1, PA2, PA3, PA4, PA5);


#include "EmonLib.h"                   // Include Emon Library
EnergyMonitor emon1;                   // Create an instance
const int numReadings = 10;


//int readings[numReadings];      // the readings from the analog input
//int readIndex = 0;              // the index of the current reading




//////////////////////////////////////////////////////////////////////

double readings[numReadings];      // not good - compiling error
double readIndex = 0;              //not good - compiling error
//////////////////////////////////////////////////////////////////////


//////////////////
 double total ;
 double average = 0;                // the average
////////////////////
//float total ;                  // the running total
//float average ;                // the average
/////////////////
int Irms;
void setup()
{
  for (int thisReading = 0; thisReading < numReadings; thisReading++)
  {
    readings[thisReading] = 0;
  }
  lcd.begin(16, 2);
  emon1.current(PA7, 2.8);             // Current: input pin, calibration.
}


void loop()


{
  double Irms = emon1.calcIrms(1480);  // Calculate Irms only


  total = total - readings[readIndex];

 //////////////////////////////
  readings[readIndex] = Irms;
  //////////////////////////////

total = total + readings[readIndex];
 readIndex = readIndex + 1;
 if (readIndex >= numReadings) {
 
    readIndex = 0;
  }
 average = total / numReadings;
 
  Serial.println(average);
  delay(1); 
        
  lcd.setCursor(0, 1);
  lcd.print("U=");
  lcd.print(Irms);


  lcd.setCursor(0, 0);
  lcd.print("A=");
  lcd.print(average);
}