Hi,

I am stuck trying to find out why the average in my program is outputting a low number. In the serial display it has numbers like: average -230 adc0 16811
The average should be close to the input. This problem sill exists even when I eliminate the 16x gain. When I lower the numReadings to 10 or some other number, I get a different average output. I have looked over the code many times.
I just want to have a running average of the input from the 15bit input on the ADS1115.

Here is the code:

#include <Wire.h>

int x;
int y;
int z;

const int analogOutPin = 9;
int outputValue = 0;

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

void setup() {
// initialize serial communication with computer:
Serial.begin(9600);
// initialize all the readings to 0:
}
}

void loop() {

// advance to the next position in the array:

// if we’re at the end of the array…
// …wrap around to the beginning:
}

// calculate the average:
// send it to the computer as ASCII digits
Serial.print(average);
Serial.print(" ");
delay(1); // delay in between reads for stability
}

total is an int. The largest value you can store in an int is 32767. Is is possible that 100 readings exceeds this value?

Use a long int, maybe unsigned long if you're feeling adventurous. See <climits> (limits.h) - C++ Reference

That makes sense. It is going to exceed that after 1 or 2 readings. Is there a way around that? Perhaps float?

I got it to work with float !!!

Here is the code for anyone else who wants to do this:

#include <Wire.h>

int x;
int y;
int z;

const int analogOutPin = 9;
int outputValue = 0;

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

void setup() {
// initialize serial communication with computer:
Serial.begin(9600);
// initialize all the readings to 0:
}
}

void loop() {

// advance to the next position in the array:

// if we’re at the end of the array…
// …wrap around to the beginning:
}

// calculate the average:
// send it to the computer as ASCII digits
Serial.print(average);
Serial.print(" ");