Help to simplify code - CapacitiveSensor

Good day.

I would like to get some help in simplifying the following code. Is there a better way to handle all the if statements? I can see that is I want to have a lot of sensors it could become messy.

I modified the code from the example in the CapacitiveSensor library.

#include <CapacitiveSensor.h>

CapacitiveSensor   cs_2_3 = CapacitiveSensor(2,3);
CapacitiveSensor   cs_2_4 = CapacitiveSensor(2,4);
CapacitiveSensor   cs_2_5 = CapacitiveSensor(2,5);

int TSW1 = 0;
int TSW2 = 0;
int TSW3 = 0;

void setup()                    
{
   cs_2_3.set_CS_AutocaL_Millis(0xFFFFFFFF);     
   Serial.begin(9600);
}

void loop()                    
{
    long SW1 =  cs_2_3.capacitiveSensor(30);
    long SW2 =  cs_2_4.capacitiveSensor(30);
    long SW3 =  cs_2_5.capacitiveSensor(30);

  if (SW1 > 500)
  {
      TSW1++;
 //     Serial.println(TSW1);
      if (TSW1 == 5)
      {
      Serial.println("SW 1 on");
      }
  }
  else TSW1 = 0;

  if (SW2 > 500)
  {
      TSW2++;
 //     Serial.println(TSW2);
      if (TSW2 == 5)
     {
       Serial.println("SW 2 on");
     }
  }
  else TSW2 = 0;
  
  if (SW3 > 500)
  {
      TSW3++;
 //     Serial.println(TSW3);
      if (TSW3 == 5)
     {
       Serial.println("SW 3 on");
     }
  }
  else TSW3 = 0;

}

Thank you.

Hello,

A for loop is what you need :slight_smile:

First step is an array of integers for TSW and of long for SW and a FOR loop as already suggested:

#include <CapacitiveSensor.h>

#define NUMBER_OF_SENSORS 3

CapacitiveSensor   cs_2_3 = CapacitiveSensor(2,3);
CapacitiveSensor   cs_2_4 = CapacitiveSensor(2,4);
CapacitiveSensor   cs_2_5 = CapacitiveSensor(2,5);

int* _TSW;

void setup()                    
{
   cs_2_3.set_CS_AutocaL_Millis(0xFFFFFFFF);     
   Serial.begin(9600);
   _TSW = (int *)malloc(sizeof(int)*NUMBER_OF_SENSORS);
   for(int i=0; i<NUMBER_OF_SENSORS; i++)
      _TSW[i] = 0;
}

void loop()                    
{

   long* SW = (long *)malloc(sizeof(long)*NUMBER_OF_SENSORS);
   for(int i=0; i<NUMBER_OF_SENSORS; i++)
      SW[i] = cs_x_y.capacitiveSensor(30);   //  <<== PAY ATTENTION HERE, I used x and y, you should use an array even for cs

   for(int i=0; i<NUMBER_OF_SENSORS; i++) {
      if (SW[i] > 500) {
         _TSW[i]++;
         Serial.println(String(_TSW[i]);
         if (TSW1 == 5) {
           Serial.println("SW " + String(i) + " on");
        }
     } else _TSW[i] = 0;
   }
}

Please note that even cs_2_3… must be changed into an array. I left your code for the definition but in the LOOP function I used cs_x_y. If you have problem in converting that (looking at the rest of the code) let me know.

Why so complex? :astonished:

#include <CapacitiveSensor.h>

const uint8_t NUM_SENSORS = 3;

CapacitiveSensor cs[NUM_SENSORS] =
{
  CapacitiveSensor(2,3),
  CapacitiveSensor(2,4),
  CapacitiveSensor(2,5)
};

int TSW[NUM_SENSORS];
long SW[NUM_SENSORS];

void setup()                    
{
   cs[0].set_CS_AutocaL_Millis(0xFFFFFFFF);     
   Serial.begin(9600);
}

void loop()                    
{
  for ( uint8_t i = 0; i < NUM_SENSORS; i++ )
  {
    SW[i] =  cs[i].capacitiveSensor(30);

    if (SW[i] > 500)
    {
      TSW[i] ++;
      //Serial.println(TSW[i]);
      if (TSW[i] == 5)
      {
        Serial.print("SW ");
        Serial.print( i );
        Serial.println( " on");
      }
    }
    else TSW[i] = 0;
  }
}

Next step would be to use a struct…

erasmudj:
Good day.

I would like to get some help in simplifying the following code. Is there a better way to handle all the if statements? I can see that is I want to have a lot of sensors it could become messy.

instead of this:

if (SW1 > 500)
  {
    TSW1++;
    //     Serial.println(TSW1);
    if (TSW1 == 5)
    {
      Serial.println("SW 1 on");
    }
  }
  else TSW1 = 0;

try this:

TSW1 = (SW1 > 500 ? TSW1+1 : 0);
if (TSW1 == 5) Serial.println("SW 1 on");

Why so complex?

It's actually the same complexity, but I used malloc to allocate memory and two more loops to initialize it. Am I missing something? Different style but same complexity to me :slight_smile:

But don't forget he is obviously a beginner, also, using malloc in loop() is really a bad idea.

In fact I made an error: if you want to use my code you have to write

free(SW);

At the end of the loop
Ooopsss