0

I want to set a class for a sensor. One of the attributes of the sensor is an int array that I'd like to pass to a methode to store values in the array. The methode sould be able to write over existing values. I want to avoid vectors (ATtiny85 can't handle it...).

  1. In the method calcAvr, I want to pass the array vec. The Error is:

prototype for 'int Sensor::calcAvr(int*)' does not match any in class 'Sensor' int Sensor::calcAvr(int vec[NUM_VAL]) { //calculates the average

I think the syntax is wrong, what is my mistake?

  1. Is it the right choice to initiate the array with 0 in the class definition? If not, where and how should it be done? (and why is it better there)

  2. How would I pass a reference of vec? I could use the right syntax. Would it be a smaller programm than, or faster?

My code:

#define DIV_LOW 0.8                        //greatest divergence from former 
average allowed
#define DIV_HIGH 1.2                    //smalest divergence from former 
average allowed
#define NUM_VAL 200 

class Sensor { //decleration of class
  public :
  int vec[NUM_VAL] = {0}; //array for holding values of average 
  int maybeVal;           //a placeholder for sensor data
  int  average;

  void setVec();
  **int calcAvr(int);**
  void updateVec();
  };

  //implementation
  void Sensor::setVec() {    //initialize array to calculate the average
    for (int i = 0; i < NUM_VAL; i++) {
    vec[i] = analogRead(AO);
    delay(1);
    }
  }

int Sensor::calcAvr(**int vec[NUM_VAL]**) { //calculates the average
  for (int i; i < NUM_VAL; i++) {
    average += vec[i];
  }
  average /= NUM_VAL;
  return average;
}

void Sensor::updateVec() {
  maybeVal = analogRead(AO);
  char avoidLooping = 0; //avoids endlessly rejecting a bad read.
  if (maybeVal <= DIV_LOW * average || maybeVal >= DIV_HIGH * average) {
    delay(1);
    avoidLooping++;
    if (avoidLooping <= 5) { //if failed to update average more than 5 times
    updateVec();
    }
  }
}

void setup () {
 //  Sensor.vec = {0};
}

void loop() {
}
  • 1
    int calcAvr(int); should be int calcAvr(int*); or int calcAvr(int v[]); – Juraj Mar 2 at 17:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.