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
}
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
}