Creating class for capacitive moisture sensor - problem with pointer?

Hi all,

I am playing around with some capacitive moisture sensors that output an analog value using an ESP32 with the Arduino IDE.

My goal is to have about 7 sensors in my new vegetable garden and output those readings to a very basic webpage.

I figured since I will be using 7 of them, it would be a good idea to write a class for them to keep the code a bit tidier.

What I have done (successfully) so far:
Constructor
Getter for pin number
Setter for reading raw ADC values into object
Getter for getting raw ADC values from object

Now I am trying to use the digitalSmooth library Arduino Playground - DigitalSmooth

I have successfully tested the library in a basic sketch before I started writing this class.

However now when I call the digitalSmooth method from within my setup(), instead of returning a value that makes sense,I get a core panic.

Here is the output (over and over as it keeps rebooting):

34
3391
In digitalSmooth: 3391
Guru Meditation Error: Core  1 panic'ed (StoreProhibited). Exception was unhandled.
Core 1 register dump:
PC      : 0x400d0c20  PS      : 0x00060930  A0      : 0x800d0cfa  A1      : 0x3ffb1f60  
A2      : 0x3ffc0194  A3      : 0x00000d3f  A4      : 0x00000000  A5      : 0x3ffc0490  
A6      : 0x00000001  A7      : 0x00000000  A8      : 0x00000004  A9      : 0x3ffc00bc  
A10     : 0x00000000  A11     : 0x3ffc0188  A12     : 0x0000000a  A13     : 0x3ffb1e60  
A14     : ⸮⸮

So I can see it prints out all of the stuff before digitalSmooth. I havent ever seen a core panic before, so I’m a bit lost. I’m pretty sure the problem may have something to do with the the variable sensSmoothArray being passed to the method, but I’m not great with pointers.

I noticed in the example from the link above, when they call the method they don’t need to use the dereference operator, however in order to get it to compile I had to add it.

Can anyone please suggest what I am doing wrong?

Here is my code:

/*
 *  MoistureSensor.h
 *  Library for usng multiple Capacitive Moisture Sensors
 */
#ifndef MoistureSensor_h
#define MoistureSensor_h

#include "Arduino.h"

#define filterSamples   51              // filterSamples should  be an odd number, no smaller than 3

class MoistureSensor
{
  private:
    int _pin;
    int rawData;      // raw ADC values
    int smoothData;  // variables for sensor data

  public:
    MoistureSensor(int pin);    // constructor
    int *sensSmoothArray [filterSamples];   // array for holding raw sensor values for sensor1 
    int getPin(void);       // get pin number for a given sensor
    int digitalSmooth(int, int*);
    void setRawData(void);  // setter for result of ADC read
    int getRawData(void);   // getter for result of ADC read
};

#endif
/*
  * MoistureSensor.cpp
  */
 
 #include "Arduino.h"
 #include "MoistureSensor.h"


 MoistureSensor::MoistureSensor(int pin)
 {
    _pin = pin;
 }


 int MoistureSensor::getPin(void)
 {
  return _pin;
 }

 void MoistureSensor::setRawData()
  {
    rawData = analogRead(_pin);
  }

    
  int MoistureSensor::getRawData(void)
  {
    return rawData;
  }


  int MoistureSensor::digitalSmooth(int rawIn, int *sensSmoothArray)
  {
    Serial.print("In digitalSmooth: "); Serial.println(rawIn);
    int j, k, temp, top, bottom;
    long total;
    static int i;
    // static int raw[filterSamples];
    static int sorted[filterSamples];
    boolean done;
  
    i = (i + 1) % filterSamples;    // increment counter and roll over if necc. -  % (modulo operator) rolls over variable
    sensSmoothArray[i] = rawIn;                 // input new data into the oldest slot
  
    // Serial.print("raw = ");
  
    for (j = 0; j < filterSamples; j++) { // transfer data array into anther array for sorting and averaging
      sorted[j] = sensSmoothArray[j];
    }
  
    done = 0;                // flag to know when we're done sorting
    while (done != 1) {      // simple swap sort, sorts numbers from lowest to highest
      done = 1;
      for (j = 0; j < (filterSamples - 1); j++) {
        if (sorted[j] > sorted[j + 1]) {    // numbers are out of order - swap
          temp = sorted[j + 1];
          sorted [j + 1] =  sorted[j] ;
          sorted [j] = temp;
          done = 0;
        }
      }
  }

  /*
    for (j = 0; j < (filterSamples); j++){    // print the array to debug
      Serial.print(sorted[j]);
      Serial.print("   ");
    }
    Serial.println();
  */

  // throw out top and bottom 15% of samples - limit to throw out at least one from top and bottom
  bottom = max(((filterSamples * 15)  / 100), 1);
  top = min((((filterSamples * 85) / 100) + 1  ), (filterSamples - 1));   // the + 1 is to make up for asymmetry caused by integer rounding
  k = 0;
  total = 0;
  for ( j = bottom; j < top; j++) {
    total += sorted[j];  // total remaining indices
    k++;
    // Serial.print(sorted[j]);
    // Serial.print("   ");
  }

  //  Serial.println();
  //  Serial.print("average = ");
  //  Serial.println(total/k);
  smoothData = total / k;
  Serial.print("Smooth Data Variable: "); Serial.println(smoothData);
  //Serial.print("\n\nDigitalSmooth Reading: "); Serial.println(MoistureSensor::getSmoothData());
  return total / k;    // divide by number of samples
 }
#include "MoistureSensor.h"

MoistureSensor sensor1(34);

void setup() {
  Serial.begin(115200);
  Serial.println(sensor1.getPin());
  sensor1.setRawData();
  int raw = sensor1.getRawData();
  Serial.println(raw);
  int smoothed = sensor1.digitalSmooth(raw, *sensor1.sensSmoothArray);
  //Serial.println(smoothed);
  
  

}
void loop() {
  // put your main code here, to run repeatedly:

}

These may have to be changed:
#include “Arduino.h”
#include “MoistureSensor.h”
To:
#include <Arduino.h>
#include <MoistureSensor.h>

Fairly sure that should be...

int sensSmoothArray [filterSamples];   // array for holding raw sensor values for sensor1

It’s an array of int, not an array of pointers to int.

And this should be...

int smoothed = sensor1.digitalSmooth(raw, sensor1.sensSmoothArray);

Although I don’t understand why you are passing in any parameters to this function at all? The values you are supplying are all variables in the MoistureSensor class, so you could just access them directly.