Few Questions on Capacitive Sensor Code

I am having some trouble with some code I've been working on and would greatly appreciate some help.

The goal of the project is to use multiple capacitive sensors to convert the physical presence of a person into analog outputs varying between 0 and 5 VDC, which I am using a DAC for.

I am working on some code using the following libraries:


I have a few questions:

1. The output of the DAC is very erratic and I was attempting to use some kind of moving average library (averaging the reading over a rolling finite duration following the current to smooth the signal ( Moving average - Wikipedia ). I was attempting to use one of the following moving average libraries:

I am having trouble getting it to work with my code though. I am attempting to get the value "total1" to be smoothed with the moving average filter. I've tried multiple times to incorporate parts of the example codes into my code to achieve this, but I'm not able to figure it out. If someone could show me how to do it, either with one of the libraries above, or with an alternative method, I would greatly appreciate it.

2. I'm trying to figure out how to make the sensor pick up movement at a greater distance, and to generally be more sensitive. I've tried to experiment with the resistance between pins 4 and 2, with values from 5M to 40M, and also messing with the delay durations, and this is creating some results, but not enough. I've also found that using higher resistances causes the output voltage to be very low, and I've also found that attaching the sensor to larger conductive objects or using high resistances between pins 4 and 2, causes a lot of latency (in that the durations between readings are very long and not very responsive). If someone could help me figure out how to improve this, that would be great.

I've attached the code and libraries to this post as well as two images of my oscilloscope screen showing typical DAC outputs when tuned as best as possible. These are the typical signals that I'm trying to smooth/make more responsive at a longer distance.

The following is my code as it is currently:

#include <CapacitiveSensor.h>
#include <Wire.h>
#include <Adafruit_MCP4725.h>
#include <Adafruit_MCP4728.h>


Adafruit_MCP4728 mcp;
Adafruit_MCP4725 MCP4725;

CapacitiveSensor   cs_4_2 = CapacitiveSensor(4, 2);
CapacitiveSensor   cs_4_6 = CapacitiveSensor(4, 6);


void setup()
{
  cs_4_2.reset_CS_AutoCal();
  cs_4_2.set_CS_AutocaL_Millis(20000);
  cs_4_6.reset_CS_AutoCal();
  cs_4_6.set_CS_AutocaL_Millis(20000);
  Serial.begin(115200);

  MCP4725.begin(0x60);
  delay(10);
  while (!Serial)
  delay(10);
  Serial.println("Adafruit MCP4728 on!");
  if (!mcp.begin()) {
    Serial.println("Failed to find MCP4728 chip");
    while (1) {
      delay(10);

    }
  }
}

void loop()
{
  long start = millis();
  long total1 =  cs_4_2.capacitiveSensor(20);
  long total2 =  cs_4_6.capacitiveSensor(10);
//  total1 = map(total1, 1, 600, 0, 2095);
//  total2 = map(total2, 0, 1000, 0, 4095);
  if (total1 >= 4094) {
  total1 = 4095;
}
  if (total2 >= 4094) {
  total2 = 4095;
}

//  if ((total1 >= 19) && (total1 <= 600)) {
//    total1 = map(total1, 0, 600, 0, 1000);
//  }
//  else if ((total1 >= 0) && (total1 <= 18)) {
//    total1 = map(total1, 0, 1000, 0, 100);
//  } else
//    total1 = map(total1, 0, 1000, 0, 255);
//
//
//  if (millis() - start == 5000) {
//    setup();
//  }


  uint32_t MCP4725_value;
  int adcValueRead = 0;
  float voltageRead = 0;

  float MCP4725_expected_output;

  mcp.setChannelValue(MCP4728_CHANNEL_A, (total1*4));
  mcp.setChannelValue(MCP4728_CHANNEL_B, 1000);
  mcp.setChannelValue(MCP4728_CHANNEL_C, 4095); 
  mcp.setChannelValue(MCP4728_CHANNEL_D, total1);

  MCP4725_expected_output = total1;
  MCP4725.setVoltage(total1, true);
  delay(5);
//  adcValueRead = analogRead((total1+average)/2);

  Serial.print(millis() - start);
  Serial.print("\t");
  
  Serial.print(total1);
  Serial.print("\t");

  Serial.println(total2);
  Serial.print("\t");

  delay(2);

}

Adafruit_MCP4725.h (1.17 KB)

Adafruit_MCP4728.h (3.08 KB)

CapacitiveSense_MPC4728_10-8-20.ino (1.86 KB)

CapacitiveSensor.h (9.77 KB)

It's not possible to discover why the averaging libraries didn't work for you, without seeing your attempts at coding it. Please supply those, and please use code tags to embed code in your post, instructions on how to use those are provided in the permanent threads at the top of the forum.

I don't think it's the libraries, I just don't know where to start with adding the moving average component into my code. My first attempt, where I tried to use MovingAverage.h is:

#include <CapacitiveSensor.h>
#include <Wire.h>
#include <Adafruit_MCP4725.h>
#include <Adafruit_MCP4728.h>

#include <MovingAverage.h>

MovingAverage<unsigned> total1(10, 2);

Adafruit_MCP4728 mcp;
Adafruit_MCP4725 MCP4725;

CapacitiveSensor   cs_4_2 = CapacitiveSensor(4, 2);
CapacitiveSensor   cs_4_6 = CapacitiveSensor(4, 6);


void setup()
{
  cs_4_2.reset_CS_AutoCal();
  cs_4_2.set_CS_AutocaL_Millis(20000);
  cs_4_6.reset_CS_AutoCal();
  cs_4_6.set_CS_AutocaL_Millis(20000);
  Serial.begin(115200);

  MCP4725.begin(0x60);
  delay(10);
  while (!Serial)
    delay(10);
  Serial.println("Adafruit MCP4728 on!");
  if (!mcp.begin()) {
    Serial.println("Failed to find MCP4728 chip");
    while (1) {
      delay(10);

    }
  }
}

void loop()
{
  long start = millis();
  long total1 =  cs_4_2.capacitiveSensor(20);
  long total2 =  cs_4_6.capacitiveSensor(10);
  //  total1 = map(total1, 1, 600, 0, 2095);
  //  total2 = map(total2, 0, 1000, 0, 4095);
  if (total1 >= 4094) {
    total1 = 4095;
  }
  if (total2 >= 4094) {
    total2 = 4095;
  }

  //  if ((total1 >= 19) && (total1 <= 600)) {
  //    total1 = map(total1, 0, 600, 0, 1000);
  //  }
  //  else if ((total1 >= 0) && (total1 <= 18)) {
  //    total1 = map(total1, 0, 1000, 0, 100);
  //  } else
  //    total1 = map(total1, 0, 1000, 0, 255);
  //
  //
  //  if (millis() - start == 5000) {
  //    setup();
  //  }

{
  total1.push(delta_x);
  delta_x += 5;
  if (delta_x > 1000) delta_x = 0;
  for (uint8_t i = 0; i < total1.size(); i++) {
    Serial.print(total1[i]);
    Serial.print(" ");
  }

  uint32_t MCP4725_value;
  int adcValueRead = 0;
  float voltageRead = 0;

  float MCP4725_expected_output;

  mcp.setChannelValue(MCP4728_CHANNEL_A, (total1));
  mcp.setChannelValue(MCP4728_CHANNEL_B, 1000);
  mcp.setChannelValue(MCP4728_CHANNEL_C, 4095);
  mcp.setChannelValue(MCP4728_CHANNEL_D, total1);

  MCP4725_expected_output = total1;
  MCP4725.setVoltage(total1, true);
  delay(5);
  //  adcValueRead = analogRead((total1+average)/2);

  Serial.print(millis() - start);
  Serial.print("\t");

  Serial.print(total1);
  Serial.print("\t");

  Serial.println(total2);
  Serial.print("\t");

  delay(2);

}

And my second attempt, using movingAvg.h is:

#include <CapacitiveSensor.h>
#include <Wire.h>
#include <Adafruit_MCP4725.h>
#include <Adafruit_MCP4728.h>

#include <movingAvg.h>

Adafruit_MCP4728 mcp;
Adafruit_MCP4725 MCP4725;

CapacitiveSensor   cs_4_2 = CapacitiveSensor(4, 2);
CapacitiveSensor   cs_4_6 = CapacitiveSensor(4, 6);

//const uint8_t total1();
movingAvg total1(10);

void setup()
{
  cs_4_2.reset_CS_AutoCal();
  cs_4_2.set_CS_AutocaL_Millis(20000);
  cs_4_6.reset_CS_AutoCal();
  cs_4_6.set_CS_AutocaL_Millis(20000);
  Serial.begin(115200);

  //total1.begin;

  MCP4725.begin(0x60);
  delay(10);
  while (!Serial)
    delay(10);
  Serial.println("Adafruit MCP4728 on!");
  if (!mcp.begin()) {
    Serial.println("Failed to find MCP4728 chip");
    while (1) {
      delay(10);

    }
  }
}

void loop()
{
  long start = millis();
  long total1 =  cs_4_2.capacitiveSensor(20);
  long total2 =  cs_4_6.capacitiveSensor(10);
  //  total1 = map(total1, 1, 600, 0, 2095);
  //  total2 = map(total2, 0, 1000, 0, 4095);
  if (total1 >= 4094) {
    total1 = 4095;
  }
  if (total2 >= 4094) {
    total2 = 4095;
  }


  //  if ((total1 >= 19) && (total1 <= 600)) {
  //    total1 = map(total1, 0, 600, 0, 1000);
  //  }
  //  else if ((total1 >= 0) && (total1 <= 18)) {
  //    total1 = map(total1, 0, 1000, 0, 100);
  //  } else
  //    total1 = map(total1, 0, 1000, 0, 255);
  //
  //
  //  if (millis() - start == 5000) {
  //    setup();
  //  }


  uint32_t MCP4725_value;
  int adcValueRead = 0;
  float voltageRead = 0;

  float MCP4725_expected_output;

  mcp.setChannelValue(MCP4728_CHANNEL_A, (total1));
  mcp.setChannelValue(MCP4728_CHANNEL_B, 1000);
  mcp.setChannelValue(MCP4728_CHANNEL_C, 4095);
  mcp.setChannelValue(MCP4728_CHANNEL_D, total1);

  MCP4725_expected_output = total1;
  MCP4725.setVoltage(total1, true);
  delay(5);
  //  adcValueRead = analogRead((total1+average)/2);

  int pc = analogRead(total1);
  int avg = total1.reading(pc);
  Serial.print(pc);

  Serial.print(millis() - start);
  Serial.print("\t");

  Serial.print(total1);
  Serial.print("\t");

  Serial.println(total2);
  Serial.print("\t");

  delay(2);

}