Get average value to be a global signal

Hello guys,
i am measuring the magnetic field with a magnetfieldsensor.
The signal is quite wavy thats why i decided to use the average value of 200 measurements.
The problem is i did that in a for loop and it just works when i print the signal inside the loop (see code). How can i use the value as a global variable and get it outside of the loop?Also this is all in void loop function. i wont put the whole code because its too long... Also the sensor uses i2c communication. I just want the value of "mittelwert" to also be used outside of the loop.

rms = sqrt(sq(current[0]) + sq(current[1]) + sq(current[2]));

  mw[Cycles] = rms;
    Cycles = Cycles +1;
    if (Cycles < 200)
    {

    }
    else
    {
    for(int i = 0; i < 200; i++)
    {
        
         mittelwert = mittelwert + mw[i];
    }
    
    mittelwert= mittelwert/200;
    Serial.println(mittelwert);
    Cycles = 0;
    mittelwert = 0;
    }

What prevent this from being done?

for(int i = 0; i < 200; i++)
    {
        
         mittelwert = mittelwert + mw[i];
    }
    
    mittelwert= mittelwert/200;
    Serial.println(mittelwert);
    Cycles = 0;
    mittelwert = 0;
    }
Serial.println(mittelwert);

?

You can put ```Serial.println(mittelwert);`` in different locations as long as the variable is in scope.

i did this but it doesnt work. it just gives me tha value of 0 every time

"It doesn't work" is what I get for trying to solve an issue using a code snippet. Good luck.

I am guessing it is because of the if condition... Is there any other way to take 200 measurements everytime and take the average value ?

No.

It doesn't work because your variable mittelwert was not declared as a global variable. Your posted snippet is useless because it doesn't show where the declaration of mittelwert took place but I am assuming it was within the loop function.

If your full code is too long to post then please write a short code that illustrates your problem.

1 Like

Hello, no i declared it as a global variable. there you go:

#include <Wire.h> //including library for I2C communication

unsigned char CMPS2_address = 0x30; //I2C address of the device

int taster = 7;
int tasterstatus = 0;
float data[3];
unsigned int raw[3];
float offset[3];

float current[3];
float field[3];

float rms;
int Cycles = 0;
int Round = 0;
float mw[200];
float MAX[100];
float mittelwert = 0;
float maximum = 0;
float MinCurrent1 = 1023;       //1. Schwellwert
float MinCurrent2 = 1023;       //2. Schwellwert
static byte alterZustand = 255;     //a value that does not make sense so that the first change is noticed
int Zustand = 0;                // Zustandsvariable
const unsigned long ZeitInterval = 5000; //Soll eine Stunde betragen
unsigned long letzteZeit = 0;

void setup() {
  Serial.begin(9600); //serial initialization
  delay(10);
  CMPS2_init(); //initialize the compass

  pinMode(taster, INPUT);
}



void loop()
{
  tasterstatus = digitalRead(taster);
  if (tasterstatus == LOW)
  {
    Serial.println("Feldunterdrückung");
    for (int i = 0; i < 3; i++)
    {
      field[i] = data[i];
    }
  }

  for (int i = 0; i < 3; i++)
  {
    current[i] = data[i] - field[i];
  }

  rms = sqrt(sq(current[0]) + sq(current[1]) + sq(current[2]));

  mw[Cycles] = rms;
    Cycles = Cycles +1;
    if (Cycles < 200)
    {

    }
    else
    {
    for(int i = 0; i < 200; i++)
    {
         
         mittelwert = mittelwert + mw[i];
    }
    
    mittelwert= mittelwert/200;
    //Serial.println(mittelwert);
Cycles = 0;
mittelwert = 0;
}

Dont put ints into float variables. A 0 is an int a 0.0 is a float.
just trying stuff. Stuff you can do yourself.

Im sorry but i think you misunderstood me... the thing is like this: i have the rms value as you can see. and i want every 200 measurements that the average is calculated (mittelwert). And everything perfectly worked out within the loop. the values i get for mittelwert are good. But when i print mittelwert outside of the loop, it just gives me 0 values. Not the same values as in the loop.

where are data[] and field[] defined?
where is the input value coming from?

leaky integration can be used to maintain a running average
avg += (samp - avg) * K; // K < 1

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