When I compile the Arduino code below I get the message "error: expected initializer before 'int'
int UpdateIntAverage10SequentialDatum::updateIntAverage10SequentialDatum(int datum"
The .cpp file is copied from the function in the Arduino code, which runs as expected. The error occurs when I replace the call to the update10SequentialDatum(int datum, int history[]) function with the call to the updateIntAverage10SequentialDatum() function from the library.
Arduino code is:
#include <UpdateIntAverage10SequentialDatum.h>
UpdateIntAverage10SequentialDatum updateIntAverage10SequentialDatum();
/*
Arrays are passsed by reference!
*/
int ref[10] ;
int rate = 250;
unsigned long timer;
int sampleDatum;
boolean flag;
int update10SequentialDatum(int datum, int history[]) {
/*
updates history with new datum and calculates average int value of data.
*/
int avg;
int sum;
sum = 0;
// shift register values
for (int i = 0 ; i < 9 ; i++) {
history[i] = history[i + 1];
sum = sum + history[i];
}
history[9] = datum; // add latest voltage reading
sum = sum + datum;
avg = sum / 10; //compute average value
return avg;
}
void setup() {
Serial.begin(9600);
timer = millis();
flag = true;
sampleDatum = 0;
}
void loop() {
// put your main code here, to run repeatedly:
if ((millis() - timer) >= rate) {
// Serial.println(updateIntAverage10SequentialDatum(sampleDatum, ref));
// for (int i = 0 ; i < 10 ; i++) {
// Serial.print(ref[i]);
// Serial.print(" ");
// }
// Serial.println("");
timer = millis();
if (flag) {
sampleDatum = sampleDatum + 1;
}
else if (!flag) {
sampleDatum = sampleDatum - 1;
}
if (sampleDatum == 13) {
flag = false ;
}
else if (sampleDatum == -2) {
flag = true ;
}
}
}
The .h file in the library is:
/*
UpdateIntAverage10SequentialDatum.cpp - Library for updating rolling average of
exactly 10 integers in an array.
Created by Fred Perkins, March 10, 2021.
Released into the public domain.
*/
#ifndef UpdateIntAverage10SequentialDatum_h
#define UpdateIntAverage10SequentialDatum_h
#include "Arduino.h"
class UpdateIntAverage10SequentialDatum
{
public:
UpdateIntAverage10SequentialDatum();
int updateIntAverage10SequentialDatum(int datum, int history[]);
private:
};
#endif
The .cpp file in the library is:
/*
UpdateIntAverage10SequentialDatum.cpp - Library for updating rolling average of
exactly 10 integers in an array.
Created by Fred Perkins, March 10, 2021.
Released into the public domain.
*/
#include "Arduino.h"
#include "UpdateIntAverage10SequentialDatum.h"
/*
need terminal semicolon below but not in Morse!
*/
UpdateIntAverage10SequentialDatum::UpdateIntAverage10SequentialDatum()
int UpdateIntAverage10SequentialDatum::updateIntAverage10SequentialDatum(int datum,
int history[]) {
/*
updates history with new datum and calculates average int value of data.
arrays are passed by reference, so updates are passed back to calling code.
*/
int avg;
int sum;
sum = 0;
// shift register values
for (int i = 0 ; i < 9 ; i++) {
history[i] = history[i + 1];
sum = sum + history[i];
}
history[9] = datum; // add latest voltage reading
sum = sum + datum;
avg = sum / 10; //compute average value
return avg;
}
Help would be appreciated. Thanks.