Using an Array for Capacitance Sensors

Hello all, I'm very much a novice so please be kind. I'm attempting to create several capacitance sensors based on a single value in my code. I've changed this to be a macro rather than an integer. I realize that this had previously given me an error.

I'm attempting to create these sensors via an array but I'm wondering if this is beyond the capacitance library's scope or if I'm missing something. The array size won't change during running so I don't think I need to worry about a heap overflow. It will be the same once compiled.

I've looked around a good bit on both here and a few other locations and can't seem to figure it out. I've seen a similar topic about creating an array for servos but for the life of me I'm unable to find out what my issue is.

In essence, it won't compile in the Arduino IDE and gives me an error that there's no matching function in the Capacitance library, I assume this is because when I declare the array it is not in the same format as a single capacitance sensor.

Any suggestions?

#include <CapacitiveSensor.h>
#define NUMBER_OF_KEYS 2
int threshold = 1000;
int i;
CapacitiveSensor capSensor[NUMBER_OF_KEYS];

void setup() {

  for(int i=0; i < NUMBER_OF_KEYS - 1; i++){
    CapacitiveSensor capSensor[i] = CapacitiveSensor(13, i + 2);
  }
    
  for(int i=1; i = NUMBER_OF_KEYS; i++){
    pinMode(NUMBER_OF_KEYS + i, OUTPUT);
  }
   
}

void loop() {
  long sensorValue;
  
  for(int i=0; i = NUMBER_OF_KEYS; i++){
    sensorValue = capSensor[i].capacitiveSensor(30);
    if(sensorValue > threshold){
      digitalWrite(NUMBER_OF_KEYS + 1 + i, HIGH);
    }
    else{
      digitalWrite(NUMBER_OF_KEYS + 1 + i, LOW);
    }
    
    delay(10);
  }    
}

Yeah, you want to construct the objects in setup() so instead, you could try to create an array of pointers to the CapacitiveSensor objects and de-reference the array with "->", kind of like this:

(unTested but compiles)

#include <CapacitiveSensor.h>
#define NUMBER_OF_KEYS 2
int threshold = 1000;
int i;
CapacitiveSensor* capSensorPtr[NUMBER_OF_KEYS];

void setup() 
{
  CapacitiveSensor* capSensor[NUMBER_OF_KEYS];
  for(int i=0; i < NUMBER_OF_KEYS - 1; i++)
  {  
    capSensor[i] =  new CapacitiveSensor(13, i + 2);
    capSensorPtr[i] = capSensor[i];
  }
    
  for(int i=1; i = NUMBER_OF_KEYS; i++){
    pinMode(NUMBER_OF_KEYS + i, OUTPUT);
  }
   
}

void loop() {
  long sensorValue;
  
  for(int i=0; i = NUMBER_OF_KEYS; i++){
    sensorValue = capSensorPtr[i]->capacitiveSensor(30);
    if(sensorValue > threshold){
      digitalWrite(NUMBER_OF_KEYS + 1 + i, HIGH);
    }
    else{
      digitalWrite(NUMBER_OF_KEYS + 1 + i, LOW);
    }
    
    delay(10);
  }    
}

(unTested but compiles)

Good thing you included that disclaimer.

You have a global array of pointers and a local array of pointers. You populate the local array and then make the global array point to the local array. The local array then goes out of scope, deleting all the instance. Your global array is then full of pointers to garbage.

The local array is completely useless.

This would be better:

CapacitiveSensor* capSensor[NUMBER_OF_KEYS];

void setup()
{
  for(int i=0; i < NUMBER_OF_KEYS - 1; i++)
  { 
    capSensor[i] =  new CapacitiveSensor(13, i + 2);

@PaulS.

Thanks for cleaning that up, Paul. I think I had an aneurism and cut/pasted a mess....

:confused:

Thanks for the help all. I've been stumbling my way through learning pointers and now have something that compiles but I'm afraid this isn't exactly a victory. My LEDs aren't working so after establishing a serial monitor I'm not seeing any values for sensor readings. Apologies for making a meal of this one but I'm not doing so well with it. I've looked around the for loop values and believe the correct pins are indexed to the correct sensors. I don't think it's a wiring issue as it's a rather simple circuit. Any other suggestions? :confused:

#include <CapacitiveSensor.h>
#define NUMBER_OF_KEYS 2
int threshold = 300;
int i;
CapacitiveSensor* capSensor[NUMBER_OF_KEYS];

void setup() {
  Serial.begin(9600);

  for(int i=0; i < NUMBER_OF_KEYS - 1; i++){
    capSensor[i] = new CapacitiveSensor(13, i + 2);
  }
    
  for(int i=1; i = NUMBER_OF_KEYS; i++){
    pinMode(NUMBER_OF_KEYS + i + 1, OUTPUT);
  }
   
}

void loop() {
  long sensorValue;
  for(int i=0; i < NUMBER_OF_KEYS; i++){
    sensorValue = capSensor[i]->capacitiveSensor(30);
    Serial.println(sensorValue);
    if(sensorValue > threshold){
      digitalWrite(NUMBER_OF_KEYS + 1 + i, HIGH);
    }
    else{
      digitalWrite(NUMBER_OF_KEYS + 1 + i, LOW);
    }
    
    delay(10);
  }    
}
  for(int i=1; i = NUMBER_OF_KEYS; i++){
    pinMode(NUMBER_OF_KEYS + i + 1, OUTPUT);
  }

Starting with i equal 1, while i is equal 2, do some stuff. Obviously, "do some stuff" will never happen.

I'm not seeing any values for sensor readings.

No output? Or nothing but 0?

Stop doing anonymous printing. A stream of values tells you nothing. A stream of name = value pairs DOES tell you something.