issues with my class

Still learning how to use classes, was trying to run my own because I have 26 capacitive sensors that I need to get a quick average from (running average library seems to be too slow for me, need quick response) so this just levels out the samples. I need it to eventually take 26 averaging samples from each sensor so class seemed like best idea.

It works outside of the class but when I moved it over to class it seems to be getting weird readings. any idea what I am doing wrong?

class file:

#include "Arduino.h"

class QuickAverage {
  public:
    QuickAverage();
    int update(int sample);
    void sampleAmount(int changeSampleTo);
    
  private:
    int _sampleAmount,
        _tempAverage,
        _capacitiveAverage;

    long _runningAverage[];
};

QuickAverage::QuickAverage() {
  _sampleAmount = 5;
  _tempAverage = 0;
  _capacitiveAverage = 0;  
  long _runningAverage[_sampleAmount];
};

void QuickAverage::sampleAmount(int changeSampleTo){
  _sampleAmount = changeSampleTo;
}; 

int QuickAverage::update(int sample) {
  for(int i = 0; i < _sampleAmount; i++){
    _runningAverage[i] = sample;
  } 
  // add all the samples up
  for(int i = 0; i < _sampleAmount; i++){
    _tempAverage = _runningAverage[i] + _tempAverage;
  } 
  // divide by number of samples to get average
  _capacitiveAverage =  _tempAverage/_sampleAmount;
  return _capacitiveAverage;
};
#include <CapacitiveSensor.h> // <> for libraries
#include "QuickAverage.h" // "" for files within same folder as sketch...?

QuickAverage a; // average class

const byte numberOfSensors = 1;
const byte sensorPin[] = { 22 }; //will eventually have 26 input pins
const byte ledPin[] = { 8 }; // also eventually 26
const byte capSenseSend = 2;

CapacitiveSensor   cs_a = CapacitiveSensor(capSenseSend,22);

void setup(){
   // setup LEDs
   for(int i = 0; i < numberOfSensors; i++){
     pinMode(ledPin[i],OUTPUT);
     digitalWrite(ledPin[i], LOW);  
   }

   cs_a.set_CS_AutocaL_Millis(0xFFFFFFFF); 
   delay(100);
   Serial.begin(19200);
   delay(100);
   
   a.sampleAmount(5);
}

int allValues[numberOfSensors];

void loop(){
    int aValue = a.update(cs_a.capacitiveSensor(50));
    Serial.print(" A : "); Serial.println(aValue);
    
    int allValues[] = { aValue };
    checkIfisOn(allValues);
    delay(100);
}

int lastReading;
int currentReading;
  

// here is where I take the reading

void checkIfisOn(int curentSensorValue[]){
  boolean firstTime = true;
  int firstRelease = 0;
  const int threshold = 55;
  for(int i = 0; i < numberOfSensors; i++){
    int differenceReading = curentSensorValue[i]-lastReading;
    if(differenceReading > threshold && firstTime == true){ 
      sendSerialData(i);
      fadeLedIn(ledPin[i]);
    if((currentReading-lastReading) < threshold){
      digitalWrite(ledPin[i], LOW); 
      fadeLedOut(ledPin[i]);}
      lastReading = currentReading;
      firstTime = false;
    }
  }
}

void fadeLedIn(int pinNumber){ 
  for(int j = 0; j <= 255; j+=10){
    analogWrite(pinNumber,j);
    delay(10);
  }  
}

void fadeLedOut(int pinNumber){ 
  for(int j = 255; j >= 0; j-=10){
    analogWrite(pinNumber,j);
    delay(10);
  }  
  digitalWrite(pinNumber, LOW); 
}

void sendSerialData(int buttonTouched){
  delay(10);
  Serial.print("Data,");
  delay(10);
  Serial.print(buttonTouched);
  delay(10);
  Serial.print(",");
  Serial.print("sensors : ");
  Serial.print(allValues[0]); 
  delay(10);
  Serial.print("\n");
  delay(10);
}
    long _runningAverage[];

How many elements can you store in this array? Yep, ZERO!

  long _runningAverage[_sampleAmount];

This array, created in the constructor immediately goes out of scope.

PaulS:

    long _runningAverage[];

How many elements can you store in this array? Yep, ZERO!

  long _runningAverage[_sampleAmount];

This array, created in the constructor immediately goes out of scope.

this is where I get confused, so should I declare and initialize the array in the private section? sorry really new to this :frowning:

You need to use either a static array of fixed size, or a pointer to memory dynamically allocated by the constructor.

long _runningAverage[100];

or

long *_runningAverage;
_runningAverage = (long *)malloc(_sampleAmount * sizeof(long));

Don't forget to free() the pointer in the destructor.