# Running Average Question

Hello everyone,

I am trying to use the running average library (http://playground.arduino.cc/Main/RunningAverage), but I need help. I am using 16 OPT101s to measure light intensity. I would like a rolling average of the light intensities from the 16 analog sensors. However, every time I run the code, I get 0 as my average.

I am following this example code:

``````#include <RunningAverage.h>

RunningAverage myRA(16);
int samples = 0;

void setup(){
myRA.clear();
}

void loop(){
for(int i = 0; i < 16; i ++){

long rn = LightIntensity;
//where light intensity is the value from my sensor entered into the equation of a line
samples++;
Serial.print(myRA.getAverage(), 4);

if (samples == 16);{
samples = 0;
myRA.clear();
}

Serial.println();

delay(1000);
}
``````

I think the problem comes from the long because if I change long to float, it prints the right data; however, it doesn’t compute the average.

``````    long rn = LightIntensity;
//where light intensity is the value from my sensor entered into the equation of a line
``````

Perhaps the fine folks at http://snippets-r-us.com could help you. Here, we need to see ALL of your code.

The code you posted does not compile so we cannot recreate the problem.

First thing to do is to be sure your get readings that are different from 0.

furthermore
myRA.addValue(rn/100); rn/100 is probably an integer division which truncates values so x = 99/100 ==> x = 0

use floating point numbers to circumvent this problem.
float rn = LightIntensity();

robtillaart:
The code you posted does not compile so we cannot recreate the problem.

First thing to do is to be sure your get readings that are different from 0.

furthermore
myRA.addValue(rn/100); rn/100 is probably an integer division which truncates values so x = 99/100 ==> x = 0

use floating point numbers to circumvent this problem.
float rn = LightIntensity();

I’m sorry.
This is my entire code:

``````#include <RunningAverage.h>

RunningAverage myRA(16);
int samples = 0;

//Mux control pins
int s0 = 8;
int s1 = 9;
int s2 = 10;
int s3 = 11;

//Mux in "SIG" pin
int SIG_pin = 0;

void setup(){
pinMode(s0, OUTPUT);
pinMode(s1, OUTPUT);
pinMode(s2, OUTPUT);
pinMode(s3, OUTPUT);

digitalWrite(s0, LOW);
digitalWrite(s1, LOW);
digitalWrite(s2, LOW);
digitalWrite(s3, LOW);

Serial.begin(9600);

while (!Serial) {
;
}

myRA.clear();

}

void loop(){

//Loop through and read all 16 values
//Reports back Value at channel 6 is: 346
for(int i = 0; i < 16; i ++){

Serial.print(i);
Serial.print("\t");
float OutputVoltage = readMux(i) / 1023.0 * 5.0;
Serial.print(OutputVoltage, 4);
Serial.print("\t");
float LightIntensity = (OutputVoltage * (391.0/880.0)) + (1.0/880.0);
Serial.print(LightIntensity, 4);
Serial.print("\t");

long rn = LightIntensity;
samples++;
Serial.print(myRA.getAverage(), 4);

if (samples == 16);{
samples = 0;
myRA.clear();
}

Serial.println();

delay(1000);
}

}

int controlPin[] = {s0, s1, s2, s3};

int muxChannel={
{0,0,0,0}, //channel 0
{1,0,0,0}, //channel 1
{0,1,0,0}, //channel 2
{1,1,0,0}, //channel 3
{0,0,1,0}, //channel 4
{1,0,1,0}, //channel 5
{0,1,1,0}, //channel 6
{1,1,1,0}, //channel 7
{0,0,0,1}, //channel 8
{1,0,0,1}, //channel 9
{0,1,0,1}, //channel 10
{1,1,0,1}, //channel 11
{0,0,1,1}, //channel 12
{1,0,1,1}, //channel 13
{0,1,1,1}, //channel 14
{1,1,1,1}  //channel 15
};

//loop through the 4 sig
for(int i = 0; i < 4; i ++){
digitalWrite(controlPin[i], muxChannel[channel][i]);
}

//read the value at the SIG pin

//return the value
return val;
}

//Header for the data (in void setup)
String OPT101 = "OPT101 Number";
String OutputVoltage = "Output Voltage";
String LightIntensity = "Light Intensity";
String AvgLightIntensity = "Avg Light Intensity";
String HeaderString = OPT101 + "\t" + OutputVoltage + "\t" + " " + LightIntensity + "\t" +  AvgLightIntensity;

delay(1000);
}
``````
``````if (samples == 16);{
``````

You need to delete this semicolon.

``````    myRA.addValue(rn/0.01);
``````

previously you were attempting to divide by 100, now you are multiplying by 100
inefficiently!