Sum divided by n

Hi
On serial monitor I have sensor reading, I need to average them, how I can do that ?

Looks like avey2 being 0 is significant here? (You didn't actually say that....)

Keep track of something like avey2previous or whatever. When avey2 goes to non-0 from 0 start a counter and start accumulating the running total. When it goes to 0 from non-0 calculate the average.

  static long avey2Total = 0;
  static int avey2Count = 0;

  if (avey2 != 0)
  {
    avey2Total += avey2;
    avey2Count++;
  }
  else if (avey2Count > 0)
  {
    int average = avey2Total / avey2Count;
    Serial.print("Average = ");
    Serial.println(average);
    avey2Count = 0;
    avey2Total = 0;
  }

Exactly as you said: sum the data in a variable and then divide by n

Thanks, it is what I was looking for, For some reasons I don't see
Serial.print("Averagex2 = ");
Serial.println(averagex2);
I changed y by x.

   Serial.print(" avx =  ");
  Serial.print(avx);                 //X , green
  Serial.print(" avy =  ");
  Serial.print(avy);            //Y black

if ((avy > 200) && (avy < 250))
   {
    Serial.print(" avx2 =  ");
    Serial.println(avx2);
 }
  else
  {
    Serial.print(" avx2 =  ");
    Serial.println(0);
  
  }


  static long avx2Total = 0;
  static int avx2Count = 0;

  if (avx2 != 0)
  {
    avx2Total += avx2;
    avx2Count++;
  }
  else if (avx2Count > 0)
  {
    int averagex2 = avx2Total / avx2Count;
    Serial.print("Averagex2 = ");
    Serial.println(averagex2);
    avx2Count = 0;
    avx2Total = 0;
  }

Because you show a value of 0 for avx2 if avy is not between 200 and 250 but you don't set avx2 to 0. That removed the clue (avx2 == 0) that a run was beginning and ending.

You can merge the two 'if/else' statements into one:

  Serial.print(" avx =  ");
  Serial.print(avx);            //X , green
  Serial.print(" avy =  ");
  Serial.print(avy);            //Y black

  static long avx2Total = 0;
  static int avx2Count = 0;

  if ((avy > 200) && (avy < 250))
  {
    Serial.print(" avx2 =  ");
    Serial.println(avx2);
    avx2Total += avx2;
    avx2Count++;
  }
  else
  {
    Serial.print(" avx2 =  ");
    Serial.println(0);
    int averagex2 = avx2Total / avx2Count;
    Serial.print("Averagex2 = ");
    Serial.println(averagex2);
    avx2Count = 0;
    avx2Total = 0;
  }

Perfect, THANK YOU

I changed

  else
  {
    Serial.print(" avx2 =  ");
    //Serial.println(0);
    Serial.print(0);

so serial monitor looks better


but there is the problem the " averagex2 "

is displayed for very short time, only one line, will be nice to have it displayed all the time until the value of it is changed

I tried if and while, but have an error

Uy_at_Ux__C:174:23: error: expected ')' before '!' token
while (averagex2 ! = 0) {
^
Uy_at_Ux__C:174:25: error: expected primary-expression before '=' token
while (averagex2 ! = 0) {
^
Uy_at_Ux__C:174:28: error: expected ';' before ')' token
while (averagex2 ! = 0) {
^
Uy_at_Ux__C:182:18: error: 'averagex3' was not declared in this scope
Serial.println(averagex3);

Serial.print(" avx =  ");
  Serial.print(avx);            //X , green
  Serial.print(" avy =  ");
  Serial.print(avy);            //Y black

  int avx3;
  static long avx2Total = 0;
  static int avx2Count = 0;

  if ((avy > 90) && (avy < 150))
  {
    Serial.print(" avx2 =  ");
    Serial.println(avx2);
    avx2Total += avx2;
    avx2Count++;
  }
  else
  {
    Serial.print(" avx2 =  ");
    //Serial.println(0);
    Serial.print(0);
    int averagex2 = avx2Total / avx2Count;
    Serial.print("   Averagex2 = ");
    Serial.print(averagex2);
    ////////////////////////////////////////////////////
    //if  ((averagex2) ! = 0) {
     while (averagex2 ! = 0) {
      int averagex3 = averagex2;
    }
    avx2Count = 0;
    avx2Total = 0;
  }

  Serial.print("   Averagex3 = ");
  Serial.println(averagex3);
  ////////////

Declare these at the top of the program and then just use them in the main part of the program... at the moment they only exist within the scope of those {}.

consider a running average

// running average (leaky integration)

float  avg;
const float K = 0.125;

// -----------------------------------------------------------------------------
void
loop ()
{
    int samp = analogRead (A0);

    if (0 != avg)
        avg += (samp - avg) * K;
    else
        avg = samp;

    Serial.println (avg, 0.01);
    delay (250);
}

// -----------------------------------------------------------------------------
void
setup ()
{
    Serial.begin (9600);
}

I am trying to add a red column

  Serial.print(" avx =  ");
  Serial.print(avx);            //X , green
  Serial.print(" avy =  ");
  Serial.print(avy);            //Y black

  static long avx2Total = 0;
  static int avx2Count = 0;
  static int averagex2 = 0;

  if ((avy > 200) && (avy < 250))
  {
    Serial.print(" avx2 =  ");
    Serial.println(avx2);
    avx2Total += avx2;
    avx2Count++;
  }
  else
  {
    Serial.print(" avx2 =  ");
    //Serial.println(0);
    Serial.print(0);

    // If a run of more than 0 values has ended,
    // calculate a new average
    if (avx2Count > 0)
    {
      averagex2 = avx2Total / avx2Count;
      // Prepare for the net run
      avx2Count = 0;
      avx2Total = 0;
    }
  }

  Serial.print(" .   averagex3 = ");
  Serial.println(averagex2);

Thanks, it is working as need it

If you always want the current average on the right, just change ".println" to ".print" in this part:

  if ((avy > 200) && (avy < 250))
  {
    Serial.print(" avx2 =  ");
    Serial.println(avx2);
    avx2Total += avx2;
    avx2Count++;
  }

Much better, now it is easy to read.


and orange line is visible

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.