Trouble with CapacitiveSensor library

I am trying to implement the CapacitiveSensor 4.0 library on my project but I face some trouble.

The problem is that I don't want the number of capacitive sensors and their pin numbers to be predefined at the program (like they are in the example sketch of the lib). I want to save the configuration (number of sensors and their pin numbers) to EEPROM, retrieve it on each start up and define the capacitive sensors accordingly. The only thing that is known is the 'send pin' that is always the same and that the number of sensors is 0-4.

Here is a minimal sketch that shows what I'm trying to do:

#include <CapacitiveSensor.h>

int sensor_count;
int sensor_pin[3];
int send_pin = 4;
CapacitiveSensor cs[3];

void setup() {
  /* here goes a procedure to access the EEPROM,
     get the number of sensors and their pin numbers,
     and put them in an array. The number of sensors
     will always be 0-4. */
     
  if (sensor_count >= 1) cs[0] = CapacitiveSensor(send_pin,sensor_pin[0]);
  if (sensor_count >= 2) cs[1] = CapacitiveSensor(send_pin,sensor_pin[1]);
  if (sensor_count >= 3) cs[2] = CapacitiveSensor(send_pin,sensor_pin[2]);
  if (sensor_count == 4) cs[3] = CapacitiveSensor(send_pin,sensor_pin[3]); 
}

void loop() {
  // poll the sensors once in a while and do other stuff
}

Unfortunately I am getting this error when trying to compile:

capacitive_sensor:10: error: no matching function for call to 'CapacitiveSensor::CapacitiveSensor()'
Z:\Documents\Arduino\libraries\CapacitiveSensor/CapacitiveSensor.h:24: note: candidates are: CapacitiveSensor::CapacitiveSensor(uint8_t, uint8_t)
Z:\Documents\Arduino\libraries\CapacitiveSensor/CapacitiveSensor.h:20: note: CapacitiveSensor::CapacitiveSensor(const CapacitiveSensor&)

I hope someone here can help me. Thanks in advance!

CapacitiveSensor cs[3];

Here you are creating an array of 3 sensors, using the no-argument constructor. Unfortunately, the class does not have a no-argument constructor.

You will need to change the class to provide a no-argument constructor and a way to provide those arguments later.

Or, you would need to change the declaration to:

CapacitiveSensor *cs[3];

Then, change this:

  if (sensor_count >= 1) cs[0] = CapacitiveSensor(send_pin,sensor_pin[0]);
  if (sensor_count >= 2) cs[1] = CapacitiveSensor(send_pin,sensor_pin[1]);
  if (sensor_count >= 3) cs[2] = CapacitiveSensor(send_pin,sensor_pin[2]);
  if (sensor_count == 4) cs[3] = CapacitiveSensor(send_pin,sensor_pin[3]);

to

  if (sensor_count >= 1) cs[0] = new CapacitiveSensor(send_pin,sensor_pin[0]);
  if (sensor_count >= 2) cs[1] = new CapacitiveSensor(send_pin,sensor_pin[1]);
  if (sensor_count >= 3) cs[2] = new CapacitiveSensor(send_pin,sensor_pin[2]);
  if (sensor_count == 4) cs[3] = new CapacitiveSensor(send_pin,sensor_pin[3]);

Of course, trying to stuff 4 things in a 3 element array is not a goof idea.

Later in the code, you'd need to change all "cs[n]." to "cs[n]->".

Thank you very much for your reply, I'm trying your suggestions now.

I thought that by int array[3] a 4 element array was created, since the element count starts from 0.

The value in the brackets is the number of elements, not the upper index.

The new code compiles and runs nicely :slight_smile: Thanks again.