Smoothing CapacitorSensor Values

Hello! So I have been working on a system that controllers 110v blacklights through touch, yes I know there is a lot of info about these 110v controllers out there and it has gotten me far but I am still after tireless effort unable to get this jitter out of my system. Most recently I am using Paul Badgers configuration and code as explained in his library (Arduino Playground - CapacitiveSensor)

I have six sensors (using wire and tin foil) and a 6-channel 5V relay board controlling my blacklights, I am using a 10MOhm for each sensor.

What is happening is my numbers within Serial Monitor stay at around 300-400 but then unexpectedly spike up to 15000 or more! I have my threshold set to around this and then it triggers my light. I am using this system as an interactive performance running though MaxforLive to trigger sample in Ableton so jitter will seriously mess up the performance if a sample goes of when its not suppose to.

After alot of reading I read it could have something to do with the grounding so I put both the laptop charger and the Mains on the same ground, still happens. Happens more frequently when I am touching the computer also.

How might I fix this? I am thinking smoothing out the data might work since the spikes are so intermittent.
And/rr fixing the possible ground issue but I'm not sure what else to do about that, I have tried many methods.

Any help would be greatly appreciated!!

Thanks,
Chelsea

#include <CapacitiveSensor.h>

/*
 * CapitiveSense Library Demo Sketch
 * Paul Badger 2008
 * Uses a high value resistor e.g. 10 megohm between send pin and receive pin
 * Resistor effects sensitivity, experiment with values, 50 kilohm - 50 megohm. Larger resistor values yield larger sensor values.
 * Receive pin is the sensor pin - try different amounts of foil/metal on this pin
 * Best results are obtained if sensor foil and wire is covered with an insulator such as paper or plastic sheet
 */

//SENSORS
//ONLY TESTING WITH FIRST SENSOR CURRENTLY
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_7 = CapacitiveSensor(4,7);        
//CapacitiveSensor   cs_4_8 = CapacitiveSensor(4,8);
//CapacitiveSensor   cs_4_12 = CapacitiveSensor(4,12); 
//CapacitiveSensor   cs_4_13 = CapacitiveSensor(4,13); 
//CapacitiveSensor   cs_4_1 = CapacitiveSensor(4,1); 

unsigned long csSum;

//6-channel relay board
const int relayPin1 = 3;
//const int relayPin2 = 5;
//const int relayPin3 = 6;
//const int relayPin4 = 9;
//const int relayPin5 = 10;
//const int relayPin6 = 11;


void setup()                    
{
  //pin setup for relay board
  pinMode(relayPin1, OUTPUT);
  //pinMode(relayPin2, OUTPUT);
  //pinMode(relayPin3, OUTPUT);
  //pinMode(relayPin4, OUTPUT);
  //pinMode(relayPin5, OUTPUT);
  //pinMode(relayPin6, OUTPUT);
  // cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF);     // turn off autocalibrate on channel 1 - just as an example
   Serial.begin(9600);
   // initialize all the readings to 0:

  } 


void loop() {
  
CSread();
}

void CSread() {
long cs = cs_4_2.capacitiveSensor(30);  //a: Sensor resolution is set to 80
 
  if (cs > 100) {                       //b: Arbitrary number
    csSum += cs;
    Serial.println(cs); 
    if (csSum >= 10000)                 //c: This value is the threshold, a High value means it takes longer to trigger
    {
      Serial.print("Trigger: ");
      Serial.println(csSum);
      digitalWrite(relayPin1, LOW);     //Turn on relay 1 if sensor 1 is above threshold
      if (csSum > 0 ) { csSum = 0; }    //Reset
      cs_4_2.reset_CS_AutoCal();        //Stops readings
    }
  } else {
    csSum = 0; //Timeout caused by bad readings
    digitalWrite(relayPin1, HIGH);
  }


    Serial.println(cs);                  // print sensor output 1
    //Serial.print("\t");
    //Serial.println(total2);                  // print sensor output 2
    //Serial.print("\t");
    //Serial.println(total3);                // print sensor output 3

    delay(250); 
}

PaulTest_blacklights.ino (2.55 KB)