Why (aread * 1000); is zero?

Hi
I did that many time before but this time is not working, what I forgot ?

#include "EmonLib.h"
EnergyMonitor emon1;
int aread, newread;

void setup()
{
  Serial.begin(115200);
  emon1.current(0, 111.1);// Current: input pin A1, calibration.
}

void loop()
{
  double Irms = emon1.calcIrms(1480);  // Calculate Irms only
  //////////////
  //newread = analogRead(A0);
  newread = Irms;
  if (newread > aread + 20 || newread < aread - 20 ) aread = newread;
  ////////////////

  Serial.print(Irms * 1000);    // Irms
  Serial.print("  average ");
  Serial.println(aread * 1000); // average
}

Because (newread > aread + 20 || newread < aread - 20 ) always evaluates to false (for whatever reason).

If you put some more print statements in, you might be able to find it.

void loop()
{
  double Irms = emon1.calcIrms(1480);  // Calculate Irms only
  //////////////
  //newread = analogRead(A0);
  newread = Irms;

  Serial.print(F("Irms = ")); Serial.println(Irms);
  Serial.print(F("aread = ")); Serial.println(aread);
  Serial.print(F("newread = ")); Serial.println(newread);

  
  if (newread > aread + 20 || newread < aread - 20 )
  {
    Serial.println(F"(adjusting aread")):
    aread = newread;
  }
  ////////////////

  Serial.print(Irms * 1000);    // Irms
  Serial.print("  average ");
  Serial.println(aread * 1000); // average
}

And else what is aread Value? The default one for global variables is 0

Also are you sure about 0 meaning A1?

emon1.current(0, 111.1);// Current: input pin A1, calibration.

Mistake , should be A0

Make sure you connected to the right pin, could possibly explain why the value did not change

I'm trying to adopt this program for Irms

int aread, newread;
void setup() {

Serial.begin(115200);
}
void loop()
{  
newread = analogRead(A0);
if (newread > aread +2 || newread < aread -2 ) aread=newread;
Serial.print(newread);
Serial.print(" newread ");
Serial.println(aread+1);

//newread = analogRead(A0);
newread = Irms;

Yes we understand what you are trying to do and the code is OK

Your challenge is that the condition is such that you never make it through the if() and so the value never changes apparently as explained by @sterretje. Try his code

Newread and aread are int so they loose the decimal part of the double

So I multiplied everything by 1000

#include "EmonLib.h"
EnergyMonitor emon1;
//int aread, newread;
float aread, newread;

void setup()
{
  Serial.begin(115200);
  emon1.current(0, 111.1);// Current: input pin A0, calibration.
}

void loop()
{
  int F;
  double Irms = emon1.calcIrms(1480);  // Calculate Irms only
  //////////////
  //newread = analogRead(A0);
  newread = Irms;

  Serial.print(F("Irms = ")); Serial.print(Irms*1000);// 0k
  Serial.print(F("  aread = ")); Serial.print(aread*1000);// = 0
  Serial.print(F("  newread = ")); Serial.print(newread*1000);// Ok


  if (newread > aread + 20 || newread < aread - 20 )
  {
    Serial.print (F("  adjusting aread  "));
    aread = newread;
  }
  ////////////////
  Serial.print( " Irms * 1000 = ");
  Serial.print(  Irms * 1000);    // Irms
  Serial.print("  average ");
  Serial.println(aread * 1000); // average = 0
}

You want to see the values, not the values multipled by 1000.

Irms is 0.24058 and aread is zero so your condition will be false; the difference is 0.24058 which is smaller than 20.

I multiply by 1000 to have numbers higher than 0.

That is the name of the topic = why ?
and how to fix it,

Doesn't matter as long as aread is not a zero

Let me try again.

newread equals 0.24058. Initially aread equals zero. So newread is smaller that 0 plus 20 and the first part of the condition is false. newread is also greater than 0 minus 20 so the second part of the condition is also false. And hence you will never see "adjusting read" and aread will not be updated.

I did this way , I have some numbers instead of 0, testing


  double Irms = emon1.calcIrms(1480);  // Calculate Irms only
  double Irms2 = Irms*1000; 
  //////////////
  //newread = analogRead(A0);
  newread = Irms2;

  Serial.print(F("Irms2 = ")); Serial.print(Irms2);// 0k
  Serial.print(F("  aread = ")); Serial.print(aread*1000);// = 0
  Serial.print(F("  newread = ")); Serial.print(newread*1000);// Ok

the results, aread = average= not good

#include "EmonLib.h"
EnergyMonitor emon1;
//int aread, newread;
float aread, newread;

void setup()
{
  Serial.begin(115200);
  emon1.current(0, 111.1);// Current: input pin A0, calibration.
}

void loop()
{
  int F;
  double Irms = emon1.calcIrms(1480);  // Calculate Irms only
  double Irms2 = Irms*1000; 
  //////////////
  //newread = analogRead(A0);
  newread = Irms2;

  Serial.print(F("Irms2 = ")); Serial.print(Irms2);// 0k
  Serial.print(F("  aread = ")); Serial.print(aread*1000);// = 0
  Serial.print(F("  newread = ")); Serial.print(newread*1000);// Ok


  if (newread > aread + 20 || newread < aread - 20 )
    //if (newread > aread + 0.1 || newread < aread - 0.1 )
  {
    Serial.print (F("  adjusting aread  "));
    aread = newread;
  }
  ////////////////
 // Serial.print( " Irms * 1000 = ");
 // Serial.print(  Irms * 1000);    // Irms
  Serial.print("  average ");
  Serial.println(aread * 1000); // average = 0
}

Please explain that. If you are trying to calculate an average, your approach is wrong.

This is a program with nice averaging - green line


And this is AC voltmeter with lot of noise,

The goal is to apply the first program to AC voltmeter.