Averaging readings

I am trying to add averaging to a standard sketch that is a demo code for the ADC1115. This is the standard code:

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 or 0x3f for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 20, 4);
#include <Adafruit_ADS1015.h>
Adafruit_ADS1115 ads(0x48);
float AverageVoltage = 0.0;
float Voltage = 0;
int MeasurementsToAverage = 16;

 
void setup(void)
{
  lcd.init();
  lcd.backlight();
  Serial.begin(57600);
  ads.begin();
}
 
void loop(void)
{
int16_t adc0; // 16 bits ADC read of input A0
adc0 = ads.readADC_SingleEnded(0);
Voltage = (adc0 * 0.1875)/1000;

  for(int i = 0; i < MeasurementsToAverage; ++i)
  {
    AverageVoltage += Voltage();
    delay(1);
  }
  AverageVoltage /= MeasurementsToAverage
 
  Serial.print("AIN0: ");
  Serial.print(adc0);
  Serial.print("\tAverageVoltage: ");
  Serial.println(AverageVoltage, 7);
  Serial.println();  
  
delay(10);
}

I found an averaging code:

 float AverageTemperature = 0;
  int MeasurementsToAverage = 16;
  for(int i = 0; i < MeasurementsToAverage; ++i)
  {
    AverageTemperature += MeasureTemperature();
    delay(1);
  }
  AverageTemperature /= MeasurementsToAverage;

from Three Methods to Filter Noisy Arduino Measurements | Coding | MegunoLink

Changing some of the variable names, I have assembled this sketch:

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 or 0x3f for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 20, 4);
#include <Adafruit_ADS1015.h>
Adafruit_ADS1115 ads(0x48);
float AverageVoltage = 0.0;
float Voltage = 0;
int MeasurementsToAverage = 16;

 
void setup(void)
{
  lcd.init();
  lcd.backlight();
  Serial.begin(57600);
  ads.begin();
}
 
void loop(void)
{
int16_t adc0; // 16 bits ADC read of input A0
adc0 = ads.readADC_SingleEnded(0);
Voltage = (adc0 * 0.1875)/1000;

  for(int i = 0; i < MeasurementsToAverage; ++i)
  {
    AverageVoltage += Voltage();
    delay(1);
  }
  AverageVoltage /= MeasurementsToAverage
 
  Serial.print("AIN0: ");
  Serial.print(adc0);
  Serial.print("\tAverageVoltage: ");
  Serial.println(AverageVoltage, 7);
  Serial.println();  
  
delay(10);
}

When compiling the sketch, I get the following error:

sketchwith_average.ino: In function ‘void loop()’:

Test_ADC1115_LCD_sketchwith_average:28:31: error: ‘Voltage’ cannot be used as a function

AverageVoltage += Voltage();

^

Test_ADC1115_LCD_sketchwith_average:33:3: error: expected ‘;’ before ‘Serial’

Serial.print("AIN0: ");

Could someone please tell me where I went wrong?

    AverageVoltage += Voltage();
    delay(1);

Remove the "()" and the useless delay to fix the immediate problem, but that won't work in any case.

Voltage needs to be updated in the averaging loop, with a new measurement.

We strongly recommend that beginners start with the simple examples that come with Arduino, in order to learn the programming language and the special features of the Arduino. Expect endless frustration if you skip this vital step.

error is pretty clear that you 'forgot' to terminate

AverageVoltage /= MeasurementsToAverage

with ; ( semi colon)

(deleted)

It might be easier to use a library that handles the filtering for you, like the Arduino Filters library:

