Long wait times on multiple capacitive sensor leads

Howdy,

I have been working on putting together a series of capacitive switches to measure the water level of a reservoir for the ranch at my college. The prototype design is a series of three capacitive sensors built from two pins connected by a 1MΩ resistor each. From one of the pins in each sensor is a long wire lead attached to a glass jug at three different heights. I am running it using the Capacitive Sensor library. Diagram is here on imgur, because the forum will not let me upload attachments.

It works with the code I have written, with one caveat: the more leads that are crossing the sensor threshold, the slower the code runs. With one lead active/immersed itʼs fine, and with two it takes less than one second to run a single loop. But when three are active/immersed it takes almost 7 seconds to report each value, leading to an ~21 second loop. If the time increases at this rate measuring a 9 foot reservoir with half-foot resolution seems somewhat untenable.

Does anyone have any advice for how to reduce this exponential growth in time, or any insight into what might be causing it? Thank you!!!

code below

#include <CapacitiveSensor.h>

const int ledPin1 = 13;
const int ledPin2 = 12;
const int ledPin3 = 11;

int threshold = 300;
int timeout = 20;

CapacitiveSensor lowPoint = CapacitiveSensor(3, 2);
CapacitiveSensor midPoint = CapacitiveSensor(5, 4);
CapacitiveSensor highPoint = CapacitiveSensor(10, 8);

void setup() {
  Serial.begin(9600);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
}

void loop() {
  // Taking each sensorʼs measure.
  unsigned int lowVal = constrain(lowPoint.capacitiveSensor(timeout), -5, 1000);
  //  Serial.print(lowVal);
  //  Serial.print("  :  ");
  unsigned int midVal = constrain(midPoint.capacitiveSensor(timeout), -5, 1000);
  //  Serial.print(midVal);
  //  Serial.print("  :  ");
  unsigned int highVal = constrain(highPoint.capacitiveSensor(timeout), -5, 1000);
  //  Serial.print(highVal);
  //  Serial.println("  :  ");

  delay(100);
  
  //report if contact is made to the LEDs.
  lowVal > threshold ? digitalWrite(ledPin1, HIGH) : digitalWrite(ledPin1, LOW);
  midVal > threshold ? digitalWrite(ledPin2, HIGH) : digitalWrite(ledPin2, LOW);
  highVal > threshold ? digitalWrite(ledPin3, HIGH) : digitalWrite(ledPin3, LOW);


  //report the water level to the serial port.
  if (highVal > threshold) {
    Serial.println ("The Water Level is High");
  }
  else if (midVal > threshold) {
    Serial.println ("The Water Level is Medium");
  }
  else if (lowVal > threshold) {
    Serial.println("The Water Level is Low");
  }
  else {
    Serial.println("The Water Level is Empty");
  }
}

The documentation mentions turning off the automatic calibration. I don't see where you did that.
Paul

I think you'll need to reverse the polarity on your LEDs if you want them to illuminate.

As an aside ….Personally I would use float switches rather than capacitive types .
You can buy a hanging switch which turn on its side when it floats on the surface - very easy to use .

They are reversed in reality, I just drew this diagram poorly.

I've been having some trouble with a project involving multiple capacitive sensors myself, but for a single sensor I did write a program that worked quite reliably on my Arduino Uno test rig that did not use the capacitive sensor library and had a much faster response. In my case, the circuit consisted of a 10MOhm resistor across pins 6 and 4, with pin 6 also connected to my touch surface. The internal LED on pin 13 was used as my indicator. My guess is that the library runs effectively the same code, but runs it multiple times per sample to improve stability, but not counting what I presume to be a brown-out condition I encountered with a low quality USB cord, this exact build worked very reliably.

Now, the caveat is that my attempt at building a multi-sensor system has run into some issues. I tried to replicate more or less the design 6 times on a Nano Every, but while testing it seems sometimes the program gets caught in the while loop until I touch the sensor again, and I've encountered strange glitches when touching the LED leads, so I suspect there may be issues related to grounding and how I wired the thing.

long T1 = 0;
long T2 = 0;
long Tn = 0;
int const Threshold = 3000;

void setup() {
  pinMode (4, OUTPUT);
  pinMode (13, OUTPUT);
  pinMode (6, INPUT);
  Serial.begin(9600);

}

void loop() {
  digitalWrite(4, HIGH);
  delay(10);
  digitalWrite(4, LOW);
  T1 = micros();
  while(digitalRead(6)){
    }
 T2 = micros();
 Tn = T2-T1;
 Serial.print(Tn);
 Serial.println();
 digitalWrite(13, Tn >= Threshold);

}

There is nothing better than relaxing in hot water and using a loofah sponge with liquid soap for cleansing the body. You will always feel relaxed after hopping into hot water after a long tiring day. As a part of self care, you must be using few products to clean your body and skin. But there is one product that you might be using while taking a shower or a bath which could cause more harm than good and that is- your Loofah.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.