variable-sized object error

Hi all,

I'm making some pressure sensors using the CapSense library (Arduino Playground - HomePage), and while I am able to get it to work for a few inputs, I am currently trying to think of a way to code it quickly for a large number of inputs.

Here is the beginning of the code for the script I know works:

CapacitiveSensor   cs_4_2 = CapacitiveSensor(4,2);        // 10 megohm resistor between pins 4 & 2, pin 2 is sensor pin, add wire, foil
CapacitiveSensor   cs_4_6 = CapacitiveSensor(4,6);        // 10 megohm resistor between pins 4 & 6, pin 6 is sensor pin, add wire, foil
CapacitiveSensor   cs_4_8 = CapacitiveSensor(4,8);        // 10 megohm resistor between pins 4 & 8, pin 8 is sensor pin, add wire, foil
CapacitiveSensor   cs_4_10 = CapacitiveSensor(4,10);        // 10 megohm resistor between pins 4 & 10, pin 10 is sensor pin, add wire, foil

However, if I were to do this for say 50 inputs, it would get quite tedious, and I have to repeat this a few times in other bits of the code as well. So I tried to put this in the void setup() bit of the code, but keep getting the error: variable-sized object 'capS' may not be initialized even though I thought I initialised it at the top.

void setup()                    
{

  int capS[4];
  for (int i = 3; i < 7; i++){
  //capS[i];
CapacitiveSensor capS[i] = CapacitiveSensor(2,i);// 10 megohm resistor between pins 4 & 2, pin 2 is sensor pin, add wire, foil

}
  
   //capS[3].set_CS_AutocaL_Millis(0xFFFFFFFF);     // turn off autocalibrate on channel 1 - just as an example
   Serial.begin(9600);

}

Also I was wondering, why can you not put a for loop in the initialisation bit at the top of the code? I get error: expected unqualified-id before 'for' if I do.

Thanks,

Raunaq

EDIT: sorry just added something to the code I left out earlier (the [4] after the capS).

  int capS;
  for (int i = 3; i < 7; i++){
  //capS[i];
CapacitiveSensor capS[i]

First, is capS supposed to be an int or an array of instances of the CapacitiveSensor class?

You can't use the same name for both purposes.

Second, you declare an array ONCE. You do NOT declare the array in a for loop.

You INITIALIZE the ith element in the loop.

PaulS:

  int capS;

for (int i = 3; i < 7; i++){
  //capS[i];
CapacitiveSensor capS[i]



First, is capS supposed to be an int or an array of instances of the CapacitiveSensor class?

You can't use the same name for both purposes.

Second, you declare an array ONCE. You do NOT declare the array in a for loop.

You INITIALIZE the ith element in the loop.

Sorry it is supposed to be an array of instances, edited the code just after I posted it to have capS[4].

When initialising the ith element, can I not make it equal to CapacativeSensor(2,i) ?

When initialising the ith element, can I not make it equal to CapacativeSensor(2,i) ?

Probably not. All libraries SHOULD provide a no-argument constructor with methods needed to set all values. But, not all do. For those that don't, like CapacativeSensor, you can't create an array that way.

CapacativeSensor capS[] = { 
   CapacativeSensor(2, 3),
   CapacativeSensor(4, 5),
   CapacativeSensor(7, 8)
}

will create an array of CapacativeSensor objects, with specific pins.

You could create an array of pointers:

CapacativeSensor *capS[5];

Then, you can populate them in a loop:

   capS[i] = new CapacativeSensor(2, i);

Then, you'd use a different operator (->, rather than .) to access the methods of the instances.

The first approach is easier.

Thanks very much Paul!