[color=#5e6d03]#include[/color] [color=#434f54]<[/color][b][color=#d35400]Filters[/color][/b][color=#434f54].[/color][color=#000000]h[/color][color=#434f54]>[/color] [color=#434f54]// [/color][u][color=#434f54]https://github.com/tttapa/Arduino-Filters[/color][/u]
[color=#5e6d03]#include[/color] [color=#434f54]<[/color][b][color=#d35400]Filters[/color][/b][color=#434f54]/[/color][color=#000000]SMA[/color][color=#434f54].[/color][color=#000000]hpp[/color][color=#434f54]>[/color]
[color=#5e6d03]#include[/color] [color=#434f54]<[/color][color=#000000]Adafruit_ADS1015[/color][color=#434f54].[/color][color=#000000]h[/color][color=#434f54]>[/color]

[color=#434f54]// ADC[/color]
[color=#000000]Adafruit_ADS1115[/color] [color=#000000]ads[/color][color=#000000]([/color][color=#000000]0x48[/color][color=#000000])[/color][color=#000000];[/color]

[color=#434f54]// Simple moving average of 16 samples[/color]
[color=#000000]SMA[/color][color=#434f54]<[/color][color=#000000]16[/color][color=#434f54]>[/color] [color=#000000]average[/color][color=#000000];[/color]

[color=#00979c]void[/color] [color=#5e6d03]setup[/color][color=#000000]([/color][color=#000000])[/color] [color=#000000]{[/color]
  [b][color=#d35400]Serial[/color][/b][color=#434f54].[/color][color=#d35400]begin[/color][color=#000000]([/color][color=#000000]57600[/color][color=#000000])[/color][color=#000000];[/color]
  [color=#000000]ads[/color][color=#434f54].[/color][color=#d35400]begin[/color][color=#000000]([/color][color=#000000])[/color][color=#000000];[/color]
[color=#000000]}[/color]

[color=#00979c]void[/color] [color=#5e6d03]loop[/color][color=#000000]([/color][color=#000000])[/color] [color=#000000]{[/color]
  [color=#434f54]// 16 bits ADC read of input A0[/color]
  [color=#00979c]uint16_t[/color] [color=#000000]adc0[/color] [color=#434f54]=[/color] [color=#000000]ads[/color][color=#434f54].[/color][color=#000000]readADC_SingleEnded[/color][color=#000000]([/color][color=#000000]0[/color][color=#000000])[/color][color=#000000];[/color]

  [color=#434f54]// Compute the running average of the adc measurements[/color]
  [color=#00979c]uint16_t[/color] [color=#000000]avg_adc0[/color] [color=#434f54]=[/color] [color=#000000]average[/color][color=#000000]([/color][color=#000000]adc0[/color][color=#000000])[/color][color=#000000];[/color]
  [color=#434f54]// Convert ADC units to a voltage[/color]
  [color=#00979c]float[/color] [color=#000000]averageVoltage[/color] [color=#434f54]=[/color] [color=#000000]([/color][color=#000000]avg_adc0[/color] [color=#434f54]*[/color] [color=#000000]0.1875[/color][color=#000000])[/color] [color=#434f54]/[/color] [color=#000000]1000[/color][color=#000000];[/color]
  
  [b][color=#d35400]Serial[/color][/b][color=#434f54].[/color][color=#d35400]print[/color][color=#000000]([/color][color=#005c5f]"AIN0: "[/color][color=#000000])[/color][color=#000000];[/color]
  [b][color=#d35400]Serial[/color][/b][color=#434f54].[/color][color=#d35400]print[/color][color=#000000]([/color][color=#000000]adc0[/color][color=#000000])[/color][color=#000000];[/color]
  [b][color=#d35400]Serial[/color][/b][color=#434f54].[/color][color=#d35400]print[/color][color=#000000]([/color][color=#005c5f]"\tAverageVoltage: "[/color][color=#000000])[/color][color=#000000];[/color]
  [b][color=#d35400]Serial[/color][/b][color=#434f54].[/color][color=#d35400]println[/color][color=#000000]([/color][color=#000000]averageVoltage[/color][color=#434f54],[/color] [color=#000000]7[/color][color=#000000])[/color][color=#000000];[/color]

  [color=#d35400]delay[/color][color=#000000]([/color][color=#000000]10[/color][color=#000000])[/color][color=#000000];[/color]
[color=#000000]}[/color]

Pieter

Wow, thank you so much all for your replies. I am going to try both modifying the existing code and using Pieters code and compare.