Smoothing with Adafruit_ADS1115 ads

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>
#include <Adafruit_ADS1015.h>

Adafruit_ADS1115 ads;
int x;
int y;
int z;

const int analogOutPin = 9;
int outputValue = 0;

const int numReadings = 100;

int readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
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:
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
ads.setGain(GAIN_SIXTEEN);
ads.begin();
}

void loop() {

int16_t adc0;
adc0 = ads.readADC_SingleEnded(0);

z = ads.readADC_SingleEnded(0);

// subtract the last reading:
total = total - readings[readIndex];
// read from the sensor:
readings[readIndex] = z;
// add the reading to the total:
total = total + readings[readIndex];
// advance to the next position in the array:
readIndex = readIndex + 1;

// if we’re at the end of the array…
if (readIndex >= numReadings) {
// …wrap around to the beginning:
readIndex = 0;
}

// calculate the average:
average = (total / numReadings);
// send it to the computer as ASCII digits
Serial.print(average);
Serial.print(" ");
Serial.println(adc0);
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>
#include <Adafruit_ADS1015.h>

Adafruit_ADS1115 ads;
int x;
int y;
int z;

const int analogOutPin = 9;
int outputValue = 0;

const int numReadings = 150;

float readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
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:
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
ads.setGain(GAIN_SIXTEEN);
ads.begin();
}

void loop() {

int16_t adc0;
adc0 = ads.readADC_SingleEnded(0);

z = ads.readADC_SingleEnded(0);

// subtract the last reading:
total = total - readings[readIndex];
// read from the sensor:
readings[readIndex] = z;
// add the reading to the total:
total = total + readings[readIndex];
// advance to the next position in the array:
readIndex = readIndex + 1;

// if we’re at the end of the array…
if (readIndex >= numReadings) {
// …wrap around to the beginning:
readIndex = 0;
}

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

Why are you using a float array to hold a collection of int values?

Why are you using a float to hold the sum of a bunch of integers?

Why did you not pay attention to ALL of reply #1?

float is really slow, use long

Sure. I tried long and that worked too. I will use long because it is more proper. Thanks guys!