Looping a library (no, instances) while parsing variables

Hi there,

My first Arduino project and I'm trying to use the CapSense library to read several inputs. Which works, to a degree so far, but now I'm trying to re-read a specific input to see if the state has changed. I need to be able to do other stuff in between so I'd like to do something like the following:

CapSense   cs_1 = CapSense(4,2);
CapSense   cs_2 = CapSense(4,3);
char* sense[]={"cs_1","cs_2"};

void loop()  {
    // Capsense from example:
    long total0 = cs_1.capSense(30);
    long total1 = cs_2.capSense(30);
    ...
    // Some code that tests which one read/sensed the highest value
    // int retest = the index of the highest input
    ...
    if (IfWeSensedSomething == TRUE)  {
        long total[retest] = [b]sense[retest][/b].capSense(30);
    }
}

Sorry for the bad example but I just hope it clarifies what I'm trying to do. What I need is suggestions as to what I'm doing wrong, why and what a better way of coding stuff like this is. I understand that the cs_1 for total0 refers to the initial statement of CapSense(4,2), but does that mean that cs_1.capSense(30) equals CapSense(4,2).capSense(30)? I'm trying to avoid a slew of if statements testing which sensor read the highest value and then test for that one specifically. Rather I'd like to parse which sensor needs to be read again. Thing is I have a good number of sensors, not just two.

What I need is suggestions as to what I'm doing wrong, why and what a better way of coding stuff like this is.

The first thing that is wrong is the supposition in the post title. It makes no sense to loop a library.

The second thing is that instances have addresses. The name of the variable that the address is sssociated with is lost at run time. The Arduino only knows about addresses. So, trying to use the name of a variable to access a method is doomed to failure, as the compiler told you.

You can, however, create an array of instances (addresses) and then use the address in the array to access the method.

CapSense cs[] = 
{ 
   CapSense(4.2),
   CapSense(4,3)
};

long total0 = cs[0].capSense(30);
long total1 = cs[1].capSense(30);

// Some code to figure index to retest

long totalN = cs[index].capSense(30);

Guilty as charged! :slight_smile: Thank you! I'll be reading up on instances...