Long wait times on multiple capacitive sensor leads


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() {
  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("  :  ");

  //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.

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.